use super::interner::SymbolId;
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
pub struct AltId(u32);
impl AltId {
pub const BLANK: Self = Self(0);
#[must_use]
#[inline]
pub const fn labelled(symbol: SymbolId) -> Option<Self> {
match symbol.get().checked_add(1) {
Some(raw) => Some(Self(raw)),
None => None,
}
}
#[must_use]
#[inline]
pub const fn is_blank(self) -> bool {
self.0 == 0
}
#[must_use]
#[inline]
pub const fn symbol(self) -> Option<SymbolId> {
match self.0 {
0 => None,
raw => Some(SymbolId::from_raw(raw - 1)),
}
}
#[must_use]
#[inline]
pub const fn get(self) -> u32 {
self.0
}
#[must_use]
#[inline]
pub const fn from_raw(raw: u32) -> Self {
Self(raw)
}
}
impl fmt::Debug for AltId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.symbol() {
Some(symbol) => write!(f, "AltId({symbol:?})"),
None => f.write_str("AltId(blank)"),
}
}
}
#[cfg(test)]
#[path = "altloc_tests.rs"]
mod tests;