Skip to main content

engine/core/
object_id.rs

1//! Object identity.
2//!
3//! Object identity is independent of object location. Every Engine
4//! object carries an [`ObjectId`] that never changes, even when the object is
5//! moved or renamed.
6
7use std::fmt;
8use std::marker::PhantomData;
9
10/// A strongly typed identifier over a `u64` value.
11///
12/// The `T` parameter exists only to make identifiers of different kinds
13/// distinct at the type level; a [`ObjectId`] and an
14/// [`ActorId`](crate::security::actor::ActorId) can never be
15/// confused. IDs are assigned by the Engine and are sequential starting at 1
16/// (the filesystem root is 1).
17#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
18pub struct Id<T> {
19    value: u64,
20    _marker: PhantomData<fn() -> T>,
21}
22
23impl<T> Id<T> {
24    /// Creates an identifier from a raw `u64` value.
25    ///
26    /// Used by the Engine to mint new IDs and by serialization code. Other
27    /// callers should treat IDs as opaque.
28    pub const fn new(value: u64) -> Self {
29        Self {
30            value,
31            _marker: PhantomData,
32        }
33    }
34
35    /// Returns the raw `u64` value.
36    pub const fn get(self) -> u64 {
37        self.value
38    }
39}
40
41impl<T> fmt::Display for Id<T> {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        self.value.fmt(f)
44    }
45}
46
47/// Marker type distinguishing [`ObjectId`] from other [`Id`] types.
48#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
49pub struct ObjectIdMarker;
50
51/// The identity of an Engine object.
52pub type ObjectId = Id<ObjectIdMarker>;