async_transmit/transmit/
transmit_map_err.rs

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