Skip to content

Commit 21be768

Browse files
committed
type MapForGrouping<...> = MapSpecialCase<...>
1 parent c3f6cf9 commit 21be768

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

src/adaptors/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::marker::PhantomData;
44
#[derive(Clone, Debug)]
55
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
66
pub struct MapSpecialCase<I, F> {
7-
iter: I,
8-
f: F,
7+
pub(crate) iter: I,
8+
pub(crate) f: F,
99
}
1010

1111
pub trait MapSpecialCaseFn<T> {

src/adaptors/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! except according to those terms.
66
77
mod coalesce;
8-
mod map;
8+
pub(crate) mod map;
99
mod multi_product;
1010
pub use self::coalesce::*;
1111
#[allow(deprecated)]

src/grouping_map.rs

+16-28
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,33 @@
11
#![cfg(feature = "use_std")]
22

3-
use crate::MinMaxResult;
3+
use crate::{
4+
adaptors::map::{MapSpecialCase, MapSpecialCaseFn},
5+
MinMaxResult,
6+
};
47
use std::cmp::Ordering;
58
use std::collections::HashMap;
6-
use std::fmt;
79
use std::hash::Hash;
810
use std::iter::Iterator;
911
use std::ops::{Add, Mul};
1012

1113
/// A wrapper to allow for an easy [`into_grouping_map_by`](crate::Itertools::into_grouping_map_by)
12-
#[derive(Clone)]
13-
pub struct MapForGrouping<I, F>(I, F);
14+
pub type MapForGrouping<I, F> = MapSpecialCase<I, GroupingMapFn<F>>;
1415

15-
impl<I: fmt::Debug, F> fmt::Debug for MapForGrouping<I, F> {
16-
debug_fmt_fields!(MapForGrouping, 0);
17-
}
16+
pub struct GroupingMapFn<F>(F);
1817

19-
impl<I, F> MapForGrouping<I, F> {
20-
pub(crate) fn new(iter: I, key_mapper: F) -> Self {
21-
Self(iter, key_mapper)
18+
impl<V, K, F: FnMut(&V) -> K> MapSpecialCaseFn<V> for GroupingMapFn<F> {
19+
type Out = (K, V);
20+
fn call(&mut self, v: V) -> Self::Out {
21+
((self.0)(&v), v)
2222
}
2323
}
2424

25-
#[allow(clippy::missing_trait_methods)]
26-
impl<K, V, I, F> Iterator for MapForGrouping<I, F>
27-
where
28-
I: Iterator<Item = V>,
29-
K: Hash + Eq,
30-
F: FnMut(&V) -> K,
31-
{
32-
type Item = (K, V);
33-
fn next(&mut self) -> Option<Self::Item> {
34-
self.0.next().map(|val| ((self.1)(&val), val))
35-
}
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)
25+
impl<K, I: Iterator, F: FnMut(&I::Item) -> K> MapForGrouping<I, F> {
26+
pub(crate) fn new(iter: I, key_mapper: F) -> Self {
27+
MapSpecialCase {
28+
iter,
29+
f: GroupingMapFn(key_mapper),
30+
}
4331
}
4432
}
4533

0 commit comments

Comments
 (0)