chore: simplify get_or_insert

This commit is contained in:
Rob Ede 2025-02-09 21:51:33 +00:00
parent 4aab56dc01
commit c895b58220
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
1 changed files with 2 additions and 6 deletions

View File

@ -119,11 +119,7 @@ impl Extensions {
/// assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1,2]));
/// ```
pub fn get_or_insert<T: 'static>(&mut self, value: T) -> &mut T {
self.map
.entry(TypeId::of::<T>())
.or_insert(Box::new(value))
.downcast_mut()
.expect("extensions map to always contain value T")
self.get_or_insert_with(|| value)
}
/// 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>())
.or_insert_with(|| Box::new(default()))
.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.