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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use crate::Pipe;
use std::marker::PhantomData;

/// A pipe that connects two other pipes together.
///
/// The input item of this connector is the input item of `P0` and its output item is the output item of `P1`. It calculates the output item of `P0` and feeds it directly into `P1`.
///
/// Obviously, the output item of `P0` has to match the input item of `P1`.
///
/// For more information, please see [the documentation of the `connect` method](trait.Pipe.html#method.connect).
pub struct Connector<P0, P1>
where
    P0: Pipe,
    P1: Pipe<InputItem = P0::OutputItem>,
{
    pipe0: P0,
    pipe1: P1,
}

impl<P0, P1> Connector<P0, P1>
where
    P0: Pipe,
    P1: Pipe<InputItem = P0::OutputItem>,
{
    /// Create a new connector with the two pipes.
    pub fn new(pipe0: P0, pipe1: P1) -> Self {
        Connector { pipe0, pipe1 }
    }
}

impl<P0, P1> Pipe for Connector<P0, P1>
where
    P0: Pipe,
    P1: Pipe<InputItem = P0::OutputItem>,
{
    type InputItem = P0::InputItem;
    type OutputItem = P1::OutputItem;

    fn next(&mut self, input: Self::InputItem) -> Self::OutputItem {
        self.pipe1.next(self.pipe0.next(input))
    }

    fn reset(&mut self) {
        self.pipe0.reset();
        self.pipe1.reset();
    }
}

/// A pipe that bypasses the effects of an internal pipe.
///
/// For more information, please see [the documentation of the `bypass` method](trait.Pipe.html#method.bypass).
pub struct Bypass<P>
where
    P: Pipe,
    P::InputItem: Clone,
{
    pipe: P,
}

impl<P> Bypass<P>
where
    P: Pipe,
    P::InputItem: Clone,
{
    /// Create a new bypassed pipe.
    pub fn new(pipe: P) -> Self {
        Self { pipe }
    }
}

impl<P> Pipe for Bypass<P>
where
    P: Pipe,
    P::InputItem: Clone,
{
    type InputItem = P::InputItem;
    type OutputItem = (P::InputItem, P::OutputItem);

    fn next(&mut self, input: P::InputItem) -> (P::InputItem, P::OutputItem) {
        (input.clone(), self.pipe.next(input))
    }

    fn reset(&mut self) {
        self.pipe.reset();
    }
}

/// A "lazily" create pipe with a mutable state.
///
/// This pipe's behavior is defined by a callable object, for example a lambda expression, and can therefore be "lazily" created inline.
///
/// Since callable objects with mutable state can not be reseted, `reset` will panic if it's called.
///
/// # Example
///
/// ```
/// use iterpipes::*;
///
/// let mut counter: u8 = 0;
/// let mut pipe = LazyMut::new(|i: u8| {
///     counter += 1;
///     i*counter
/// });
///
/// assert_eq!(1, pipe.next(1));
/// assert_eq!(4, pipe.next(2));
/// ```
pub struct LazyMut<I, O, F>
where
    F: FnMut(I) -> O,
{
    function: F,
    input: PhantomData<I>,
    output: PhantomData<O>,
}

impl<I, O, F> LazyMut<I, O, F>
where
    F: FnMut(I) -> O,
{
    /// Create a new lazy pipe.
    pub fn new(function: F) -> Self {
        LazyMut {
            function,
            input: PhantomData,
            output: PhantomData,
        }
    }
}

impl<I, O, F> Pipe for LazyMut<I, O, F>
where
    F: FnMut(I) -> O,
{
    type InputItem = I;
    type OutputItem = O;

    fn next(&mut self, input: I) -> O {
        (self.function)(input)
    }

    fn reset(&mut self) {
        unimplemented!();
    }
}

