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§
Sourcetype ConstructionError
type ConstructionError
Constructing a token may fail.
Sourcetype RunError
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.
Sourcetype Identifier
type Identifier
Lets a TokenCell
keep track of the token.
In most cases, this is a ZST in release mode.
Sourcetype ComparisonError
type ComparisonError
core::convert::Infallible
unless TokenTrait::compare
is fallible (ie. comparison is done at runtime).
Sourcetype Branded<'a>
type Branded<'a>
Rebrands the token, this is necessary for GhostToken
to function properly
Required Methods§
Sourcefn new() -> Result<Self, Self::ConstructionError>
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.
Sourcefn with_token<R, F: for<'a> FnOnce(Self::Branded<'a>) -> R>(
f: F,
) -> Result<R, Self::RunError>
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.
Sourcefn identifier(&self) -> Self::Identifier
fn identifier(&self) -> Self::Identifier
Returns the Token’s identifier, which cells may store to allow comparison.
Sourcefn compare(&self, id: &Self::Identifier) -> Result<(), Self::ComparisonError>
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.