use core::num::{
NonZeroU16,
NonZeroU32,
NonZeroUsize,
};
pub trait Symbol: Copy + Eq {
fn try_from_usize(index: usize) -> Option<Self>;
fn to_usize(self) -> usize;
}
#[cfg(feature = "backends")]
#[inline]
pub(crate) fn expect_valid_symbol<S>(index: usize) -> S
where
S: Symbol,
{
S::try_from_usize(index).expect("encountered invalid symbol")
}
pub type DefaultSymbol = SymbolU32;
impl Symbol for usize {
#[inline]
fn try_from_usize(index: usize) -> Option<Self> {
Some(index)
}
#[inline]
fn to_usize(self) -> usize {
self
}
}
macro_rules! gen_symbol_for {
(
$( #[$doc:meta] )*
struct $name:ident($non_zero:ty; $base_ty:ty);
) => {
$( #[$doc] )*
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name {
value: $non_zero,
}
impl Symbol for $name {
#[inline]
fn try_from_usize(index: usize) -> Option<Self> {
<$non_zero>::new((index as $base_ty).wrapping_add(1))
.map(|value| Self { value })
}
#[inline]
fn to_usize(self) -> usize {
self.value.get() as usize - 1
}
}
};
}
gen_symbol_for!(
struct SymbolU16(NonZeroU16; u16);
);
gen_symbol_for!(
struct SymbolU32(NonZeroU32; u32);
);
gen_symbol_for!(
struct SymbolUsize(NonZeroUsize; usize);
);
#[cfg(test)]
mod tests {
use super::*;
use core::mem::size_of;
#[test]
fn same_size_as_u32() {
assert_eq!(size_of::<DefaultSymbol>(), size_of::<u32>());
}
#[test]
fn same_size_as_optional() {
assert_eq!(
size_of::<DefaultSymbol>(),
size_of::<Option<DefaultSymbol>>()
);
}
#[test]
fn try_from_usize_works() {
assert_eq!(
SymbolU16::try_from_usize(0),
Some(SymbolU16 {
value: NonZeroU16::new(1).unwrap()
})
);
assert_eq!(
SymbolU16::try_from_usize(u16::MAX as usize - 1),
Some(SymbolU16 {
value: NonZeroU16::new(u16::MAX).unwrap()
})
);
assert_eq!(SymbolU16::try_from_usize(u16::MAX as usize), None);
assert_eq!(SymbolU16::try_from_usize(usize::MAX), None);
}
macro_rules! gen_test_for {
( $test_name:ident: struct $name:ident($non_zero:ty; $base_ty:ty); ) => {
#[test]
fn $test_name() {
for val in 0..10 {
assert_eq!(
<$name>::try_from_usize(val),
Some($name {
value: <$non_zero>::new(val as $base_ty + 1).unwrap()
})
);
}
assert_eq!(
<$name>::try_from_usize(<$base_ty>::MAX as usize - 1),
Some($name {
value: <$non_zero>::new(<$base_ty>::MAX).unwrap()
})
);
assert_eq!(<$name>::try_from_usize(<$base_ty>::MAX as usize), None);
assert_eq!(<$name>::try_from_usize(<usize>::MAX), None);
}
};
}
gen_test_for!(
try_from_usize_works_for_u16:
struct SymbolU16(NonZeroU16; u16);
);
gen_test_for!(
try_from_usize_works_for_u32:
struct SymbolU32(NonZeroU32; u32);
);
gen_test_for!(
try_from_usize_works_for_usize:
struct SymbolUsize(NonZeroUsize; usize);
);
}