o3 0.34.0

shared-nothing primitives
Documentation
use std::{marker, pin};

pub use value::Value;

use crate::base::cell::{Sealed, region, values};

mod value;

/// One generative state identity, invariant and unforgeable.
///
/// The trait is sealed: [`Identity`] is the only brand this crate issues, so no
/// consumer can implement a brand or forge one outside the generative scopes.
pub trait Brand: Sealed {}

/// The invariant identity of one generative region.
/// Copyable identity proof carried by capabilities derived from a region token.
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct Identity<'id, const ID: u8 = 0>(marker::PhantomData<*mut &'id ()>);

impl<'id, const ID: u8> Sealed for Identity<'id, ID> {}

impl<'id, const ID: u8> Brand for Identity<'id, ID> {}

impl<'id, const ID: u8> Identity<'id, ID> {
    /// Binds newly owned state to this exact region identity.
    pub const fn bind<T>(self, value: T) -> region::Value<Self, T> {
        values::Value::new(value)
    }
}

/// Exclusive permission for one generative state domain.
pub use crate::base::cell::values::Token;

impl<B: Brand> Token<B> {
    pub fn borrow<'a, T>(&'a self, value: &'a region::Value<B, T>) -> &'a T {
        self.read(value)
    }
}

impl<'id, const ID: u8> Token<Identity<'id, ID>> {
    /// Copies the opaque identity proof for capabilities owned by this region.
    pub const fn proof(&self) -> Identity<'id, ID> {
        Identity(marker::PhantomData)
    }
}

impl Token<Identity<'_>> {
    /// Creates one state domain that cannot escape the operation.
    pub fn scope<R>(f: impl for<'id> FnOnce(Token<Identity<'id>>) -> R) -> R {
        f(Token::new())
    }

    /// Binds one state domain to the full borrow of its pinned root.
    pub fn scope_pinned<T: ?Sized, R>(
        root: pin::Pin<&mut T>,
        f: impl for<'id> FnOnce(Token<Identity<'id>>, pin::Pin<&'id mut T>) -> R,
    ) -> R {
        f(Token::new(), root)
    }
}

const _: () = {
    assert!(size_of::<Identity<'static>>() == 0);
    assert!(size_of::<Identity<'static, 1>>() == 0);
};