TokenTrait

Trait TokenTrait 

Source
pub trait TokenTrait: Sized {
    type ConstructionError;
    type RunError;
    type Identifier;
    type ComparisonError;
    type Branded<'a>;

    // Required methods
    fn new() -> Result<Self, Self::ConstructionError>;
    fn with_token<R, F: for<'a> FnOnce(Self::Branded<'a>) -> R>(
        f: F,
    ) -> Result<R, Self::RunError>;
    fn identifier(&self) -> Self::Identifier;
    fn compare(
        &self,
        id: &Self::Identifier,
    ) -> Result<(), Self::ComparisonError>;
}
Expand description

A trait for tokens

Required Associated Types§

Source

type ConstructionError

Constructing a token may fail.

Source

type RunError

TokenTrait::with_token may fail, typically if construction failed.

Some types, like GhostToken are inconstructible with TokenTrait::new, but cannot fail to run, hence the distinction.

Source

type Identifier

Lets a TokenCell keep track of the token.

In most cases, this is a ZST in release mode.

Source

type ComparisonError

core::convert::Infallible unless TokenTrait::compare is fallible (ie. comparison is done at runtime).

Source

type Branded<'a>

Rebrands the token, this is necessary for GhostToken to function properly

Required Methods§

Source

fn new() -> Result<Self, Self::ConstructionError>

Constructs a new Token.

§Errors

Some token implementations may chose to be constructible only once, or only while holding a given lock.

Source

fn with_token<R, F: for<'a> FnOnce(Self::Branded<'a>) -> R>( f: F, ) -> Result<R, Self::RunError>

Constructs a new, lifetime-branded Token, and provides it to the closure.

This is especially useful for GhostToken, which can only be constructed that way, as they use lifetimes to obtain a unique brand.

§Errors

If construction failed, so will this.

Source

fn identifier(&self) -> Self::Identifier

Returns the Token’s identifier, which cells may store to allow comparison.

Source

fn compare(&self, id: &Self::Identifier) -> Result<(), Self::ComparisonError>

Allows the cell to compare its identifier to the Token.

§Errors

If a wrong token was mistakenly passed to the cell.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§