3 const UNITS: [&str; 4] = ["B", "kB", "MB", "GB"];
10 pub fn new(bytes: u64) -> Self {
17 impl std::fmt::Display for Bytes {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 debug_assert!(num >= 0.0);
22 return write!(f, "{} B", num);
24 let delimiter = 1000_f64;
25 let exponent = cmp::min((num.ln() / 6.90775).floor() as i32, 4);
27 write!(f, "{:.2} ", num / delimiter.powi(exponent))?;
28 write!(f, "{}", UNITS[exponent as usize])