Skip to main content

TokenRegistry

Struct TokenRegistry 

Source
pub struct TokenRegistry<T> { /* private fields */ }
Expand description

Waiting closures keyed by token: the shape behind every platform completion that crosses an FFI boundary as a number (docs/bridge.md “Callbacks”).

Rules that make it safe to hand a token to code in another language:

  • Register before the call goes out. A platform may complete synchronously, or fail before returning; a slot that exists first is found either way.
  • An unknown token is a no-op. A late, duplicate, or cancelled completion finds nothing and does nothing.
  • Deliver outside the lock. The closure may register a new token or drop a handle; the registry is never held while it runs.

const-constructible, so a generated static can own one per declaration.

Implementations§

Source§

impl<T> TokenRegistry<T>

Source

pub const fn new() -> Self

An empty registry.

Source

pub fn insert(&self, cb: impl FnOnce(T) + Send + 'static) -> u64

Store cb under a fresh token.

Source

pub fn complete(&self, token: u64, value: T) -> bool

Remove the closure for token and run it with value. false when no closure waits under that token (completed, cancelled, or never registered).

Source

pub fn remove(&self, token: u64) -> bool

Forget token without running its closure: the waiting side gave up.

Source

pub fn contains(&self, token: u64) -> bool

Whether a closure still waits under token.

Source

pub fn len(&self) -> usize

How many closures wait, for tests and diagnostics.

Source

pub fn is_empty(&self) -> bool

Whether nothing waits.

Trait Implementations§

Source§

impl<T> Default for TokenRegistry<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for TokenRegistry<T>

§

impl<T> RefUnwindSafe for TokenRegistry<T>

§

impl<T> Send for TokenRegistry<T>
where Mutex<BTreeMap<u64, Box<dyn FnOnce(T) + Send>>>: Send,

§

impl<T> Sync for TokenRegistry<T>
where Mutex<BTreeMap<u64, Box<dyn FnOnce(T) + Send>>>: Sync,

§

impl<T> Unpin for TokenRegistry<T>
where Mutex<BTreeMap<u64, Box<dyn FnOnce(T) + Send>>>: Unpin,

§

impl<T> UnsafeUnpin for TokenRegistry<T>
where Mutex<BTreeMap<u64, Box<dyn FnOnce(T) + Send>>>: UnsafeUnpin,

§

impl<T> UnwindSafe for TokenRegistry<T>
where Mutex<BTreeMap<u64, Box<dyn FnOnce(T) + Send>>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.