pub struct Extensions { /* 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 Extensions
impl Extensions
Sourcepub fn new() -> Extensions
pub fn new() -> Extensions
Creates an empty Extensions
.
Sourcepub fn insert<T>(&mut self, val: T) -> Option<T>where
T: 'static,
pub fn insert<T>(&mut self, val: T) -> Option<T>where
T: 'static,
Insert an item into the map.
If an item of this type was already stored, it will be replaced and returned.
let mut map = Extensions::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>(&self) -> boolwhere
T: 'static,
pub fn contains<T>(&self) -> boolwhere
T: 'static,
Check if map contains an item of a given type.
let mut map = Extensions::new();
assert!(!map.contains::<u32>());
assert_eq!(map.insert(1u32), None);
assert!(map.contains::<u32>());
Sourcepub fn get<T>(&self) -> Option<&T>where
T: 'static,
pub fn get<T>(&self) -> Option<&T>where
T: 'static,
Get a reference to an item of a given type.
let mut map = Extensions::new();
map.insert(1u32);
assert_eq!(map.get::<u32>(), Some(&1u32));
Sourcepub fn get_mut<T>(&mut self) -> Option<&mut T>where
T: 'static,
pub fn get_mut<T>(&mut self) -> Option<&mut T>where
T: 'static,
Get a mutable reference to an item of a given type.
let mut map = Extensions::new();
map.insert(1u32);
assert_eq!(map.get_mut::<u32>(), Some(&mut 1u32));
Sourcepub fn get_or_insert<T>(&mut self, value: T) -> &mut Twhere
T: 'static,
pub fn get_or_insert<T>(&mut self, value: T) -> &mut Twhere
T: 'static,
Inserts the given value
into the extensions if it is not present, then returns a reference
to the value in the extensions.
let mut map = Extensions::new();
assert_eq!(map.get::<Vec<u32>>(), None);
map.get_or_insert(Vec::<u32>::new()).push(1);
assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1]));
map.get_or_insert(Vec::<u32>::new()).push(2);
assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1,2]));
Sourcepub fn get_or_insert_with<T, F>(&mut self, default: F) -> &mut Twhere
T: 'static,
F: FnOnce() -> T,
pub fn get_or_insert_with<T, F>(&mut self, default: F) -> &mut Twhere
T: 'static,
F: FnOnce() -> T,
Inserts a value computed from f
into the extensions if the given value
is not present,
then returns a reference to the value in the extensions.
let mut map = Extensions::new();
assert_eq!(map.get::<Vec<u32>>(), None);
map.get_or_insert_with(Vec::<u32>::new).push(1);
assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1]));
map.get_or_insert_with(Vec::<u32>::new).push(2);
assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1,2]));
Sourcepub fn remove<T>(&mut self) -> Option<T>where
T: 'static,
pub fn remove<T>(&mut self) -> Option<T>where
T: 'static,
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 = Extensions::new();
map.insert(1u32);
assert_eq!(map.get::<u32>(), Some(&1u32));
assert_eq!(map.remove::<u32>(), Some(1u32));
assert!(!map.contains::<u32>());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the Extensions
of all inserted extensions.
let mut map = Extensions::new();
map.insert(1u32);
assert!(map.contains::<u32>());
map.clear();
assert!(!map.contains::<u32>());
Sourcepub fn extend(&mut self, other: Extensions)
pub fn extend(&mut self, other: Extensions)
Extends self with the items from another Extensions
.
Trait Implementations§
Source§impl Debug for Extensions
impl Debug for Extensions
Source§impl Default for Extensions
impl Default for Extensions
Source§fn default() -> Extensions
fn default() -> Extensions
Auto Trait Implementations§
impl Freeze for Extensions
impl !RefUnwindSafe for Extensions
impl !Send for Extensions
impl !Sync for Extensions
impl Unpin for Extensions
impl !UnwindSafe for Extensions
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more