use crate::{
Error, Result,
sync::{AtomicUsize, Ordering},
};
static NEXT_DOMAIN_ID: AtomicUsize = AtomicUsize::new(1);
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct DomainId(usize);
impl DomainId {
pub const PROCESS: Self = Self(0);
pub fn new() -> Self {
Self(
NEXT_DOMAIN_ID
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |id| id.checked_add(1))
.expect("runtime domain identity space is exhausted"),
)
}
#[inline]
pub(crate) fn ensure(self, actual: Self) -> Result<()> {
if self == actual {
Ok(())
} else {
Err(Error::DomainMismatch {
expected: self,
actual,
})
}
}
}
impl core::fmt::Display for DomainId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}