Skip to content

Commit

Permalink
Added FromIterator for BTreeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchellBerend authored and Maneren committed Feb 5, 2024
1 parent 153c89e commit baf108a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/default_btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,20 @@ where
}
}

impl<K, V> FromIterator<(K, V)> for DefaultBTreeMap<K, V>
where
K: Ord,
V: Default,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let mut map = DefaultBTreeMap::default();
for (k, v) in iter {
let _ = map.insert(k, v);
}
map
}
}

#[macro_export]
/// A quick way to instantiate a BTreeMap.
///
Expand Down
10 changes: 10 additions & 0 deletions tests/test_btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,3 +700,13 @@ fn macro_test_slight_change_btree() {
assert_eq!(map, _map);
assert_eq!(map1, _map1);
}

#[test]
fn from_iter_btreemap() {
let data = [(1, 1), (2, 2), (3, 3), (4, 4)];
let map: DefaultBTreeMap<i8, u8> = data.iter().cloned().collect();

let correct_map: DefaultBTreeMap<i8, u8> = defaultbtreemap!((1, 1), (2, 2), (3, 3), (4, 4));

assert_eq!(map, correct_map);
}

0 comments on commit baf108a

Please sign in to comment.