Add Extensions::extend() and Extensions::is_empty() methods

This commit is contained in:
Nikolay Kim 2022-11-25 16:44:51 +01:00
parent f731a7800d
commit d947f7f08c
3 changed files with 39 additions and 3 deletions

View file

@ -1,6 +1,8 @@
# Changes
## [0.1.18] - 2022-xx-xx
## [0.1.18] - 2022-11-25
* Add Extensions::extend() and Extensions::is_empty() methods
* Add fmt::Debug impl to channel::Pool

View file

@ -1,6 +1,6 @@
[package]
name = "ntex-util"
version = "0.1.17"
version = "0.1.18"
authors = ["ntex contributors <team@ntex.rs>"]
description = "Utilities for ntex framework"
keywords = ["network", "framework", "async", "futures"]

View file

@ -1,4 +1,4 @@
use std::{any::Any, any::TypeId, fmt};
use std::{any::Any, any::TypeId, fmt, iter::Extend};
#[derive(Default)]
/// A type map of request extensions.
@ -51,6 +51,16 @@ impl Extensions {
.and_then(|boxed| boxed.downcast().ok().map(|boxed| *boxed))
}
/// Add all items from other `Extensions`
pub fn extend(&mut self, other: Extensions) {
self.map.extend(other.map.into_iter());
}
#[inline]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
/// Clear the `Extensions` of all inserted extensions.
#[inline]
pub fn clear(&mut self) {
@ -169,3 +179,27 @@ fn test_extensions() {
assert_eq!(extensions.get::<bool>(), None);
assert_eq!(extensions.get(), Some(&MyType(10)));
}
#[test]
fn test_extend() {
#[derive(Debug, PartialEq)]
struct MyType(i32);
let mut extensions2 = Extensions::new();
assert!(extensions2.is_empty());
extensions2.insert(MyType(10));
assert!(!extensions2.is_empty());
let mut extensions = Extensions::new();
extensions.insert(5i32);
extensions.extend(extensions2);
assert_eq!(extensions.get(), Some(&5i32));
assert_eq!(extensions.get_mut(), Some(&mut 5i32));
assert_eq!(extensions.remove::<i32>(), Some(5i32));
assert!(extensions.get::<i32>().is_none());
assert_eq!(extensions.get::<bool>(), None);
assert_eq!(extensions.get(), Some(&MyType(10)));
}