From ae8c196702c3ce0bb7ef043ddbc71878d70be345 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 4 Feb 2021 00:01:13 -0800 Subject: [PATCH] Implement `From>` for `HashSet`. Closes #219. --- src/set.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/set.rs b/src/set.rs index 83eccbd2e7..8ceaf1200b 100644 --- a/src/set.rs +++ b/src/set.rs @@ -1107,6 +1107,15 @@ where } } +impl From> for HashSet +where + A: Allocator + Clone, +{ + fn from(map: HashMap) -> Self { + Self { map } + } +} + impl FromIterator for HashSet where T: Eq + Hash, @@ -2038,6 +2047,23 @@ mod test_set { assert_eq!(i, expected.len()); } + #[test] + fn test_from_map() { + let mut a = crate::HashMap::new(); + a.insert(1, ()); + a.insert(2, ()); + a.insert(3, ()); + a.insert(4, ()); + + let a: HashSet<_> = a.into(); + + assert_eq!(a.len(), 4); + assert!(a.contains(&1)); + assert!(a.contains(&2)); + assert!(a.contains(&3)); + assert!(a.contains(&4)); + } + #[test] fn test_from_iter() { let xs = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9];