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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use std::fmt;
use std::iter::FromIterator;
mod kleisli;
pub use kleisli::Kleisli;
mod pipe;
pub use pipe::*;
enum Void {}
pub enum ConduitM<'a, I, O, A> {
Pure(Box<A>),
Await(Kleisli<'a, Option<I>, I, O, A>),
Yield(Box<O>, Kleisli<'a, (), I, O, A>)
}
pub type Source<'a, O> = ConduitM<'a, (), O, ()>;
impl<'a, O> ConduitM<'a, (), O, ()> {
pub fn to_producer<I>(self) -> ConduitM<'a, I, O, ()> where O: 'static {
match self {
ConduitM::Pure(x) => ConduitM::Pure(x),
ConduitM::Await(k) => k.run(Some(())).to_producer(),
ConduitM::Yield(o, k) => ConduitM::Yield(o, Kleisli::new().append(move |_| {
k.run(()).to_producer()
}))
}
}
pub fn connect<A>(mut self, mut sink: Sink<'a, O, A>) -> A where O: 'static {
loop {
let (next_src, next_sink) = match sink {
ConduitM::Pure(b_box) => {
return *b_box;
},
ConduitM::Await(k_sink) => {
match self {
ConduitM::Pure(x) => {
(ConduitM::Pure(x), k_sink.run(None))
},
ConduitM::Await(k_src) => {
(k_src.run(Some(())), ConduitM::Await(k_sink))
},
ConduitM::Yield(a_box, k_src) => {
(k_src.run(()), k_sink.run(Some(*a_box)))
}
}
},
ConduitM::Yield(_, _) => unreachable!()
};
self = next_src;
sink = next_sink;
}
}
}
pub type Conduit<'a, I, O> = ConduitM<'a, I, O, ()>;
impl<'a, I, O> ConduitM<'a, I, O, ()> {
pub fn fuse<P, A>(self, other: ConduitM<'a, O, P, A>) -> ConduitM<'a, I, P, A>
where I: 'static, O: 'static, P: 'static, A: 'a {
match other {
ConduitM::Pure(r) => ConduitM::Pure(r),
ConduitM::Yield(c, k) => ConduitM::Yield(c, Kleisli::new().append(move |_| {
self.fuse(k.run(()))
})),
ConduitM::Await(k_right) => match self {
ConduitM::Pure(_) => ConduitM::fuse(().into(), k_right.run(None)),
ConduitM::Yield(b, k_left) => k_left.run(()).fuse(k_right.run(Some(*b))),
ConduitM::Await(k_left) => ConduitM::Await(Kleisli::new().append(move |a| {
k_left.run(a).fuse(ConduitM::Await(k_right))
}))
}
}
}
pub fn transform<F>(f: F) -> Self where F: 'a + Fn(I) -> O {
consume().and_then(|io| match io {
None => ().into(),
Some(o) => produce(f(o)).and_then(move |_| Conduit::transform(f))
})
}
}
impl<'a, I, O: 'a> FromIterator<O> for ConduitM<'a, I, O, ()> {
fn from_iter<T: IntoIterator<Item=O>>(iterator: T) -> Self
where T::Item: 'a {
let mut conduit: Self = ().into();
for x in iterator {
conduit = conduit.and_then(move |_| produce(x));
}
conduit
}
}
pub type Sink<'a, I, A> = ConduitM<'a, I, Void, A>;
impl<'a, I, A> ConduitM<'a, I, Void, A> {
pub fn to_consumer<O>(self) -> ConduitM<'a, I, O, A> {
unsafe { std::mem::transmute(self) }
}
fn sink<F>(a: A, f: F) -> Self
where A: 'a, F: 'a + Fn(A, I) -> Result<A, A> {
consume().and_then(|io| {
match io {
None => a.into(),
Some(i) => match f(a, i) {
Ok(a) => Self::sink(a, f),
Err(a) => a.into()
}
}
})
}
pub fn fold<F>(a: A, f: F) -> Self
where A: 'a, F: 'a + Fn(A, I) -> A {
Self::sink(a, move |a, i| Ok(f(a, i)))
}
}
impl<'a, I, O, A> ConduitM<'a, I, O, A> {
fn and_then_boxed<B, F>(self, js: F) -> ConduitM<'a, I, O, B>
where F: 'a + FnOnce(Box<A>) -> ConduitM<'a, I, O, B> {
match self {
ConduitM::Pure(a) => js(a),
ConduitM::Await(is) => ConduitM::Await(kleisli::append_boxed(is, js)),
ConduitM::Yield(o, is) => ConduitM::Yield(o, kleisli::append_boxed(is, js))
}
}
pub fn and_then<B, F>(self, js: F) -> ConduitM<'a, I, O, B>
where F: 'a + FnOnce(A) -> ConduitM<'a, I, O, B> {
match self {
ConduitM::Pure(a) => js(*a),
ConduitM::Await(is) => ConduitM::Await(is.append(js)),
ConduitM::Yield(o, is) => ConduitM::Yield(o, is.append(js))
}
}
pub fn map<B, F>(self, f: F) -> ConduitM<'a, I, O, B>
where F: 'a + FnOnce(A) -> B {
self.and_then(move |a| f(a).into())
}
}
impl<'a, I, O, A: PartialEq> PartialEq for ConduitM<'a, I, O, A> {
fn eq(&self, other: &ConduitM<'a, I, O, A>) -> bool {
match (self, other) {
(&ConduitM::Pure(ref a), &ConduitM::Pure(ref b)) => a == b,
_ => false
}
}
}
impl<'a, I, O, A: fmt::Debug> fmt::Debug for ConduitM<'a, I, O, A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&ConduitM::Pure(ref a) => write!(f, "Pure({:?})", a),
&ConduitM::Await(_) => write!(f, "Await(..)"),
&ConduitM::Yield(_, _) => write!(f, "Yield(..)")
}
}
}
impl<'a, I, O, A> From<A> for ConduitM<'a, I, O, A> {
fn from(a: A) -> ConduitM<'a, I, O, A> {
ConduitM::Pure(Box::new(a))
}
}
pub fn consume<'a, I, O>() -> ConduitM<'a, I, O, Option<I>> {
ConduitM::Await(Kleisli::new())
}
pub fn produce<'a, I, O>(o: O) -> ConduitM<'a, I, O, ()> {
ConduitM::Yield(Box::new(o), Kleisli::new())
}