Skip to content

Commit 7d78f2d

Browse files
committed
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.
2 parents 2d3e837 + a73d27f commit 7d78f2d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,14 @@ impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S>
15831583
}
15841584
}
15851585

1586+
#[stable(feature = "hash_extend_copy", since = "1.4.0")]
1587+
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S>
1588+
where K: Eq + Hash + Copy, V: Copy, S: HashState
1589+
{
1590+
fn extend<T: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: T) {
1591+
self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
1592+
}
1593+
}
15861594

15871595
/// `RandomState` is the default state for `HashMap` types.
15881596
///
@@ -2347,4 +2355,20 @@ mod test_map {
23472355
check(&m);
23482356
}
23492357
}
2358+
2359+
#[test]
2360+
fn test_extend_ref() {
2361+
let mut a = HashMap::new();
2362+
a.insert(1, "one");
2363+
let mut b = HashMap::new();
2364+
b.insert(2, "two");
2365+
b.insert(3, "three");
2366+
2367+
a.extend(&b);
2368+
2369+
assert_eq!(a.len(), 3);
2370+
assert_eq!(a[&1], "one");
2371+
assert_eq!(a[&2], "two");
2372+
assert_eq!(a[&3], "three");
2373+
}
23502374
}

src/libstd/collections/hash/set.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,16 @@ impl<T, S> Extend<T> for HashSet<T, S>
654654
}
655655
}
656656

657+
#[stable(feature = "hash_extend_copy", since = "1.4.0")]
658+
impl<'a, T, S> Extend<&'a T> for HashSet<T, S>
659+
where T: 'a + Eq + Hash + Copy,
660+
S: HashState,
661+
{
662+
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
663+
self.extend(iter.into_iter().cloned());
664+
}
665+
}
666+
657667
#[stable(feature = "rust1", since = "1.0.0")]
658668
impl<T, S> Default for HashSet<T, S>
659669
where T: Eq + Hash,
@@ -1325,4 +1335,32 @@ mod test_set {
13251335
assert_eq!(it.next(), Some(&Foo("a", 2)));
13261336
assert_eq!(it.next(), None);
13271337
}
1338+
1339+
#[test]
1340+
fn test_extend_ref() {
1341+
let mut a = HashSet::new();
1342+
a.insert(1);
1343+
1344+
a.extend(&[2, 3, 4]);
1345+
1346+
assert_eq!(a.len(), 4);
1347+
assert!(a.contains(&1));
1348+
assert!(a.contains(&2));
1349+
assert!(a.contains(&3));
1350+
assert!(a.contains(&4));
1351+
1352+
let mut b = HashSet::new();
1353+
b.insert(5);
1354+
b.insert(6);
1355+
1356+
a.extend(&b);
1357+
1358+
assert_eq!(a.len(), 6);
1359+
assert!(a.contains(&1));
1360+
assert!(a.contains(&2));
1361+
assert!(a.contains(&3));
1362+
assert!(a.contains(&4));
1363+
assert!(a.contains(&5));
1364+
assert!(a.contains(&6));
1365+
}
13281366
}

0 commit comments

Comments
 (0)