Skip to content

Commit b5b5be8

Browse files
committed
Auto merge of #235 - smmalis37:patch-1, r=Amanieu
Implement `From<HashMap<T, ()>>` for `HashSet<T>`. Closes #219.
2 parents 91afba3 + ae8c196 commit b5b5be8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/set.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,15 @@ where
11071107
}
11081108
}
11091109

1110+
impl<T, S, A> From<HashMap<T, (), S, A>> for HashSet<T, S, A>
1111+
where
1112+
A: Allocator + Clone,
1113+
{
1114+
fn from(map: HashMap<T, (), S, A>) -> Self {
1115+
Self { map }
1116+
}
1117+
}
1118+
11101119
impl<T, S, A> FromIterator<T> for HashSet<T, S, A>
11111120
where
11121121
T: Eq + Hash,
@@ -2038,6 +2047,23 @@ mod test_set {
20382047
assert_eq!(i, expected.len());
20392048
}
20402049

2050+
#[test]
2051+
fn test_from_map() {
2052+
let mut a = crate::HashMap::new();
2053+
a.insert(1, ());
2054+
a.insert(2, ());
2055+
a.insert(3, ());
2056+
a.insert(4, ());
2057+
2058+
let a: HashSet<_> = a.into();
2059+
2060+
assert_eq!(a.len(), 4);
2061+
assert!(a.contains(&1));
2062+
assert!(a.contains(&2));
2063+
assert!(a.contains(&3));
2064+
assert!(a.contains(&4));
2065+
}
2066+
20412067
#[test]
20422068
fn test_from_iter() {
20432069
let xs = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9];

0 commit comments

Comments
 (0)