/// A "lazily" create pipe with an immutable state.
///
/// This pipe's behavior is defined by a callable object, for example a lambda expression, and can therefore be "lazily" created inline.
///
/// Since callable objects with an immutable state don't need to be reseted, `reset` is a no-op.
///
/// # Example
///
/// ```
/// use iterpipes::*;
///
/// let mut counter: u8 = 2;
/// let mut pipe = Lazy::new(|i: u8| {
///     i*counter
/// });
///
/// assert_eq!(2, pipe.next(1));
/// assert_eq!(4, pipe.next(2));
/// ```
pub struct Lazy<I, O, F>
where
    F: Fn(I) -> O,
{
    function: F,
    input: PhantomData<I>,
    output: PhantomData<O>,
}

impl<I, O, F> Lazy<I, O, F>
where
    F: Fn(I) -> O,
{
    /// Create a new lazy pipe.
    pub fn new(function: F) -> Self {
        Lazy {
            function,
            input: PhantomData,
            output: PhantomData,
        }
    }
}

impl<I, O, F> Pipe for Lazy<I, O, F>
where
    F: Fn(I) -> O,
{
    type InputItem = I;
    type OutputItem = O;

    fn next(&mut self, input: I) -> O {
        (self.function)(input)
    }

    fn reset(&mut self) {}
}

/// A pipe that wraps another pipe's IO in an `Option`.
///
/// For more information, please see [the documentation of the `optional` method](trait.Pipe.html#method.optional).
pub struct Optional<P>
where
    P: Pipe,
{
    pipe: P,
}

impl<P: Pipe> Optional<P> {
    /// Create a new optional pipe.
    pub fn new(pipe: P) -> Self {
        Optional { pipe }
    }
}

impl<P> Pipe for Optional<P>
where
    P: Pipe,
{
    type InputItem = Option<P::InputItem>;
    type OutputItem = Option<P::OutputItem>;

    fn next(&mut self, item: Option<P::InputItem>) -> Option<P::OutputItem> {
        item.map(|item| self.pipe.next(item))
    }

    fn reset(&mut self) {
        self.pipe.reset();
    }
}

/// A pipe that enumerates the output items of another pipe.
///
/// The inputs of this pipe are the same as the wrapped ones, but it's output item is a tuple of an index and the wrapped pipe's output. The index starts with zero and counts up for every produces output item.
///
/// For more information, please see [the documentation of the `enumerate` method](trait.Pipe.html#method.enumerate).
pub struct Enumerate<P>
where
    P: Pipe,
{
    pipe: P,
    progress: usize,
}

impl<P: Pipe> Enumerate<P> {
    /// Create a new enumerating pipe.
    pub fn new(pipe: P) -> Self {
        Enumerate { pipe, progress: 0 }
    }
}

impl<P: Pipe> Pipe for Enumerate<P> {
    type InputItem = P::InputItem;
    type OutputItem = (usize, P::OutputItem);

    fn next(&mut self, item: P::InputItem) -> (usize, P::OutputItem) {
        let next_item = self.pipe.next(item);
        let index = self.progress;
        self.progress += 1;
        (index, next_item)
    }

    fn reset(&mut self) {
        self.pipe.reset();
        self.progress = 0;
    }
}

/// A continous counter.
///
/// This pipe has an counter and a delta value. Every time `next` is called, the current counter value is returned and the delta is added to the counter. It also knows it's starting value and can therefore be reseted.
///
/// # Example
///
/// ```
/// use iterpipes::*;
///
/// let mut counter: Counter<u8> = Counter::new(1, 2);
/// assert_eq!(1, counter.next(()));
/// assert_eq!(3, counter.next(()));
/// ```
pub struct Counter<T>
where
    T: std::ops::AddAssign<T> + Copy,
{
    starting_value: T,
    delta: T,
    counter: T,
}

impl<T> Counter<T>
where
    T: std::ops::AddAssign<T> + Copy,
{
    pub fn new(starting_value: T, delta: T) -> Self {
        Self {
            starting_value,
            delta,
            counter: starting_value,
        }
    }
}

impl<T> Pipe for Counter<T>
where
    T: std::ops::AddAssign<T> + Copy,
{
    type InputItem = ();
    type OutputItem = T;

    fn next(&mut self, _: ()) -> T {
        let item = self.counter;
        self.counter += self.delta;
        item
    }

    fn reset(&mut self) {
        self.counter = self.starting_value;
    }
}