#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NumBytes(pub(crate) usize);
impl NumBytes {
pub const MAX: NumBytes = NumBytes(usize::MAX);
#[must_use]
pub fn zero() -> Self {
Self(0)
}
pub(crate) fn assert_non_zero(self, parameter_name: &str) {
assert!(
self.0 > 0,
"{parameter_name} must be greater than zero bytes"
);
}
#[must_use]
pub fn bytes(&self) -> usize {
self.0
}
}
pub trait NumBytesExt {
fn bytes(self) -> NumBytes;
fn kilobytes(self) -> NumBytes;
fn megabytes(self) -> NumBytes;
}
impl NumBytesExt for usize {
fn bytes(self) -> NumBytes {
NumBytes(self)
}
fn kilobytes(self) -> NumBytes {
NumBytes(self * 1024)
}
fn megabytes(self) -> NumBytes {
NumBytes(self * 1024 * 1024)
}
}