Skip to content

Commit f8c7d19

Browse files
New GroupingMap::collect_in
1 parent 742a678 commit f8c7d19

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/grouping_map.rs

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

272263
/// Groups elements from the `GroupingMap` source by key and finds the maximum of each group.
@@ -663,4 +654,17 @@ where
663654
map,
664655
)
665656
}
657+
658+
/// Apply [`collect`](Self::collect) with a provided map.
659+
pub fn collect_in<C, M>(self, mut map: M) -> M
660+
where
661+
C: Default + Extend<V>,
662+
M: Map<Key = K, Value = C>,
663+
{
664+
self.iter.for_each(|(key, val)| {
665+
map.entry_or_default(key).extend(Some(val));
666+
});
667+
668+
map
669+
}
666670
}

0 commit comments

Comments
 (0)