Struct task_local_extensions::Extensions
source · pub struct Extensions { /* private fields */ }
Expand description
Extensions
is a type map: values are stored and retrieved using their
TypeId
.
This allows storing arbitrary data that implements Sync + Send + 'static
. This is
useful when you need to share data between different middlewares in the middleware chain
or make some values available from the handler to middlewares
on the outgoing path (e.g. error class).
Implementations§
source§impl Extensions
impl Extensions
sourcepub fn with<T: Send + Sync + 'static>(self, val: T) -> Self
pub fn with<T: Send + Sync + 'static>(self, val: T) -> Self
Insert a value ino this Extensions
, returning self instead of any pre-inserted values.
This is useful for any builder style patterns
let ext = Extensions::new().with(true).with(5_i32);
assert_eq!(ext.get(), Some(&true));
assert_eq!(ext.get(), Some(&5_i32));
sourcepub fn append(&mut self, other: &mut Self)
pub fn append(&mut self, other: &mut Self)
Removes the values from other
and inserts them into self
.
sourcepub fn insert<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T>
pub fn insert<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T>
Insert a value into this Extensions
.
If a value of this type already exists, it will be returned.
sourcepub fn get<T: 'static>(&self) -> Option<&T>
pub fn get<T: 'static>(&self) -> Option<&T>
Get a reference to a value previously inserted on this Extensions
.
sourcepub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
Get a mutable reference to a value previously inserted on this Extensions
.