Skip to main content

iterpipes/
composed.rs

1use crate::{Connector, Pipe, ResetablePipe};
2use std::ops::Shr;
3
4/// A composable or composed pipe.
5///
6/// This struct is an implementation of the [newtype pattern](https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-the-newtype-pattern-to-implement-external-traits-on-external-types) to implement the `>>` operator for pipes (manifested as the `Shr` trait).
7///
8/// For more information, please see [the documentation of the `compose` method](trait.Pipe.html#method.compose).
9pub struct Composed<P>
10where
11    P: Pipe,
12{
13    pipe: P,
14}
15
16impl<P> Composed<P>
17where
18    P: Pipe,
19{
20    /// Create new composable or composed pipe.
21    pub fn new(pipe: P) -> Self {
22        Composed { pipe }
23    }
24
25    /// Unwrap the inner pipe.
26    pub fn unwrap(self) -> P {
27        self.pipe
28    }
29}
30
31impl<P> Pipe for Composed<P>
32where
33    P: Pipe,
34{
35    type InputItem = P::InputItem;
36    type OutputItem = P::OutputItem;
37
38    fn next(&mut self, item: P::InputItem) -> P::OutputItem {
39        self.pipe.next(item)
40    }
41}
42
43impl<P: ResetablePipe> ResetablePipe for Composed<P>
44where
45    P: ResetablePipe,
46{
47    fn reset(&mut self) {
48        self.pipe.reset();
49    }
50}
51
52impl<P0, P1> Shr<P1> for Composed<P0>
53where
54    P0: Pipe,
55    P1: Pipe<InputItem = P0::OutputItem>,
56{
57    type Output = Composed<Connector<P0, P1>>;
58
59    fn shr(self, other: P1) -> Self::Output {
60        Self::Output::new(Connector::new(self.pipe, other))
61    }
62}