async_transmit/transmit/
with.rs

1use async_trait::async_trait;
2use core::marker::PhantomData;
3
4use crate::transmit::Transmit;
5
6#[derive(Debug)]
7pub struct With<T, F, I, U, E> {
8    inner: T,
9    f: F,
10    phantom: PhantomData<(I, U, E)>,
11}
12
13impl<T, F, I, U, E> With<T, F, I, U, E>
14where
15    T: Transmit<Item = I, Error = E> + Send,
16    I: Send,
17    E: Send,
18{
19    pub(crate) fn new(inner: T, f: F) -> Self {
20        Self {
21            inner,
22            f,
23            phantom: PhantomData,
24        }
25    }
26
27    /// Consumes this combinator, returning the underlying transmit.
28    pub fn into_inner(self) -> T {
29        self.inner
30    }
31
32    /// Acquires a reference to the underlying transmit that this
33    /// combinator is pulling from.
34    pub fn get_ref(&self) -> &T {
35        &self.inner
36    }
37
38    /// Acquires a mutable reference to the underlying transmit that
39    /// this combinator is pulling from.
40    pub fn get_mut(&mut self) -> &mut T {
41        &mut self.inner
42    }
43}
44
45#[async_trait]
46impl<T, F, I, U, E> Transmit for With<T, F, I, U, E>
47where
48    T: Transmit<Item = I, Error = E> + Send,
49    F: FnMut(U) -> I + Send,
50    I: Send,
51    U: Send,
52    E: Send,
53{
54    type Item = U;
55    type Error = E;
56
57    async fn transmit(&mut self, item: Self::Item) -> Result<(), Self::Error> {
58        let item = (self.f)(item);
59        self.inner.transmit(item).await
60    }
61}