nuclei/sync/local/mpsc/
unbounded.rs1use core::fmt;
4use std::rc::Rc;
5
6use super::chan::{Chan, Limit};
7use crate::sync::mpsc::error::SendError;
8
9pub(super) struct Unbounded;
10
11impl Limit for Unbounded {
12 fn release(&self, _n: usize) {}
13
14 fn is_idle(&self) -> bool {
15 true
16 }
17
18 fn close(&self) {}
19}
20
21pub fn unbounded_channel<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) {
35 let chan = Rc::new(Chan::new(Unbounded));
36 let tx = UnboundedSender {
37 chan: Rc::clone(&chan),
38 };
39 (tx, UnboundedReceiver { chan })
40}
41
42pub struct UnboundedSender<T> {
46 chan: Rc<Chan<T, Unbounded>>,
47}
48
49impl<T> UnboundedSender<T> {
50 pub fn send(&self, value: T) -> Result<(), SendError<T>> {
56 if self.chan.is_closed() {
57 return Err(SendError(value));
58 }
59 self.chan.push(value);
60 Ok(())
61 }
62
63 pub async fn closed(&self) {
66 self.chan.closed().await;
67 }
68
69 pub fn is_closed(&self) -> bool {
71 self.chan.is_closed()
72 }
73
74 pub fn same_channel(&self, other: &Self) -> bool {
76 Rc::ptr_eq(&self.chan, &other.chan)
77 }
78}
79
80impl<T> Clone for UnboundedSender<T> {
81 fn clone(&self) -> Self {
82 self.chan.add_sender();
83 Self {
84 chan: Rc::clone(&self.chan),
85 }
86 }
87}
88
89impl<T> Drop for UnboundedSender<T> {
90 fn drop(&mut self) {
91 self.chan.drop_sender();
92 }
93}
94
95impl<T> fmt::Debug for UnboundedSender<T> {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 f.debug_struct("UnboundedSender")
98 .field("closed", &self.is_closed())
99 .finish()
100 }
101}
102
103pub struct UnboundedReceiver<T> {
108 chan: Rc<Chan<T, Unbounded>>,
109}
110
111impl<T> UnboundedReceiver<T> {
112 receiver_methods!();
113}
114
115impl<T> Drop for UnboundedReceiver<T> {
116 fn drop(&mut self) {
117 self.chan.drop_rx();
118 }
119}
120
121impl<T> fmt::Debug for UnboundedReceiver<T> {
122 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123 f.debug_struct("UnboundedReceiver")
124 .field("len", &self.len())
125 .field("closed", &self.is_closed())
126 .finish()
127 }
128}