diff --git a/src/default_btree.rs b/src/default_btree.rs index 33e4b5a..563511d 100644 --- a/src/default_btree.rs +++ b/src/default_btree.rs @@ -790,6 +790,46 @@ where } } +impl Iterator for DefaultBTreeMapIter +where + K: Eq + Ord, + V: Default, +{ + type Item = (K, V); + fn next(&mut self) -> Option { + match self.keys.pop() { + Some(key) => { + let val = self._defaultbtree.remove(&key); + Option::Some((key, val)) + } + None => Option::None, + } + } +} + +pub struct DefaultBTreeMapIter +where + K: Eq + Ord, + V: Default, +{ + _defaultbtree: DefaultBTreeMap, + keys: Vec, +} + +impl FromIterator<(K, V)> for DefaultBTreeMap +where + K: Ord, + V: Default, +{ + fn from_iter>(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. /// diff --git a/tests/test_btree.rs b/tests/test_btree.rs index aca7b74..51b9d1b 100644 --- a/tests/test_btree.rs +++ b/tests/test_btree.rs @@ -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 = data.iter().cloned().collect(); + + let correct_map: DefaultBTreeMap = defaultbtreemap!((1, 1), (2, 2), (3, 3), (4, 4)); + + assert_eq!(map, correct_map); +}