spin_resource_table

Struct Table

Source
pub struct Table<V> { /* private fields */ }
Expand description

This is a table for generating unique u32 identifiers for each element in a dynamically-changing set of resources.

This is inspired by the Table type in wasi-common and serves the same purpose: allow opaque resources and their lifetimes to be managed across an interface boundary, analogous to how file handles work across the user-kernel boundary.

Implementations§

Source§

impl<V> Table<V>

Source

pub fn new(capacity: u32) -> Self

Create a new, empty table with the specified capacity.

Source

pub fn push(&mut self, value: V) -> Result<u32, ()>

Add the specified resource to this table.

If the table is full (i.e. there already are self.capacity resources inside), this returns Err(()). Otherwise, a new, unique identifier is allocated for the resource and returned.

This function will attempt to avoid reusing recently closed identifiers, but after 2^32 calls to this function they will start repeating.

Source

pub fn get(&self, key: u32) -> Option<&V>

Get a reference to the resource identified by the specified key, if it exists.

Source

pub fn get_mut(&mut self, key: u32) -> Option<&mut V>

Get a mutable reference to the resource identified by the specified key, if it exists.

Source

pub fn remove(&mut self, key: u32) -> Option<V>

Remove the resource identified by the specified key, if present.

This makes the key eligible for eventual reuse (i.e. for a newly-pushed resource).

Trait Implementations§

Source§

impl<V> Default for Table<V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<V> Freeze for Table<V>

§

impl<V> RefUnwindSafe for Table<V>
where V: RefUnwindSafe,

§

impl<V> Send for Table<V>
where V: Send,

§

impl<V> Sync for Table<V>
where V: Sync,

§

impl<V> Unpin for Table<V>
where V: Unpin,

§

impl<V> UnwindSafe for Table<V>
where V: 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.