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
use async_trait::async_trait;
use futures_sink::Sink;
use futures_util::sink::SinkExt;
use std::marker::PhantomData;

use crate::transmit::Transmit;

pub struct FromSink<S, I, E> {
    sink: S,
    phantom: PhantomData<(I, E)>,
}

impl<S, I, E> FromSink<S, I, E> {
    fn new(sink: S) -> Self {
        Self {
            sink,
            phantom: PhantomData,
        }
    }

    /// Consumes this transmit, returning the underlying sink.
    pub fn into_inner(self) -> S {
        self.sink
    }

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

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

#[async_trait]
impl<S, I> Transmit for FromSink<S, I, S::Error>
where
    I: Send,
    S: Sink<I> + Unpin + Send,
    S::Error: Send,
{
    type Item = I;
    type Error = S::Error;

    async fn transmit(&mut self, item: Self::Item) -> Result<(), Self::Error> {
        SinkExt::send(&mut self.sink, item).await?;
        Ok(())
    }
}

impl<S, I> From<S> for FromSink<S, I, S::Error>
where
    S: Sink<I>,
{
    fn from(sink: S) -> Self {
        Self::new(sink)
    }
}

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

    use anyhow::Result;
    use futures::channel::mpsc;
    use futures::prelude::*;
    use futures_await_test::async_test;

    #[async_test]
    async fn from_sink_is_transmit() -> Result<()> {
        let (s, mut r) = mpsc::unbounded::<&'static str>();

        let mut t = assert_transmit(FromSink::from(s));
        assert_eq!((), t.transmit("Hello").await?);
        assert_eq!((), t.transmit("World").await?);
        drop(t);
        assert_eq!(r.next().await, Some("Hello"));
        assert_eq!(r.next().await, Some("World"));
        assert_eq!(r.next().await, None);

        Ok(())
    }
}