mirror of https://github.com/fafhrd91/actix-web
chore: simplify get_or_insert
This commit is contained in:
parent
4aab56dc01
commit
c895b58220
|
@ -119,11 +119,7 @@ impl Extensions {
|
||||||
/// assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1,2]));
|
/// assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1,2]));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn get_or_insert<T: 'static>(&mut self, value: T) -> &mut T {
|
pub fn get_or_insert<T: 'static>(&mut self, value: T) -> &mut T {
|
||||||
self.map
|
self.get_or_insert_with(|| value)
|
||||||
.entry(TypeId::of::<T>())
|
|
||||||
.or_insert(Box::new(value))
|
|
||||||
.downcast_mut()
|
|
||||||
.expect("extensions map to always contain value T")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Inserts a value computed from `f` into the extensions if the given `value` is not present,
|
/// Inserts a value computed from `f` into the extensions if the given `value` is not present,
|
||||||
|
@ -145,7 +141,7 @@ impl Extensions {
|
||||||
.entry(TypeId::of::<T>())
|
.entry(TypeId::of::<T>())
|
||||||
.or_insert_with(|| Box::new(default()))
|
.or_insert_with(|| Box::new(default()))
|
||||||
.downcast_mut()
|
.downcast_mut()
|
||||||
.expect("extensions map to always contain value T")
|
.expect("extensions map should now contain a T value")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove an item from the map of a given type.
|
/// Remove an item from the map of a given type.
|
||||||
|
|
Loading…
Reference in New Issue