use crate::BitOffset;
pub(crate) const BITS_PER_BYTE: usize = 8;
pub trait Sized {
fn bits() -> usize;
fn bytes() -> usize {
Self::bits().div_ceil(BITS_PER_BYTE)
}
fn least_bytes() -> usize {
Self::bits() / BITS_PER_BYTE
}
fn bytes_and_bits() -> (usize, BitOffset) {
let bits = Self::bits();
(bits / BITS_PER_BYTE, BitOffset::new_wrapped(bits))
}
}
macro_rules! impl_sized_literal {
($(($type:ty, $size:expr)),*) => {
$(
impl self::Sized for $type {
fn bits() -> usize {
$size
}
}
)*
};
}
impl_sized_literal! {
((), 0),
(bool, 1),
(u8, 8),
(u16, 16),
(u32, 32),
(u64, 64),
(u128, 128),
(i8, 8),
(i16, 16),
(i32, 32),
(i64, 64),
(i128, 128),
(f32, 32),
(f64, 64),
(char, 32),
(core::net::Ipv4Addr, 32),
(core::net::Ipv6Addr, 128)
}
impl<T, const N: usize> self::Sized for [T; N]
where
T: self::Sized,
{
fn bits() -> usize {
T::bits() * N
}
}