use crate::*;
use futures::task::Context;
use futures::task::Poll;
use std::pin::Pin;
pub type Receiver<T> = crate::channel::Receiver<T>;
#[derive(Clone, Debug, PartialEq)]
pub struct Sender<T> {
sender: crate::channel::Sender<T>,
}
unsafe impl<T: Send> Send for Sender<T> {}
unsafe impl<T: Sync> Sync for Sender<T> {}
impl<T: Message + 'static> Sender<T> {
fn new(sender: crate::channel::Sender<T>) -> Self {
Sender { sender }
}
#[allow(unused_mut)]
pub fn send(mut self, v: T) -> Result<(), T> {
self.sender.send_msg(v);
Ok(())
}
}
pub fn channel<T>() -> (Sender<T>, Receiver<T>)
where
T: Clone + std::fmt::Debug + PartialEq + Message + 'static,
{
let (tx, rx) = crate::channel::Builder::<T>::new().build();
(Sender::new(tx), rx)
}
impl<T: Message + Clone + 'static> Future for Receiver<T> {
type Output = Result<T, error::RecvError>;
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(Ok(self.recv_msg_block()))
}
}
pub mod error {
use std::fmt;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct RecvError(pub(super) ());
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum TryRecvError {
Empty,
Closed,
}
impl fmt::Display for RecvError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "channel closed")
}
}
impl std::error::Error for RecvError {}
impl fmt::Display for TryRecvError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TryRecvError::Empty => write!(fmt, "channel empty"),
TryRecvError::Closed => write!(fmt, "channel closed"),
}
}
}
impl std::error::Error for TryRecvError {}
}