vce-engine 0.1.1

The generic state substrate of the Virtual Computing Engine (VCE)
Documentation
//! Object identity.
//!
//! Object identity is independent of object location. Every Engine
//! object carries an [`ObjectId`] that never changes, even when the object is
//! moved or renamed.

use std::fmt;
use std::marker::PhantomData;

/// A strongly typed identifier over a `u64` value.
///
/// The `T` parameter exists only to make identifiers of different kinds
/// distinct at the type level; a [`ObjectId`] and an
/// [`ActorId`](crate::security::actor::ActorId) can never be
/// confused. IDs are assigned by the Engine and are sequential starting at 1
/// (the filesystem root is 1).
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct Id<T> {
    value: u64,
    _marker: PhantomData<fn() -> T>,
}

impl<T> Id<T> {
    /// Creates an identifier from a raw `u64` value.
    ///
    /// Used by the Engine to mint new IDs and by serialization code. Other
    /// callers should treat IDs as opaque.
    pub const fn new(value: u64) -> Self {
        Self {
            value,
            _marker: PhantomData,
        }
    }

    /// Returns the raw `u64` value.
    pub const fn get(self) -> u64 {
        self.value
    }
}

impl<T> fmt::Display for Id<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.value.fmt(f)
    }
}

/// Marker type distinguishing [`ObjectId`] from other [`Id`] types.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct ObjectIdMarker;

/// The identity of an Engine object.
pub type ObjectId = Id<ObjectIdMarker>;