use std::{
iter::Sum,
ops::{Add, Mul},
};
#[derive(Debug, Clone, Copy, Default)]
pub struct Size(usize);
impl Size {
pub const fn in_bytes(self) -> usize {
self.0
}
pub const fn in_slots(self) -> usize {
self.0 / SLOT_SIZE
}
}
impl Mul<Size> for usize {
type Output = Size;
fn mul(self, other: Size) -> Size {
Size(self * other.0)
}
}
impl Add<Size> for Size {
type Output = Size;
fn add(self, other: Size) -> Size {
Size(self.0 + other.0)
}
}
impl Sum for Size {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let mut sum = Size::default();
for el in iter {
sum = sum + el;
}
sum
}
}
pub trait Unit: Send + Sync + Copy {
const UNIT: Size;
}
#[derive(Debug, Clone, Copy)]
pub struct InBytes;
impl Unit for InBytes {
const UNIT: Size = Size(1);
}
#[derive(Debug, Clone, Copy)]
pub struct InSlots;
const SLOT_SIZE: usize = 8;
impl Unit for InSlots {
const UNIT: Size = Size(SLOT_SIZE);
}