Skip to main content

xtk_core/num/
sizes.rs

1//! Byte scales
2
3/// Byte scale (bytes, kilobytes, megabytes, gigabytes, etc)
4#[repr(u64)]
5#[derive(Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash, Debug)]
6pub enum ByteScale {
7    /// single byte
8    B = 1,
9    /// Kilobytes, this is the default value when using [Default]
10    #[default]
11    KB = 1024,
12    /// Megabytes
13    MB = Self::KB as u64 * 1024,
14    /// Gigabytes
15    GB = Self::MB as u64 * 1024,
16    /// Terabytes
17    TB = Self::GB as u64 * 1024,
18    /// Petabytes
19    PB = Self::TB as u64 * 1024,
20    /// Exabytes
21    EB = Self::PB as u64 * 1024,
22}
23
24impl Into<u64> for ByteScale {
25    fn into(self) -> u64 {
26        self as _
27    }
28}
29
30impl Into<usize> for ByteScale {
31    fn into(self) -> usize {
32        #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
33        assert!(self < Self::MB, "pointer width not 32 or 64");
34
35        #[cfg(not(target_pointer_width = "64"))]
36        assert!(self < Self::TB, "pointer width not 64");
37
38        self as _
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn sizes() {
48        println!("{}", ByteScale::MB as u64);
49        println!("{}", ByteScale::GB as u64);
50        println!("{}", ByteScale::TB as u64);
51        println!("{}", ByteScale::PB as u64);
52        println!("{}", ByteScale::EB as u64);
53    }
54}