Skip to content

Commit d4b3723

Browse files
New GroupingMap::collect_in
1 parent e256f07 commit d4b3723

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/grouping_map.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,7 @@ where
256256
where
257257
C: Default + Extend<V>,
258258
{
259-
let mut destination_map = HashMap::new();
260-
261-
self.iter.for_each(|(key, val)| {
262-
destination_map
263-
.entry(key)
264-
.or_insert_with(C::default)
265-
.extend(Some(val));
266-
});
267-
268-
destination_map
259+
self.collect_in(HashMap::new())
269260
}
270261

271262
/// Groups elements from the `GroupingMap` source by key and finds the maximum of each group.
@@ -667,4 +658,20 @@ where
667658
map,
668659
)
669660
}
661+
662+
/// Apply [`collect`](Self::collect) with a provided empty map
663+
/// (`BTreeMap` or `HashMap` with any hasher).
664+
pub fn collect_in<C, M>(self, mut map: M) -> M
665+
where
666+
C: Default + Extend<V>,
667+
M: Map<Key = K, Value = C>,
668+
{
669+
debug_assert!(map.is_empty());
670+
671+
self.iter.for_each(|(key, val)| {
672+
map.entry_or_default(key).extend(Some(val));
673+
});
674+
675+
map
676+
}
670677
}

0 commit comments

Comments
 (0)