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
//! channels in the futures-rs crate cannot error, yet they return () for their error type for some
//! stupid reason. This is a wrapper around futures-rs unbounded channels which removes the error.

use futures::{self, Stream, Async};
use void::Void;

pub use futures::sync::mpsc::{UnboundedSender, SendError};

#[derive(Debug)]
pub struct UnboundedReceiver<T> {
    inner: futures::sync::mpsc::UnboundedReceiver<T>,
}

pub fn unbounded<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) {
    let (tx, rx) = futures::sync::mpsc::unbounded();
    (tx, UnboundedReceiver { inner: rx })
}

impl<T> Stream for UnboundedReceiver<T> {
    type Item = T;
    type Error = Void;

    fn poll(&mut self) -> Result<Async<Option<T>>, Void> {
        Ok(unwrap!(self.inner.poll()))
    }
}

impl<T> UnboundedReceiver<T> {
    /// Closes the receiving half
    ///
    /// This prevents any further messages from being sent on the channel while still enabling the
    /// receiver to drain messages that are buffered.
    pub fn close(&mut self) {
        self.inner.close()
    }
}