Skip to content

Commit

Permalink
Change Extensions::insert() method according doc #345
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 29, 2024
1 parent c9c85f2 commit 58c436c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions ntex-util/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Changes

## [1.1.0] - 2024-03-xx
## [1.1.0] - 2024-04-xx

* Added server worker's management utils
* Change Extensions::insert() method according doc #345

## [1.0.1] - 2024-01-19

Expand All @@ -12,7 +12,7 @@

* Release

## [1.0.0-b.1] - 2024-01-xx
## [1.0.0-b.1] - 2024-01-08

* Remove unnecessary 'static

Expand Down
2 changes: 1 addition & 1 deletion ntex-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-util"
version = "1.0.1"
version = "1.1.0"
authors = ["ntex contributors <team@ntex.rs>"]
description = "Utilities for ntex framework"
keywords = ["network", "framework", "async", "futures"]
Expand Down
7 changes: 5 additions & 2 deletions ntex-util/src/services/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ impl Extensions {
///
/// If a extension of this type already existed, it will
/// be returned.
pub fn insert<T: 'static>(&mut self, val: T) {
self.map.insert(TypeId::of::<T>(), Box::new(val));
pub fn insert<T: 'static>(&mut self, val: T) -> Option<T> {
self.map
.insert(TypeId::of::<T>(), Box::new(val))
.and_then(|item| item.downcast::<T>().map(|boxed| *boxed).ok())
}

/// Check if container contains entry
Expand Down Expand Up @@ -89,6 +91,7 @@ fn test_remove() {

map.insert::<i8>(123);
assert!(map.get::<i8>().is_some());
assert_eq!(map.insert::<i8>(10), Some(123));

map.remove::<i8>();
assert!(map.get::<i8>().is_none());
Expand Down

0 comments on commit 58c436c

Please sign in to comment.