nya-core 0.2.0

nya core library
Documentation
//! Byte scales

/// Byte scale (bytes, kilobytes, megabytes, gigabytes, etc)
#[repr(u64)]
#[derive(Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash, Debug)]
pub enum ByteScale {
    /// single byte
    B = 1,
    /// Kilobytes, this is the default value when using [Default]
    #[default]
    KB = 1024,
    /// Megabytes
    MB = Self::KB as u64 * 1024,
    /// Gigabytes
    GB = Self::MB as u64 * 1024,
    /// Terabytes
    TB = Self::GB as u64 * 1024,
    /// Petabytes
    PB = Self::TB as u64 * 1024,
    /// Exabytes
    EB = Self::PB as u64 * 1024,
}

impl Into<u64> for ByteScale {
    fn into(self) -> u64 {
        self as _
    }
}

impl Into<usize> for ByteScale {
    fn into(self) -> usize {
        #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
        assert!(self < Self::MB, "pointer width not 32 or 64");

        #[cfg(not(target_pointer_width = "64"))]
        assert!(self < Self::TB, "pointer width not 64");

        self as _
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sizes() {
        println!("{}", ByteScale::MB as u64);
        println!("{}", ByteScale::GB as u64);
        println!("{}", ByteScale::TB as u64);
        println!("{}", ByteScale::PB as u64);
        println!("{}", ByteScale::EB as u64);
    }
}