#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ItemIndex(u16);
impl ItemIndex {
pub fn new(id: usize) -> Self {
Self(id.try_into().expect("invalid item index: too many items"))
}
#[inline(always)]
pub const fn const_new(id: u16) -> Self {
Self(id)
}
#[inline(always)]
pub const fn as_usize(&self) -> usize {
self.0 as usize
}
}
impl core::fmt::Display for ItemIndex {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", &self.as_usize())
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GlobalItemIndex {
pub module: ModuleIndex,
pub index: ItemIndex,
}
impl core::fmt::Display for GlobalItemIndex {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}:{}", &self.module, &self.index)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ModuleIndex(u16);
impl ModuleIndex {
pub fn new(index: usize) -> Self {
Self(index.try_into().expect("invalid module index: too many modules"))
}
pub const fn const_new(index: u16) -> Self {
Self(index)
}
#[inline(always)]
pub const fn as_usize(&self) -> usize {
self.0 as usize
}
}
impl core::ops::Add<ItemIndex> for ModuleIndex {
type Output = GlobalItemIndex;
fn add(self, rhs: ItemIndex) -> Self::Output {
GlobalItemIndex { module: self, index: rhs }
}
}
impl core::fmt::Display for ModuleIndex {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", &self.as_usize())
}
}