use core::{
num::NonZeroU64,
sync::atomic::{AtomicU64, Ordering},
};
use tributary_proto::ScopeId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InstanceId(NonZeroU64);
impl InstanceId {
pub(crate) const fn get(&self) -> u64 {
self.0.get()
}
pub(crate) fn mint() -> Self {
static NEXT_INSTANCE: AtomicU64 = AtomicU64::new(0);
let raw = NEXT_INSTANCE
.fetch_add(1, Ordering::Relaxed)
.wrapping_add(1);
Self(NonZeroU64::new(raw).expect("process-global instance id counter wrapped the u64 space"))
}
#[cfg(test)]
pub(crate) const TEST: Self = Self(NonZeroU64::MIN);
}
impl core::fmt::Display for InstanceId {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(&self.0, f)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Subscription {
instance: InstanceId,
scope: ScopeId,
}
impl Subscription {
#[inline]
pub(crate) const fn new(instance: InstanceId, scope: ScopeId) -> Self {
Self { instance, scope }
}
#[cfg(test)]
pub(crate) const fn for_test(scope: ScopeId) -> Self {
Self::new(InstanceId::TEST, scope)
}
#[inline]
pub const fn instance(&self) -> InstanceId {
self.instance
}
#[inline]
pub const fn id(&self) -> ScopeId {
self.scope
}
}
impl core::fmt::Display for Subscription {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(&self.scope, f)
}
}