diff options
| author | bors <bors@rust-lang.org> | 2015-09-01 00:32:07 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-09-01 00:32:07 +0000 |
| commit | 7d78f2d333b323b0ce155152ae2e648450bffb01 (patch) | |
| tree | c6153facf0135190bbdc08a99bb48706ad96a5d5 /src/libstd | |
| parent | 2d3e8379c8314ac523197997dd573cfcbaa0a631 (diff) | |
| parent | a73d27f6d3e5b19edbc5eb0fc8fd6024d3aa8878 (diff) | |
| download | rust-7d78f2d333b323b0ce155152ae2e648450bffb01.tar.gz rust-7d78f2d333b323b0ce155152ae2e648450bffb01.zip | |
Auto merge of #28094 - apasel422:extend-hashmap, r=alexcrichton
It appears that these impls were left out of #25989 by mistake. r? @alexcrichton I'm not sure what the stability markers for these should be.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 24 | ||||
| -rw-r--r-- | src/libstd/collections/hash/set.rs | 38 |
2 files changed, 62 insertions, 0 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 1e5c012e7d8..4ad8fce8120 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1583,6 +1583,14 @@ impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S> } } +#[stable(feature = "hash_extend_copy", since = "1.4.0")] +impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S> + where K: Eq + Hash + Copy, V: Copy, S: HashState +{ + fn extend<T: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: T) { + self.extend(iter.into_iter().map(|(&key, &value)| (key, value))); + } +} /// `RandomState` is the default state for `HashMap` types. /// @@ -2347,4 +2355,20 @@ mod test_map { check(&m); } } + + #[test] + fn test_extend_ref() { + let mut a = HashMap::new(); + a.insert(1, "one"); + let mut b = HashMap::new(); + b.insert(2, "two"); + b.insert(3, "three"); + + a.extend(&b); + + assert_eq!(a.len(), 3); + assert_eq!(a[&1], "one"); + assert_eq!(a[&2], "two"); + assert_eq!(a[&3], "three"); + } } diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 1f19a72371c..7264b5827c4 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -654,6 +654,16 @@ impl<T, S> Extend<T> for HashSet<T, S> } } +#[stable(feature = "hash_extend_copy", since = "1.4.0")] +impl<'a, T, S> Extend<&'a T> for HashSet<T, S> + where T: 'a + Eq + Hash + Copy, + S: HashState, +{ + fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) { + self.extend(iter.into_iter().cloned()); + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<T, S> Default for HashSet<T, S> where T: Eq + Hash, @@ -1325,4 +1335,32 @@ mod test_set { assert_eq!(it.next(), Some(&Foo("a", 2))); assert_eq!(it.next(), None); } + + #[test] + fn test_extend_ref() { + let mut a = HashSet::new(); + a.insert(1); + + a.extend(&[2, 3, 4]); + + assert_eq!(a.len(), 4); + assert!(a.contains(&1)); + assert!(a.contains(&2)); + assert!(a.contains(&3)); + assert!(a.contains(&4)); + + let mut b = HashSet::new(); + b.insert(5); + b.insert(6); + + a.extend(&b); + + assert_eq!(a.len(), 6); + assert!(a.contains(&1)); + assert!(a.contains(&2)); + assert!(a.contains(&3)); + assert!(a.contains(&4)); + assert!(a.contains(&5)); + assert!(a.contains(&6)); + } } |
