use core::fmt;
use enum_as_inner::EnumAsInner;
macro_rules! system_id {
($(#[$($doc_comment:tt)*])* pub struct $name:ident(pub $backing_ty:ty);) => {
$(#[$($doc_comment)*])*
#[derive(Debug, Default, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[repr(transparent)]
pub struct $name(pub $backing_ty);
impl From<$backing_ty> for $name {
fn from(value: $backing_ty) -> Self {
Self(value)
}
}
impl From<$name> for $backing_ty {
fn from(value: $name) -> Self {
value.0
}
}
impl $name {
pub fn idx(self) -> usize {
self.0 as usize
}
}
impl nohash_hasher::IsEnabled for $name {}
impl From<usize> for $name {
fn from(value: usize) -> Self {
Self(value as _)
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<i32> for $name {
fn from(value: i32) -> Self {
Self(value as _)
}
}
#[cfg(feature = "memory-usage")]
impl spacetimedb_memory_usage::MemoryUsage for $name {}
};
}
macro_rules! auto_inc_system_id {
($name:ident) => {
impl $name {
pub const SENTINEL: Self = Self(0);
pub fn is_sentinel(self) -> bool {
self == Self::SENTINEL
}
}
};
}
system_id! {
pub struct TableId(pub u32);
}
auto_inc_system_id!(TableId);
system_id! {
pub struct ViewId(pub u32);
}
auto_inc_system_id!(ViewId);
system_id! {
pub struct ArgId(pub u64);
}
auto_inc_system_id!(ArgId);
system_id! {
pub struct SequenceId(pub u32);
}
auto_inc_system_id!(SequenceId);
system_id! {
pub struct IndexId(pub u32);
}
auto_inc_system_id!(IndexId);
system_id! {
pub struct ConstraintId(pub u32);
}
auto_inc_system_id!(ConstraintId);
system_id! {
pub struct ScheduleId(pub u32);
}
auto_inc_system_id!(ScheduleId);
system_id! {
pub struct ColId(pub u16);
}
system_id! {
pub struct ReducerId(pub u32);
}
system_id! {
pub struct ProcedureId(pub u32);
}
system_id! {
pub struct ViewFnPtr(pub u32);
}
#[derive(Clone, Copy, Debug, EnumAsInner)]
pub enum FunctionId {
Reducer(ReducerId),
Procedure(ProcedureId),
}