pub struct Context { /* private fields */ }Expand description
A type map for request extensions.
All entries into this map must be owned types (or static references).
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.
let mut map = Context::new();
assert_eq!(map.insert(""), None);
assert_eq!(map.insert(1u32), None);
assert_eq!(map.insert(2u32), Some(1u32));
assert_eq!(*map.get::<u32>().unwrap(), 2u32);sourcepub fn contains<T: 'static>(&self) -> bool
pub fn contains<T: 'static>(&self) -> bool
Check if map contains an item of a given type.
let mut map = Context::new();
assert!(!map.contains::<u32>());
assert_eq!(map.insert(1u32), None);
assert!(map.contains::<u32>());sourcepub fn extract<T: 'static>(&self) -> &T
pub fn extract<T: 'static>(&self) -> &T
Get a reference to an item of a given type.
let mut map = Context::new();
map.insert(1u32);
assert_eq!(map.get::<u32>(), Some(&1u32));sourcepub fn get<T: 'static>(&self) -> Option<&T>
pub fn get<T: 'static>(&self) -> Option<&T>
Get a reference to an item of a given type.
let mut map = Context::new();
map.insert(1u32);
assert_eq!(map.get::<u32>(), Some(&1u32));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.
let mut map = Context::new();
map.insert(1u32);
assert_eq!(map.get_mut::<u32>(), Some(&mut 1u32));sourcepub fn remove<T: Send + Sync + 'static>(&mut self) -> Option<T>
pub fn remove<T: Send + Sync + 'static>(&mut self) -> Option<T>
Remove an item from the map of a given type.
If an item of this type was already stored, it will be returned.
let mut map = Context::new();
map.insert(1u32);
assert_eq!(map.get::<u32>(), Some(&1u32));
assert_eq!(map.remove::<u32>(), Some(1u32));
assert!(!map.contains::<u32>());Trait Implementations§
Auto Trait Implementations§
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