#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Deserialize, Serialize)]
#[repr(transparent)]
pub struct ValueSizeU32(NonZeroU32);
impl TryFrom<usize> for ValueSizeU32
{
type Error = ParseNumberError;
#[inline(always)]
fn try_from(value: usize) -> Result<Self, Self::Error>
{
if unlikely!(value > u32::MAX as usize)
{
Err(ParseNumberError::OutOfRange)
}
else
{
Self::try_from(value as u32)
}
}
}
impl TryFrom<u32> for ValueSizeU32
{
type Error = ParseNumberError;
#[inline(always)]
fn try_from(value: u32) -> Result<Self, Self::Error>
{
if unlikely!(value == 0)
{
Err(ParseNumberError::WasZero)
}
else
{
let non_zero = new_non_zero_u32(value);
Self::try_from(non_zero)
}
}
}
impl TryFrom<NonZeroU32> for ValueSizeU32
{
type Error = ParseNumberError;
#[inline(always)]
fn try_from(value: NonZeroU32) -> Result<Self, Self::Error>
{
if value >= Self::ExclusiveMaximum.0
{
Err(ParseNumberError::TooLarge)
}
else
{
Ok(Self(value))
}
}
}
impl ValueSizeU32
{
const MAX_ORDER: u32 = 11;
pub(crate) const PAGE_SHIFT: u32 = 12;
const KMALLOC_SHIFT_MAX: u32 = Self::MAX_ORDER + Self::PAGE_SHIFT - 1;
const KMALLOC_MAX_SIZE: u32 = 1 << Self::KMALLOC_SHIFT_MAX;
pub const InclusiveMinimum: Self = Self::new_unsafe(1);
pub const ExclusiveMaximum: Self = Self::new_unsafe(Self::KMALLOC_MAX_SIZE / 2);
pub const InclusiveMaximum: Self = Self::new_unsafe(Self::ExclusiveMaximum.0.get() - 1);
#[inline(always)]
const fn new_unsafe(value: u32) -> Self
{
Self(new_non_zero_u32(value))
}
#[inline(always)]
pub(crate) const fn to_non_zero_u32(self) -> NonZeroU32
{
self.0
}
#[inline(always)]
pub(crate) fn try_from_value_size<V: Sized>() -> Result<Self, ParseNumberError>
{
Self::try_from(size_of::<V>())
}
}