pub use futures::channel::mpsc::{SendError, TrySendError};
use futures::{
channel::mpsc::{self, Receiver, Sender},
prelude::*,
};
use std::{
pin::Pin,
task::{Context, Poll},
};
#[derive(Debug)]
pub struct MessageBox<Msg>(Sender<Msg>);
#[derive(Debug)]
pub struct MessageQueue<Msg>(Receiver<Msg>);
pub fn channel<Msg>(buffer: usize) -> (MessageBox<Msg>, MessageQueue<Msg>) {
let (tx, rx) = mpsc::channel(buffer);
(MessageBox(tx), MessageQueue(rx))
}
impl<Msg> MessageBox<Msg> {
pub fn try_send(&mut self, a: Msg) -> Result<(), TrySendError<Msg>> {
self.0.try_send(a)
}
pub fn start_send(&mut self, a: Msg) -> Result<(), SendError> {
self.0.start_send(a)
}
pub fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), SendError>> {
self.0.poll_ready(cx)
}
}
impl<Msg> Sink<Msg> for MessageBox<Msg> {
type Error = SendError;
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), SendError>> {
self.0.poll_ready(cx)
}
fn start_send(mut self: Pin<&mut Self>, msg: Msg) -> Result<(), SendError> {
self.0.start_send(msg)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), SendError>> {
Pin::new(&mut self.0).poll_flush(cx)
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), SendError>> {
Pin::new(&mut self.0).poll_close(cx)
}
}
impl<Msg> Stream for MessageQueue<Msg> {
type Item = Msg;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Msg>> {
Pin::new(&mut self.0).poll_next(cx)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<Msg> Clone for MessageBox<Msg> {
fn clone(&self) -> Self {
MessageBox(self.0.clone())
}
}