File tree Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -256,16 +256,7 @@ where
256
256
where
257
257
C : Default + Extend < V > ,
258
258
{
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 ( ) )
269
260
}
270
261
271
262
/// Groups elements from the `GroupingMap` source by key and finds the maximum of each group.
@@ -667,4 +658,20 @@ where
667
658
map,
668
659
)
669
660
}
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
+ }
670
677
}
You can’t perform that action at this time.
0 commit comments