pub trait Offset: Copy + Send + Sync + 'static {
const TYPE_NAME: &'static str;
fn try_from_usize(value: usize) -> Option<Self>;
fn to_usize(self) -> usize;
}
#[macro_export]
macro_rules! impl_offset {
($wrapper:ty: $inner:ty) => {
impl $crate::Offset for $wrapper {
const TYPE_NAME: &'static str = stringify!($wrapper);
#[inline]
fn try_from_usize(value: usize) -> Option<Self> {
<$inner as $crate::Offset>::try_from_usize(value).map(Self)
}
#[inline]
fn to_usize(self) -> usize {
<$inner as $crate::Offset>::to_usize(self.0)
}
}
};
($wrapper:ty: $inner:ty, $($rest:tt)*) => {
$crate::impl_offset!($wrapper: $inner);
$crate::impl_offset!($($rest)*);
};
($($ty:ty),+ $(,)?) => {
$(
impl $crate::Offset for $ty {
const TYPE_NAME: &'static str = stringify!($ty);
#[inline]
fn try_from_usize(value: usize) -> Option<Self> {
<Self as core::convert::TryFrom<usize>>::try_from(value).ok()
}
#[inline]
fn to_usize(self) -> usize {
self as usize
}
}
)+
};
}
#[cfg(target_pointer_width = "64")]
crate::impl_offset!(u8, u16, u32, u64, usize);
#[cfg(target_pointer_width = "32")]
crate::impl_offset!(u8, u16, u32, usize);
#[cfg(target_pointer_width = "16")]
crate::impl_offset!(u8, u16, usize);
#[cfg(not(any(
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64"
)))]
compile_error!("lite-strtab requires a 16-bit, 32-bit, or 64-bit target");