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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
use crate::*;
use std::ops::{Add, Mul};
/// `Size` represents a non-negative number of bytes or bits.
///
/// It is basically a copy of the `Size` type in the Rust compiler.
/// See [Size](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/abi/struct.Size.html).
///
/// Note that the `Size` type has no upper-bound.
/// Users needs check whether a given `Size` is too large for their Machine themselves.
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Hash, GcCompat)]
pub struct Size { raw: Int }
impl Size {
/// The "0 bytes" size.
pub const ZERO: Size = Size { raw: Int::ZERO };
/// Returns None if `bits` is negative or not divisible by 8.
pub fn from_bits(bits: impl Into<Int>) -> Option<Size> {
let bits = bits.into();
if bits % 8 != 0 { return None; };
if bits < 0 { return None; }
let raw = bits / 8;
Some(Size { raw })
}
/// Variation of `from_bits` for const contexts.
/// Returns None if `bits` is not divisible by 8.
pub const fn from_bits_const(bits: u64) -> Option<Size> {
if bits % 8 != 0 { return None; }
let bytes = bits / 8;
let raw = Int::from(bytes);
Some(Size { raw })
}
/// Returns None if `bytes` is negative.
pub fn from_bytes(bytes: impl Into<Int>) -> Option<Size> {
let bytes = bytes.into();
if bytes < 0 { return None; }
Some(Size { raw: bytes })
}
/// Variation of `from_bytes` for const contexts.
/// Cannot fail since the input is unsigned and already in bytes.
pub const fn from_bytes_const(bytes: u64) -> Size {
let raw = Int::from(bytes);
Size { raw }
}
/// The number of bytes of `self`
pub fn bytes(self) -> Int { self.raw }
/// The number of bits of `self`
pub fn bits(self) -> Int { self.raw * 8 }
/// Returns whether `self` is `Size::ZERO`
pub fn is_zero(&self) -> bool {
self.bytes() == 0
}
}
impl Add for Size {
type Output = Size;
fn add(self, rhs: Size) -> Size {
let b = self.bytes() + rhs.bytes();
Size::from_bytes(b).unwrap()
}
}
impl Mul<Int> for Size {
type Output = Size;
fn mul(self, rhs: Int) -> Size {
let b = self.bytes() * rhs;
Size::from_bytes(b).unwrap()
}
}
impl Mul<Size> for Int {
type Output = Size;
fn mul(self, rhs: Size) -> Size {
let b = self * rhs.bytes();
Size::from_bytes(b).unwrap()
}
}