]> git.lizzy.rs Git - rust.git/commitdiff
Implement RFC 839 for `{HashMap, HashSet}`
authorAndrew Paseltiner <apaseltiner@gmail.com>
Sun, 30 Aug 2015 01:06:11 +0000 (21:06 -0400)
committerAndrew Paseltiner <apaseltiner@gmail.com>
Mon, 31 Aug 2015 17:57:59 +0000 (13:57 -0400)
It appears that these impls were left out of #25989 by mistake.

src/libstd/collections/hash/map.rs
src/libstd/collections/hash/set.rs

index 1e5c012e7d8084880726a88342bdc1cdc2ea489b..4ad8fce8120aa2318db78c476b2aad8c3f46bf03 100644 (file)
@@ -1583,6 +1583,14 @@ fn extend<T: IntoIterator<Item=(K, V)>>(&mut self, iter: T) {
     }
 }
 
+#[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 @@ fn check(m: &HashMap<isize, ()>) {
             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");
+    }
 }
index 1f19a72371c9adc51a08b0dbe3dd07ca2ed96b51..7264b5827c4fb0d46d9ff6719824e0ebda671614 100644 (file)
@@ -654,6 +654,16 @@ fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) {
     }
 }
 
+#[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 @@ fn hash<H: hash::Hasher>(&self, h: &mut H) {
         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));
+    }
 }