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
use async_trait::async_trait;
use core::marker::PhantomData;

use crate::transmit::Transmit;

pub struct With<T, F, I, U, E> {
    inner: T,
    f: F,
    phantom: PhantomData<(I, U, E)>,
}

impl<T, F, I, U, E> With<T, F, I, U, E>
where
    T: Transmit<Item = I, Error = E> + Send,
    I: Send,
    E: Send,
{
    pub(crate) fn new(inner: T, f: F) -> Self {
        Self {
            inner,
            f,
            phantom: PhantomData,
        }
    }

    /// Consumes this combinator, returning the underlying transmit.
    pub fn into_inner(self) -> T {
        self.inner
    }

    /// Acquires a reference to the underlying transmit that this
    /// combinator is pulling from.
    pub fn get_ref(&self) -> &T {
        &self.inner
    }

    /// Acquires a mutable reference to the underlying transmit that
    /// this combinator is pulling from.
    pub fn get_mut(&mut self) -> &mut T {
        &mut self.inner
    }
}

#[async_trait]
impl<T, F, I, U, E> Transmit for With<T, F, I, U, E>
where
    T: Transmit<Item = I, Error = E> + Send,
    F: FnMut(U) -> I + Send,
    I: Send,
    U: Send,
    E: Send,
{
    type Item = U;
    type Error = E;

    async fn transmit(&mut self, item: Self::Item) -> Result<(), Self::Error> {
        let item = (self.f)(item);
        self.inner.transmit(item).await
    }
}