forked from hydro-project/hydroflow
/
map.rs
103 lines (91 loc) · 2.51 KB
/
map.rs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use super::{BaseSurface, PullSurface, PushSurface, PushSurfaceReversed};
use std::marker::PhantomData;
use crate::builder::build::pull_map::MapPullBuild;
use crate::builder::build::push_map::MapPushBuild;
pub struct MapSurface<Prev, Func>
where
Prev: BaseSurface,
{
prev: Prev,
func: Func,
}
impl<Prev, Func, Out> MapSurface<Prev, Func>
where
Prev: BaseSurface,
Func: FnMut(Prev::ItemOut) -> Out,
{
pub fn new(prev: Prev, func: Func) -> Self {
Self { prev, func }
}
}
impl<Prev, Func, Out> BaseSurface for MapSurface<Prev, Func>
where
Prev: BaseSurface,
Func: FnMut(Prev::ItemOut) -> Out,
{
type ItemOut = Out;
}
impl<Prev, Func, Out> PullSurface for MapSurface<Prev, Func>
where
Prev: PullSurface,
Func: FnMut(Prev::ItemOut) -> Out,
{
type InputHandoffs = Prev::InputHandoffs;
type Build = MapPullBuild<Prev::Build, Func>;
fn into_parts(self) -> (Self::InputHandoffs, Self::Build) {
let (connect, build) = self.prev.into_parts();
let build = MapPullBuild::new(build, self.func);
(connect, build)
}
}
impl<Prev, Func, Out> PushSurface for MapSurface<Prev, Func>
where
Prev: PushSurface,
Func: FnMut(Prev::ItemOut) -> Out,
{
type Output<Next> = Prev::Output<MapPushSurfaceReversed<Next, Func, Prev::ItemOut>>
where
Next: PushSurfaceReversed<ItemIn = Self::ItemOut>;
fn push_to<Next>(self, next: Next) -> Self::Output<Next>
where
Next: PushSurfaceReversed<ItemIn = Self::ItemOut>,
{
self.prev
.push_to(MapPushSurfaceReversed::new(next, self.func))
}
}
pub struct MapPushSurfaceReversed<Next, Func, In>
where
Next: PushSurfaceReversed,
{
next: Next,
func: Func,
_phantom: PhantomData<fn(In)>,
}
impl<Next, Func, In> MapPushSurfaceReversed<Next, Func, In>
where
Next: PushSurfaceReversed,
Func: FnMut(In) -> Next::ItemIn,
{
pub fn new(next: Next, func: Func) -> Self {
Self {
next,
func,
_phantom: PhantomData,
}
}
}
impl<Next, Func, In> PushSurfaceReversed for MapPushSurfaceReversed<Next, Func, In>
where
Next: PushSurfaceReversed,
Func: FnMut(In) -> Next::ItemIn,
{
type ItemIn = In;
type OutputHandoffs = Next::OutputHandoffs;
type Build = MapPushBuild<Next::Build, Func, In>;
fn into_parts(self) -> (Self::OutputHandoffs, Self::Build) {
let (connect, build) = self.next.into_parts();
let build = MapPushBuild::new(build, self.func);
(connect, build)
}
}