Skip to content

Commit

Permalink
Add Extensions::extend (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte authored Apr 26, 2022
1 parent 4fb67bc commit f4cff03
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 f4cff03

Please sign in to comment.