future_utils/
mpsc.rs

1//! channels in the futures-rs crate cannot error, yet they return () for their error type for some
2//! stupid reason. This is a wrapper around futures-rs unbounded channels which removes the error.
3
4use futures::{self, Stream, Async};
5use void::Void;
6
7pub use futures::sync::mpsc::{UnboundedSender, SendError};
8
9#[derive(Debug)]
10pub struct UnboundedReceiver<T> {
11    inner: futures::sync::mpsc::UnboundedReceiver<T>,
12}
13
14pub fn unbounded<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) {
15    let (tx, rx) = futures::sync::mpsc::unbounded();
16    (tx, UnboundedReceiver { inner: rx })
17}
18
19impl<T> Stream for UnboundedReceiver<T> {
20    type Item = T;
21    type Error = Void;
22
23    fn poll(&mut self) -> Result<Async<Option<T>>, Void> {
24        Ok(unwrap!(self.inner.poll()))
25    }
26}
27
28impl<T> UnboundedReceiver<T> {
29    /// Closes the receiving half
30    ///
31    /// This prevents any further messages from being sent on the channel while still enabling the
32    /// receiver to drain messages that are buffered.
33    pub fn close(&mut self) {
34        self.inner.close()
35    }
36}
37