use { std::{ fmt::Debug, task::{Context, Poll}, }, tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}, }; /// FIFO queue used in async streams. /// /// This type symantically behaves like a fifo queue and it enables /// signalling futures whenever new elements are available through a Waker. pub struct Channel { tx: UnboundedSender, rx: UnboundedReceiver, } impl Channel { pub fn new() -> Self { let (tx, rx) = unbounded_channel(); Self { tx, rx } } pub fn send(&self, message: T) { self .tx .send(message) .expect("lifetime of receiver is equal to sender") } pub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll> { self.rx.poll_recv(cx) } pub fn sender(&self) -> UnboundedSender { self.tx.clone() } pub fn split(self) -> (UnboundedSender, UnboundedReceiver) { (self.tx, self.rx) } }