pub use crate::system::errors::mpsc::{RecvError, RecvTimeoutError, SendError, TryRecvError};
use crate::time::Instant;
#[must_use]
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
let (tx, rx) = std::sync::mpsc::channel();
(Sender(tx), Receiver(rx))
}
#[derive_where::derive_where(Clone)]
pub struct Sender<T>(std::sync::mpsc::Sender<T>);
impl<T> Sender<T> {
pub fn send(&self, value: T) -> Result<(), SendError<T>> {
self.0.send(value)
}
}
pub struct Receiver<T>(std::sync::mpsc::Receiver<T>);
impl<T> Receiver<T> {
#[track_caller]
pub fn recv(&self) -> Result<T, RecvError> {
crate::no_block::forbid("mpsc::recv");
self.0.recv()
}
#[track_caller]
pub fn recv_timeout(&self, deadline: Instant) -> Result<T, RecvTimeoutError> {
crate::no_block::forbid("mpsc::recv_timeout");
let now = Instant::now();
let remaining = deadline.saturating_duration_since(now);
self.0.recv_timeout(remaining)
}
delegate::delegate! {
to self.0 {
pub fn iter(&self) -> impl Iterator<Item = T> + '_;
pub fn try_iter(&self) -> impl Iterator<Item = T> + '_;
pub fn try_recv(&self) -> Result<T, TryRecvError>;
}
}
}