Skip to content

Commit c3f6cf9

Browse files
Philippe-Choletphimuemue
authored andcommitted
Specialize MapForGrouping::fold
`MapForGrouping` is only internally used by `GroupingMapBy` and it only calls `for_each` on it (which in turn rely on `fold`). So I allow the clippy lint `missing_trait_methods` because specialize other methods is simply useless. I could replace `next` body by `unreachable!()` and it would still work fine. Anyway, it will disappear from test coverage now that we have one. I previously wandered how to test and benchmark this. The current tests will test this. And I guess I could benchmark this `fold` specialization through some `into_grouping_map_by` benchmark but I simply don't think it's worth the time: no doubt `adapted_iterator.map.fold` is faster than the default `fold` calling `next` repeatedly. Note that all `GroupingMapBy` methods ultimately rely on this `fold` so this specialization will improve performance for all its methods.
1 parent 3e47a02 commit c3f6cf9

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/grouping_map.rs

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl<I, F> MapForGrouping<I, F> {
2222
}
2323
}
2424

25+
#[allow(clippy::missing_trait_methods)]
2526
impl<K, V, I, F> Iterator for MapForGrouping<I, F>
2627
where
2728
I: Iterator<Item = V>,
@@ -32,6 +33,14 @@ where
3233
fn next(&mut self) -> Option<Self::Item> {
3334
self.0.next().map(|val| ((self.1)(&val), val))
3435
}
36+
37+
fn fold<B, G>(self, init: B, f: G) -> B
38+
where
39+
G: FnMut(B, Self::Item) -> B,
40+
{
41+
let mut key_mapper = self.1;
42+
self.0.map(|val| (key_mapper(&val), val)).fold(init, f)
43+
}
3544
}
3645

3746
/// Creates a new `GroupingMap` from `iter`

0 commit comments

Comments
 (0)