1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
mod aggregate_mut;
mod into_aggregate;
pub use aggregate_mut::*;
pub use into_aggregate::*;
use crate::{
aggregate::{AggregateOp, Group, OccupiedGroup, VacantGroup},
assert_collector,
};
/// A group map.
pub trait GroupMap {
/// The key of each group.
type Key;
/// The value of each group.
type Value;
/// An existing group.
type Occupied<'a>: OccupiedGroup<Key = Self::Key, Value = Self::Value>
where
Self: 'a;
/// A group not existing yet.
type Vacant<'a>: VacantGroup<Key = Self::Key, Value = Self::Value>
where
Self: 'a;
/// Returns a [`Group`] for the given `key`, representing either an
/// existing group or a new group that can be created.
fn group(&mut self, key: Self::Key) -> Group<Self::Occupied<'_>, Self::Vacant<'_>>;
/// Creates a [`Collector`] that aggregates items into groups. This method takes
/// the ownership of the map.
///
/// This collects `(K, V)`s. Items that have the same key `K` go to the same group, and the way
/// all values `V` of the same key are grouped is determined by the provided `op`.
///
/// # Examples
///
/// [`Collector`]: crate::collector::Collector
fn into_aggregate<Op>(self, op: Op) -> IntoAggregate<Self, Op>
where
Self: Sized,
Op: AggregateOp<Key = Self::Key, Value = Self::Value>,
{
assert_collector(IntoAggregate::new(self, op))
}
/// Creates a [`Collector`] that aggregates items into groups. This method takes
/// a mutable reference to the map.
///
/// This collects `(K, V)`s. Items that have the same key `K` go to the same group, and the way
/// all values `V` of the same key are grouped is determined by the provided `op`.
///
/// # Examples
///
/// [`Collector`]: crate::collector::Collector
fn aggregate_mut<Op>(&mut self, op: Op) -> AggregateMut<'_, Self, Op>
where
Self: Sized,
Op: AggregateOp<Key = Self::Key, Value = Self::Value>,
{
assert_collector(AggregateMut::new(self, op))
}
}