]> git.lizzy.rs Git - rust.git/commitdiff
std: add a Clone impl for HashSet.
authorHuon Wilson <dbau.pp+github@gmail.com>
Fri, 9 Aug 2013 14:44:35 +0000 (00:44 +1000)
committerHuon Wilson <dbau.pp+github@gmail.com>
Fri, 9 Aug 2013 14:44:35 +0000 (00:44 +1000)
src/libstd/hashmap.rs

index 84cba254dcf23599258f50f64fdbfdc1637cff30..623d395f60d676e25f17febdab9748d2135df00f 100644 (file)
@@ -745,6 +745,14 @@ pub fn union_iter<'a>(&'a self, other: &'a HashSet<T>)
 
 }
 
+impl<T:Hash + Eq + Clone> Clone for HashSet<T> {
+    fn clone(&self) -> HashSet<T> {
+        HashSet {
+            map: self.map.clone()
+        }
+    }
+}
+
 impl<K: Eq + Hash, T: Iterator<K>> FromIterator<K, T> for HashSet<K> {
     fn from_iterator(iter: &mut T) -> HashSet<K> {
         let (lower, _) = iter.size_hint();
@@ -1190,4 +1198,22 @@ fn test_consume() {
         let v = hs.consume().collect::<~[char]>();
         assert!(['a', 'b'] == v || ['b', 'a'] == v);
     }
+
+    #[test]
+    fn test_eq() {
+        let mut s1 = HashSet::new();
+        s1.insert(1);
+        s1.insert(2);
+        s1.insert(3);
+
+        let mut s2 = HashSet::new();
+        s2.insert(1);
+        s2.insert(2);
+
+        assert!(s1 != s2);
+
+        s2.insert(3);
+
+        assert_eq!(s1, s2);
+    }
 }