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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::ops::BitOr;

use signalo_traits::filter::Filter;
use signalo_traits::source::Source;

/// A `Pipe` is a simple container joining a pair of a `Source` and a `Filter`
///
/// ```plain
/// ╠════════════ + ════════════
/// ║ ╭────────╮  +  ╭────────╮
/// ║ │ Source │  +  │ Filter │
/// ║ ╰────────╯  +  ╰────────╯
/// ╠════════════ + ════════════
/// └─┬───────────────────────┘
///   └ Pipe
/// ```
#[derive(Default, Clone, Debug)]
pub struct Pipe<T, U> {
    lhs: T,
    rhs: U,
}

impl<T, U> Pipe<T, U>
{
    /// Creates a new pipe connecting `lhs` and `rhs`.
    #[inline]
    pub fn new(lhs: T, rhs: U) -> Self {
        Self { lhs, rhs }
    }
}

impl<T, U> From<(T, U)> for Pipe<T, U> {
    #[inline]
    fn from(parts: (T, U)) -> Self {
        let (lhs, rhs) = parts;
        Self::new(lhs, rhs)
    }
}

impl<T, U, Rhs> BitOr<Rhs> for Pipe<T, U> {
    type Output = Pipe<Self, Rhs>;

    #[inline]
    fn bitor(self, rhs: Rhs) -> Self::Output {
        Pipe::new(self, rhs)
    }
}

impl<T, U> Source for Pipe<T, U>
where
    T: Source,
    U: Filter<T::Output>,
{
    type Output = U::Output;

    #[inline]
    fn source(&mut self) -> Option<Self::Output> {
        self.lhs.source().map(|input| self.rhs.filter(input))
    }
}

impl<T, U> Filter<()> for Pipe<T, U>
where
    T: Source,
    U: Filter<T::Output>,
{
    type Output = Option<U::Output>;

    #[inline]
    fn filter(&mut self, _input: ()) -> Self::Output {
        self.lhs.source().map(|input| self.rhs.filter(input))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    type Value = usize;
    const VALUE: Value = 42;

    struct DummySource;

    impl Source for DummySource {
        type Output = Value;

        #[inline]
        fn source(&mut self) -> Option<Self::Output> {
            Some(VALUE)
        }
    }

    struct DummyFilter;

    impl Filter<Value> for DummyFilter {
        type Output = Value;

        #[inline]
        fn filter(&mut self, input: Value) -> Self::Output {
            input + 1
        }
    }

    #[test]
    fn test() {
        const COUNT: usize = 3;
        let pipe = Pipe::new(DummySource, DummyFilter);
        let subject: Vec<_> = (0..COUNT).scan(pipe, |pipe, _| {
            pipe.source()
        }).collect();
        let expected = vec![VALUE + 1; COUNT];
        assert_eq!(subject, expected);
    }
}