pub struct Context { /* private fields */ }Expand description
A type map for storing request-scoped data.
Context stores values by their Rust type, allowing type-safe retrieval.
All entries must be owned types that are Send + Sync + 'static.
For thread-safe access, use RwContext instead.
§Example
ⓘ
let mut ctx = Context::new();
ctx.insert(42u32);
ctx.insert("hello".to_string());
assert_eq!(ctx.get::<u32>(), Some(&42));
assert_eq!(ctx.get::<String>(), Some(&"hello".to_string()));Implementations§
Source§impl Context
impl Context
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 an item into the map.
If an item of this type was already stored, it will be replaced and returned.
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 an item of a given type.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Context
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more