gabi/gabi.rs
1use gabi::*;
2
3fn print_type_of<T>(_: &T) {
4 println!("{}", std::any::type_name::<T>())
5}
6fn main() {
7 // Create a config struct with parameters like this:
8 let _bb = BytesConfig::new(BytesBase::Bibyte, 2, false);
9
10 // Or alternatively use defaults (default values are the same as above)
11 let bb = BytesConfig::default();
12
13 // And optionally use setters to modify parameters
14 bb.set_base(BytesBase::Gabyte);
15 bb.set_precision(3);
16 bb.set_aligned(true);
17
18 // Then instanciate `Bytes` structs that contains values
19 // that can be displayed the chosen configuration options
20 let a = bb.bytes(124 as u8);
21 let b = bb.bytes(5247 as u16);
22 let c = bb.bytes(156765 as u32);
23 let d = bb.bytes(5259731856 as u64);
24
25 // But not:
26 // let e = bb.bytes("5");
27 // let f = bb.bytes(5 as i8);
28 // let g = bb.bytes(5 as f64);
29
30 // Use into_inner to get back the internal number store (consumes the Byte instance)
31 print!("{} stored as ", a);
32 print_type_of(&a.into_inner()); // 124 B stored as u8
33
34 print!("{} stored as ", b);
35 print_type_of(&b.into_inner()); // 5.25 KB stored as u16
36
37 print!("{} stored as ", c);
38 print_type_of(&c.into_inner()); // 156.76 KB stored as u32
39
40 print!("{} stored as ", d);
41 print_type_of(&d.into_inner()); // 5.26 GB stored as u64
42
43 // Create new instances
44 let my_byte = bb.bytes(152585 as u32);
45
46 // Display it with the current preferences
47 println!("{}", my_byte);
48
49 // Modify preferences
50 bb.set_precision(1);
51 bb.set_aligned(true);
52
53 // Create another instance
54 let my_byte2 = bb.bytes(248 as u32);
55
56 // Display instances with modified preferences
57 println!("{}", my_byte);
58 println!("{}", my_byte2);
59}