Skip to content

Commit

Permalink
Add Extensions::extend
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Apr 19, 2022
1 parent d8b35db commit 81a6403
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Add `extend()` method to `Extension`.

# 0.2.6 (December 30, 2021)

* Upgrade internal `itoa` dependency to 1.0.
Expand Down
33 changes: 33 additions & 0 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,39 @@ impl Extensions {
.as_ref()
.map_or(0, |map| map.len())
}

/// Extends `self` with another `Extensions`.
///
/// If an instance of a specific type exists in both, the one in `self` is overwritten with the
/// one from `other`.
///
/// # Example
///
/// ```
/// # use http::Extensions;
/// let mut ext_a = Extensions::new();
/// ext_a.insert(8u8);
/// ext_a.insert(16u16);
///
/// let mut ext_b = Extensions::new();
/// ext_b.insert(4u8);
/// ext_b.insert("hello");
///
/// ext_a.extend(ext_b);
/// assert_eq!(ext_a.len(), 3);
/// assert_eq!(ext_a.get::<u8>(), Some(&4u8));
/// assert_eq!(ext_a.get::<u16>(), Some(&16u16));
/// assert_eq!(ext_a.get::<&'static str>().copied(), Some("hello"));
/// ```
pub fn extend(&mut self, other: Self) {
if let Some(other) = other.map {
if let Some(map) = &mut self.map {
map.extend(*other);
} else {
self.map = Some(other);
}
}
}
}

impl fmt::Debug for Extensions {
Expand Down

0 comments on commit 81a6403

Please sign in to comment.