Skip to main content

Context

Struct Context 

Source
pub struct Context<'ty, 'r> { /* private fields */ }
Expand description

Re-export the main Context type. Runtime context storing values by type.

The context can store owned values as well as borrowed references (immutable or mutable). Values are keyed by TypeId using a specialized hasher for fast lookups.

Implementations§

Source§

impl<'ty, 'r> Context<'ty, 'r>

Source

pub fn new() -> Self

Create a new empty Context.

Source

pub fn insert_unchecked(&mut self, key: TypeId, data: Data<'ty, 'r>)

Insert a value into the context without checking the type.

This is a low-level escape hatch for advanced use-cases.

Source

pub fn insert_ref<T: ShareableTid<'ty>>(&mut self, value: &'r T)

Insert a borrowed value into the context.

Source

pub fn insert_mut<T: ShareableTid<'ty>>(&mut self, value: &'r mut T)

Insert a mutable reference into the context.

Source

pub fn insert<T: ShareableTid<'ty>>(&mut self, value: T)

Insert an owned value into the context.

Source

pub fn get<'b, T: ShareableTid<'ty>>(&'b self) -> Option<&'b T>

Get a shared reference to a stored value by type.

Source

pub fn get_mut<'b, T: ShareableTid<'ty>>(&'b mut self) -> Option<&'b mut T>

Get a mutable reference to a stored value by type.

Source

pub fn get_data<'b>(&'b self, id: &TypeId) -> Option<&'b Data<'ty, 'r>>

Get a stored Data by TypeId.

Source

pub fn get_data_mut<'b>( &'b mut self, id: &TypeId, ) -> Option<&'b mut Data<'ty, 'r>>

Get a mutable Data by TypeId.

Source

pub fn get_disjoint_mut<'b, const N: usize>( &'b mut self, keys: [&TypeId; N], ) -> [Option<&'b mut Data<'ty, 'r>>; N]

Get multiple mutable Data entries by distinct TypeIds.

Source

pub fn take<T: ShareableTid<'ty>>(&mut self) -> Option<T>

Remove an owned value from the context and return it.

Source

pub fn remove<T: ShareableTid<'ty>>(&mut self) -> Option<Data<'ty, 'r>>

Remove any stored value for the given type and return the raw Data.

Source

pub fn contains<T: ShareableTid<'ty>>(&self) -> bool

Check if a value of a specific type is present.

Source

pub fn clear(&mut self)

Clear all values from the context.

Trait Implementations§

Source§

impl Default for Context<'_, '_>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'ty, 'r> Freeze for Context<'ty, 'r>

§

impl<'ty, 'r> !RefUnwindSafe for Context<'ty, 'r>

§

impl<'ty, 'r> Send for Context<'ty, 'r>

§

impl<'ty, 'r> Sync for Context<'ty, 'r>

§

impl<'ty, 'r> Unpin for Context<'ty, 'r>

§

impl<'ty, 'r> !UnwindSafe for Context<'ty, 'r>

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> AnyExt for T
where T: Any + ?Sized,

Source§

fn downcast_ref<T>(this: &Self) -> Option<&T>
where T: Any,

Attempts to downcast this to T behind reference
Source§

fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>
where T: Any,

Attempts to downcast this to T behind mutable reference
Source§

fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
where T: Any,

Attempts to downcast this to T behind Rc pointer
Source§

fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
where T: Any,

Attempts to downcast this to T behind Arc pointer
Source§

fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>
where T: Any,

Attempts to downcast this to T behind Box pointer
Source§

fn downcast_move<T>(this: Self) -> Option<T>
where T: Any, Self: Sized,

Attempts to downcast owned Self to T, useful only in generic context as a workaround for specialization
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.