mirror of https://github.com/fafhrd91/actix-web
add get_or_insert and get_or_insert_with for Extensions
This commit is contained in:
parent
91e29c0ce4
commit
d57d5f3461
|
@ -104,6 +104,22 @@ impl Extensions {
|
|||
.and_then(|boxed| boxed.downcast_mut())
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
pub fn get_or_insert_with<T: 'static, F: FnOnce() -> T>(&mut self, default: F) -> &mut T {
|
||||
self.map
|
||||
.entry(TypeId::of::<T>())
|
||||
.or_insert_with(|| Box::new(default()))
|
||||
.downcast_mut()
|
||||
.expect("extensions map to always contain value T")
|
||||
}
|
||||
|
||||
/// Remove an item from the map of a given type.
|
||||
///
|
||||
/// If an item of this type was already stored, it will be returned.
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
- On Windows, an error is now returned from `HttpServer::bind()` (or TLS variants) when binding to a socket that's already in use.
|
||||
- Update `brotli` dependency to `7`.
|
||||
- Minimum supported Rust version (MSRV) is now 1.75.
|
||||
- Added `Extensions::get_or_insert` and `Extensions::get_or_insert_with`
|
||||
|
||||
## 4.9.0
|
||||
|
||||
|
|
Loading…
Reference in New Issue