1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct PrettySize(pub u64);
const UNIT_NAMES: [&str; 5] = ["b", "kb", "mb", "gb", "pb"];
impl std::fmt::Debug for PrettySize {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
let size = self.0 as f32;
let unit_pow = size.log(1024.0).clamp(0.0, 4.0).floor();
let unit_size_in_bytes = 1024.0_f32.powf(unit_pow);
let size_in_units = size / unit_size_in_bytes;
let unit_name = UNIT_NAMES[unit_pow as usize];
f.write_fmt(format_args!("{} {}", size_in_units, unit_name))
} else {
f.write_fmt(format_args!("{}", self.0))
}
}
}
impl std::fmt::Display for PrettySize {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{:#?}", self))
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct PrettyBitflag(pub u32);
impl std::fmt::Debug for PrettyBitflag {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{:b}", self.0))
}
}
impl std::fmt::Display for PrettyBitflag {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{:#?}", self))
}
}