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
68
69
70
71
72
pub trait Push<T> {
fn push(&mut self, element: T);
}
impl<T> Push<T> for Vec<T> {
fn push(&mut self, element: T) {
Vec::push(self, element)
}
}
impl<T, P> Push<T> for &mut P
where
P: Push<T>,
{
fn push(&mut self, element: T) {
P::push(self, element)
}
}
mod private {
pub trait Sealed<T> {}
impl<P, T> Sealed<T> for P where P: super::Push<T> {}
}
pub trait PushExt<T>: Push<T> + private::Sealed<T> {
fn gmap<B, F>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: FnMut(B) -> T;
}
impl<P, T> PushExt<T> for P
where
P: Push<T>,
{
fn gmap<B, F>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: FnMut(B) -> T,
{
Map { pushable: self, f }
}
}
pub struct Map<P, F> {
pushable: P,
f: F,
}
impl<A, B, P, F> Push<B> for Map<P, F>
where
P: Push<A>,
F: FnMut(B) -> A,
{
fn push(&mut self, element: B) {
self.pushable.push((self.f)(element))
}
}