use std::fmt;
use std::num::NonZeroU32;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct NonZeroId(NonZeroU32);
impl From<NonZeroU32> for NonZeroId {
fn from(value: NonZeroU32) -> Self {
Self(value)
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
#[repr(transparent)]
pub struct Id(Option<NonZeroId>);
impl Id {
pub(crate) const fn new(value: NonZeroId) -> Self {
Self(Some(value))
}
pub(crate) fn is_set(&self) -> bool {
self.0.is_some()
}
pub(crate) fn set(&mut self, value: NonZeroId) {
debug_assert!(self.0.is_none(), "id should not be set multiple times");
self.0 = Some(value);
}
pub(crate) fn as_ref(&self) -> Option<&NonZeroId> {
self.0.as_ref()
}
}
impl fmt::Debug for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
Some(n) => write!(f, "Id({})", n.0.get()),
None => write!(f, "Id(*)"),
}
}
}