### Broadcast Blocking Example Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Demonstrates broadcasting a message using `broadcast_blocking`. It shows successful broadcast and error handling when the receiver is dropped. ```rust use async_broadcast::{broadcast, SendError}; let (s, r) = broadcast(1); assert_eq!(s.broadcast_blocking(1), Ok(None)); drop(r); assert_eq!(s.broadcast_blocking(2), Err(SendError(2))); ``` -------------------------------- ### Create New Receiver - Async Broadcast Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Produce a new Receiver for an existing channel. This receiver starts with zero messages available and is slightly faster than a full clone. It's useful for creating independent listening points for the same broadcast. ```rust # futures_lite::future::block_on(async { use async_broadcast::{broadcast, RecvError}; let (s, mut r1) = broadcast(2); assert_eq!(s.broadcast(1).await, Ok(None)); let mut r2 = r1.new_receiver(); assert_eq!(s.broadcast(2).await, Ok(None)); drop(s); assert_eq!(r1.recv().await, Ok(1)); assert_eq!(r1.recv().await, Ok(2)); assert_eq!(r1.recv().await, Err(RecvError::Closed)); assert_eq!(r2.recv().await, Ok(2)); assert_eq!(r2.recv().await, Err(RecvError::Closed)); # }); ``` -------------------------------- ### Create New Receiver - async-broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Produces a new Receiver for this channel. The new receiver starts with zero messages available and will not re-open a closed channel. ```rust use async_broadcast::{broadcast, RecvError}; let (s, mut r1) = broadcast(2); assert_eq!(s.broadcast(1).await, Ok(None)); let mut r2 = s.new_receiver(); assert_eq!(s.broadcast(2).await, Ok(None)); drop(s); assert_eq!(r1.recv().await, Ok(1)); assert_eq!(r1.recv().await, Ok(2)); assert_eq!(r1.recv().await, Err(RecvError::Closed)); assert_eq!(r2.recv().await, Ok(2)); assert_eq!(r2.recv().await, Err(RecvError::Closed)); ``` -------------------------------- ### Create New Receiver Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Produces a new Receiver for the channel. Unlike `Receiver::clone`, this creates a receiver that starts with zero messages available, offering a slight performance advantage over cloning. ```rust use async_broadcast::{broadcast, RecvError}; let (s, mut r1) = broadcast(2); assert_eq!(s.broadcast(1).await, Ok(None)); let mut r2 = r1.new_receiver(); assert_eq!(s.broadcast(2).await, Ok(None)); drop(s); assert_eq!(r1.recv().await, Ok(1)); assert_eq!(r1.recv().await, Ok(2)); assert_eq!(r1.recv().await, Err(RecvError::Closed)); assert_eq!(r2.recv().await, Ok(2)); assert_eq!(r2.recv().await, Err(RecvError::Closed)); ``` -------------------------------- ### Try Receive Message - async-broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Attempts to receive a message without waiting. Returns an error if the channel is empty or closed. This example shows successful receives, empty channel errors, and closed channel errors. ```rust use async_broadcast::{broadcast, TryRecvError}; let (s, mut r1) = broadcast(1); let mut r2 = r1.clone(); assert_eq!(s.broadcast(1).await, Ok(None)); assert_eq!(r1.try_recv(), Ok(1)); assert_eq!(r1.try_recv(), Err(TryRecvError::Empty)); assert_eq!(r2.try_recv(), Ok(1)); assert_eq!(r2.try_recv(), Err(TryRecvError::Empty)); drop(s); assert_eq!(r1.try_recv(), Err(TryRecvError::Closed)); assert_eq!(r2.try_recv(), Err(TryRecvError::Closed)); ``` -------------------------------- ### Create New Receiver for Async Broadcast Channel Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Produces a new Receiver for the channel. The new receiver starts with no messages available and will not re-open a closed channel. ```rust pub fn new_receiver(&self) -> Receiver { let mut inner = self.inner.lock().unwrap(); inner.receiver_count += 1; Receiver { inner: self.inner.clone(), pos: inner.head_pos + inner.queue.len() as u64, listener: None, } } ``` -------------------------------- ### Get Sender Capacity Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Returns the current capacity of the broadcast channel. ```rust use async_broadcast::broadcast; let (s, r) = broadcast::(5); assert_eq!(s.capacity(), 5); ``` -------------------------------- ### SendError Error::description (Deprecated) Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.SendError.html Deprecated method to get a description of the error. Use the Display implementation or to_string() instead. ```rust fn description(&self) -> &str ``` -------------------------------- ### Get Sender Count - async-broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Returns the number of active senders for the channel. ```rust use async_broadcast::broadcast; let (s, r) = broadcast::<()>(1); assert_eq!(s.sender_count(), 1); let s2 = s.clone(); assert_eq!(s.sender_count(), 2); ``` -------------------------------- ### Get Channel Capacity Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Returns the current capacity of the broadcast channel. This indicates the maximum number of messages the channel can hold. ```rust use async_broadcast::broadcast; let (_s, r) = broadcast::(5); assert_eq!(r.capacity(), 5); ``` -------------------------------- ### SendError Error::cause (Deprecated) Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.SendError.html Deprecated method to get the cause of the error. Use Error::source instead. ```rust fn cause(&self) -> Option<&dyn Error> ``` -------------------------------- ### Get the number of messages in the channel Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns the current number of messages buffered in the channel. This count can change immediately after retrieval. ```rust # futures_lite::future::block_on(async { use async_broadcast::broadcast; let (s, r) = broadcast(2); assert_eq!(s.len(), 0); s.broadcast(1).await; s.broadcast(2).await; assert_eq!(s.len(), 2); # }); ``` -------------------------------- ### Get Sender Channel Length Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Returns the number of messages currently present in the broadcast channel. This count is from the perspective of the sender. ```rust use async_broadcast::broadcast; let (s, r) = broadcast(2); assert_eq!(s.len(), 0); s.broadcast(1).await; s.broadcast(2).await; assert_eq!(s.len(), 2); ``` -------------------------------- ### Create New Sender from Receiver Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Produces a new Sender for the channel. This does not re-open the channel if it was closed due to all senders being dropped. It allows for creating additional senders after the initial broadcast setup. ```rust use async_broadcast::{broadcast, RecvError}; let (s1, mut r) = broadcast(2); assert_eq!(s1.broadcast(1).await, Ok(None)); let mut s2 = r.new_sender(); assert_eq!(s2.broadcast(2).await, Ok(None)); drop(s1); drop(s2); assert_eq!(r.recv().await, Ok(1)); assert_eq!(r.recv().await, Ok(2)); assert_eq!(r.recv().await, Err(RecvError::Closed)); ``` -------------------------------- ### Get Sender Count for Async Broadcast Channel Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns the number of active senders for the channel. This count increases when the sender is cloned. ```rust pub fn sender_count(&self) -> usize { self.inner.lock().unwrap().sender_count } ``` -------------------------------- ### Create and Use a Broadcast Channel Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Demonstrates creating a broadcast channel with a capacity of 1, sending a message, and receiving it with two subscribers. It also shows error handling for full and empty channels. ```rust use async_broadcast::{broadcast, TryRecvError, TrySendError}; futures_lite::future::block_on(async { let (s, mut r1) = broadcast(1); let mut r2 = r1.clone(); assert_eq!(s.broadcast(10).await, Ok(None)); assert_eq!(s.try_broadcast(20), Err(TrySendError::Full(20))); assert_eq!(r1.recv().await, Ok(10)); assert_eq!(r2.recv().await, Ok(10)); assert_eq!(r1.try_recv(), Err(TryRecvError::Empty)); assert_eq!(r2.try_recv(), Err(TryRecvError::Empty)); }); ``` -------------------------------- ### Create and Use a Broadcast Channel Source: https://docs.rs/async-broadcast/latest/async_broadcast/fn.broadcast.html Demonstrates creating a broadcast channel with a capacity of 1, sending a message, and receiving it on multiple cloned receivers. Shows how `broadcast` and `try_broadcast` behave when the channel is full. ```rust use async_broadcast::{broadcast, TryRecvError, TrySendError}; let (s, mut r1) = broadcast(1); let mut r2 = r1.clone(); assert_eq!(s.broadcast(10).await, Ok(None)); assert_eq!(s.try_broadcast(20), Err(TrySendError::Full(20))); assert_eq!(r1.recv().await, Ok(10)); assert_eq!(r2.recv().await, Ok(10)); assert_eq!(r1.try_recv(), Err(TryRecvError::Empty)); assert_eq!(r2.try_recv(), Err(TryRecvError::Empty)); ``` -------------------------------- ### Type ID Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html The `Any` trait implementation provides a way to get the `TypeId` of a value. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### CloneToUninit for T Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Nightly-only experimental API for cloning into uninitialized memory. ```APIDOC ## unsafe fn clone_to_uninit(&self, dest: *mut u8) ### Description Performs copy-assignment from `self` to `dest`. ### Method `clone_to_uninit` ### Parameters - `&self`: An immutable reference to the source value. - `dest`: A raw pointer to the destination memory location. ``` -------------------------------- ### Basic Broadcast Channel Usage Source: https://docs.rs/async-broadcast/latest/async_broadcast/index.html Demonstrates creating a broadcast channel, sending messages from multiple senders, and receiving messages from multiple receivers. Shows how to handle channel capacity and closure. ```rust use async_broadcast::{broadcast, TryRecvError}; use futures_lite::{future::block_on, stream::StreamExt}; block_on(async move { let (s1, mut r1) = broadcast(2); let s2 = s1.clone(); let mut r2 = r1.clone(); // Send 2 messages from two different senders. s1.broadcast(7).await.unwrap(); s2.broadcast(8).await.unwrap(); // Channel is now at capacity so sending more messages will result in an error. assert!(s2.try_broadcast(9).unwrap_err().is_full()); assert!(s1.try_broadcast(10).unwrap_err().is_full()); // We can use `recv` method of the `Stream` implementation to receive messages. assert_eq!(r1.next().await.unwrap(), 7); assert_eq!(r1.recv().await.unwrap(), 8); assert_eq!(r2.next().await.unwrap(), 7); assert_eq!(r2.recv().await.unwrap(), 8); // All receiver got all messages so channel is now empty. assert_eq!(r1.try_recv(), Err(TryRecvError::Empty)); assert_eq!(r2.try_recv(), Err(TryRecvError::Empty)); // Drop both senders, which closes the channel. drop(s1); drop(s2); assert_eq!(r1.try_recv(), Err(TryRecvError::Closed)); assert_eq!(r2.try_recv(), Err(TryRecvError::Closed)); }) ``` -------------------------------- ### Get the number of active receivers Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns the count of currently active receivers for the channel. Inactive receivers are not included in this count. ```rust use async_broadcast::broadcast; let (s, r) = broadcast::<()>(1); assert_eq!(s.receiver_count(), 1); let r = r.deactivate(); assert_eq!(s.receiver_count(), 0); let r2 = r.activate_cloned(); ``` -------------------------------- ### is_full Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns true if the channel is full. ```APIDOC ## is_full ### Description Returns `true` if the channel is full. ### Method `is_full() -> bool` ``` -------------------------------- ### SendError Error::provide (Experimental) Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.SendError.html Experimental nightly-only API for providing type-based access to error context. ```rust fn provide<'a>(&'a self, request: &mut Request<'a>) ``` -------------------------------- ### New Receiver Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Produce a new Receiver for this channel. ```APIDOC ## new_receiver ### Description Produce a new Receiver for this channel. The new receiver starts with zero messages available. This will not re-open the channel if it was closed due to all receivers being dropped. ### Method `new_receiver(&self) -> Receiver` ### Parameters None ### Response - `Receiver`: A new receiver instance. ``` -------------------------------- ### try_into Method Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TrySendError.html The `try_into` method attempts to perform a conversion, returning a Result that indicates success or failure. ```APIDOC #### fn try_into(self) -> Result>::Error> Performs the conversion. ``` -------------------------------- ### Get Receiver Count (Excluding Inactive) Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Retrieves the number of currently active receivers for the channel. Inactive receivers are not included in this count. ```rust use async_broadcast::broadcast; let (s, r) = broadcast::<()>(1); assert_eq!(s.receiver_count(), 1); let r = r.deactivate(); assert_eq!(s.receiver_count(), 0); let r2 = r.activate_cloned(); assert_eq!(r.receiver_count(), 1); assert_eq!(r.inactive_receiver_count(), 1); ``` -------------------------------- ### Get Inactive Receiver Count for Async Broadcast Channel Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns the number of inactive receivers for the channel. Inactive receivers are those that have been explicitly deactivated. ```rust pub fn inactive_receiver_count(&self) -> usize { self.inner.lock().unwrap().inactive_receiver_count } ``` -------------------------------- ### Clone to Uninitialized Memory (Nightly) Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html This experimental, nightly-only API allows cloning a value into uninitialized memory. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### Get Receiver Count for Async Broadcast Channel Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns the number of active receivers for the channel. This count includes receivers that have been deactivated but not yet dropped. ```rust pub fn receiver_count(&self) -> usize { self.inner.lock().unwrap().receiver_count } ``` -------------------------------- ### new_receiver Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Produces a new Receiver for this channel. ```APIDOC ## new_receiver ### Description Produce a new Receiver for this channel. The new receiver starts with zero messages available. This will not re-open the channel if it was closed due to all receivers being dropped. ### Method `&self.new_receiver() -> Receiver` ``` -------------------------------- ### Compare TryRecvError for Equality Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html Implementations for `PartialEq` and `Eq` allow comparing `TryRecvError` values for equality. ```rust fn eq(&self, other: &TryRecvError) -> bool ``` ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### is_empty Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns true if the channel is empty. ```APIDOC ## is_empty ### Description Returns `true` if the channel is empty. ### Method `is_empty() -> bool` ``` -------------------------------- ### Deactivate Receiver - async-broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Downgrades a receiver to an inactive state, preventing it from receiving messages. This example shows that broadcasting to a channel with only inactive receivers results in `TrySendError::Inactive`. ```rust use async_broadcast::{broadcast, TrySendError}; let (s, r) = broadcast(1); let inactive = r.deactivate(); assert_eq!(s.try_broadcast(10), Err(TrySendError::Inactive(10))); let mut r = inactive.activate(); assert_eq!(s.broadcast(10).await, Ok(None)); assert_eq!(r.recv().await, Ok(10)); ``` -------------------------------- ### Display Implementation for TryRecvError Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html This implementation allows `TryRecvError` to be formatted as a display string, providing human-readable messages for different error conditions like empty, closed, or overflowed channels. ```rust impl fmt::Display for TryRecvError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { TryRecvError::Empty => write!(f, "receiving from an empty channel"), TryRecvError::Closed => write!(f, "receiving from an empty and closed channel"), TryRecvError::Overflowed(n) => { write!(f, "receiving operation observed {} lost messages", n) } } } } ``` -------------------------------- ### TryInto Conversion Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html The `TryInto` implementation allows attempting a conversion from `T` to `U`, returning a `Result`. ```rust type Error = >::Error ``` ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### is_full Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Checks if the broadcast channel is full. A channel is full if it has reached its capacity. ```APIDOC ## is_full ### Description Checks if the broadcast channel is full. A channel is full if it has reached its capacity. ### Method `is_full(&self) -> bool` ### Examples ```rust use async_broadcast::broadcast; let (s, r) = broadcast(1); assert!(!s.is_full()); s.broadcast(1).await; assert!(s.is_full()); ``` ``` -------------------------------- ### TryRecvError Methods for Checking Channel State Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html These methods help determine the state of the channel when a `TryRecvError` occurs. Use `is_empty` to check if the channel is empty but still open, `is_closed` for an empty and closed channel, and `is_overflowed` to see if messages were missed. ```rust impl TryRecvError { /// Returns `true` if the channel is empty but not closed. pub fn is_empty(&self) -> bool { match self { TryRecvError::Empty => true, TryRecvError::Closed => false, TryRecvError::Overflowed(_) => false, } } /// Returns `true` if the channel is empty and closed. pub fn is_closed(&self) -> bool { match self { TryRecvError::Empty => false, TryRecvError::Closed => true, TryRecvError::Overflowed(_) => false, } } /// Returns `true` if this error indicates the receiver missed messages. pub fn is_overflowed(&self) -> bool { match self { TryRecvError::Empty => false, TryRecvError::Closed => false, TryRecvError::Overflowed(_) => true, } } } ``` -------------------------------- ### Try Broadcast Message - async-broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Attempts to broadcast a message on the channel. Returns an error if the channel is full, unless overflow mode is enabled. Returns an error if the channel is closed. ```rust use async_broadcast::{broadcast, TrySendError}; let (s, r) = broadcast(1); assert_eq!(s.try_broadcast(1), Ok(None)); assert_eq!(s.try_broadcast(2), Err(TrySendError::Full(2))); drop(r); assert_eq!(s.try_broadcast(3), Err(TrySendError::Closed(3))); ``` -------------------------------- ### From Conversion Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html The `From` implementation for `T` allows converting a value into itself. ```rust fn from(t: T) -> T ``` -------------------------------- ### broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/fn.broadcast.html Creates a new broadcast channel. The channel has space to hold at most `cap` messages at a time. Panics if `cap` is zero. ```APIDOC ## broadcast(cap: usize) -> (Sender, Receiver) ### Description Create a new broadcast channel. The created channel has space to hold at most `cap` messages at a time. ### Panics Capacity must be a positive number. If `cap` is zero, this function will panic. ### Examples ```rust use async_broadcast::{broadcast, TryRecvError, TrySendError}; let (s, mut r1) = broadcast(1); let mut r2 = r1.clone(); assert_eq!(s.broadcast(10).await, Ok(None)); assert_eq!(s.try_broadcast(20), Err(TrySendError::Full(20))); assert_eq!(r1.recv().await, Ok(10)); assert_eq!(r2.recv().await, Ok(10)); assert_eq!(r1.try_recv(), Err(TryRecvError::Empty)); assert_eq!(r2.try_recv(), Err(TryRecvError::Empty)); ``` ``` -------------------------------- ### Receiver Status and Configuration Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Provides methods to check the status of the broadcast channel and configure its behavior. ```APIDOC ## Receiver Methods ### `overflow()` #### Description Checks if the channel is in overflow mode. #### Method `GET` #### Endpoint `/receiver/overflow` ### `set_overflow(overflow: bool)` #### Description Sets the overflow mode for the channel. When overflow mode is enabled, broadcasting to a full channel will remove the oldest message. #### Method `POST` #### Endpoint `/receiver/set_overflow` #### Parameters ##### Request Body - **overflow** (bool) - Required - Whether to enable or disable overflow mode. ### `await_active()` #### Description Checks if the sender will wait for active receivers. If `false`, `Send` resolves immediately with an error if no receivers are active. #### Method `GET` #### Endpoint `/receiver/await_active` ### `set_await_active(await_active: bool)` #### Description Specifies whether the sender should wait for active receivers. If set to `false`, `Send` will resolve immediately with a `SendError` if no receivers are active. #### Method `POST` #### Endpoint `/receiver/set_await_active` #### Parameters ##### Request Body - **await_active** (bool) - Required - Whether the sender should wait for active receivers. ### `close()` #### Description Closes the channel. Returns `true` if the channel was closed by this call and was not already closed. Remaining messages can still be received. #### Method `POST` #### Endpoint `/receiver/close` ### `is_closed()` #### Description Checks if the channel is closed. #### Method `GET` #### Endpoint `/receiver/is_closed` ### `is_empty()` #### Description Checks if the channel is empty. #### Method `GET` #### Endpoint `/receiver/is_empty` ### `is_full()` #### Description Checks if the channel is full. #### Method `GET` #### Endpoint `/receiver/is_full` ### `len()` #### Description Returns the number of messages currently in the channel. #### Method `GET` #### Endpoint `/receiver/len` ### `receiver_count()` #### Description Returns the number of active receivers for the channel. #### Method `GET` #### Endpoint `/receiver/receiver_count` ### `inactive_receiver_count()` #### Description Returns the number of inactive receivers for the channel. #### Method `GET` #### Endpoint `/receiver/inactive_receiver_count` ### `sender_count()` #### Description Returns the number of senders for the channel. #### Method `GET` #### Endpoint `/receiver/sender_count` ``` -------------------------------- ### TryInto for T Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Blanket implementation of the TryInto trait for any type T. ```APIDOC ## fn try_into(self) -> Result>::Error> ### Description Performs the conversion. ### Method `try_into` ### Parameters - `self`: The value to convert. ### Returns - `Result>::Error>`: Ok(U) if the conversion was successful, or an error if it failed. ``` -------------------------------- ### Into Conversion Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html The `Into` implementation allows converting a value into another type `U` if `U` implements `From`. ```rust fn into(self) -> U ``` -------------------------------- ### Borrowing Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html Implementations for `Borrow` and `BorrowMut` allow borrowing references to the value. ```rust fn borrow(&self) -> &T ``` ```rust fn borrow_mut(&mut self) -> &mut T ``` -------------------------------- ### Check if Channel is Empty Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html Use `is_empty()` to determine if the channel is empty but still open for receiving messages. ```rust pub fn is_empty(&self) -> bool ``` -------------------------------- ### Create New Sender - Async Broadcast Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Produce a new Sender for an existing channel. This does not re-open a channel if it was closed due to all senders being dropped. It is useful for scenarios where multiple senders are needed. ```rust # futures_lite::future::block_on(async { use async_broadcast::{broadcast, RecvError}; let (s1, mut r) = broadcast(2); assert_eq!(s1.broadcast(1).await, Ok(None)); let mut s2 = r.new_sender(); assert_eq!(s2.broadcast(2).await, Ok(None)); drop(s1); drop(s2); assert_eq!(r.recv().await, Ok(1)); assert_eq!(r.recv().await, Ok(2)); assert_eq!(r.recv().await, Err(RecvError::Closed)); # }); ``` -------------------------------- ### broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Broadcasts a message on the channel, waiting if the channel is full. ```APIDOC ## broadcast ### Description Broadcasts a message on the channel. If the channel is full, this method waits until there is space for a message unless overflow mode is enabled or awaiting active receivers is disabled. If the channel is closed, this method returns an error. The future returned by this function is pinned to the heap. ### Method `&self.broadcast(msg: T) -> Pin>>` ``` -------------------------------- ### TryFrom Conversion Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html The `TryFrom` implementation allows attempting a conversion from type `U` to `T`, returning a `Result`. ```rust type Error = Infallible ``` ```rust fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### StructuralPartialEq for TrySendError Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TrySendError.html Provides structural partial equality for `TrySendError`. ```rust impl StructuralPartialEq for TrySendError ``` -------------------------------- ### Receiver::is_full Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Checks if the channel is currently full. ```APIDOC ## Receiver::is_full ### Description Returns `true` if the channel is full. ### Method `receiver.is_full()` ### Parameters None ### Response - `bool`: `true` if the channel is full, `false` otherwise. ### Example ```rust use async_broadcast::broadcast; let (s, r) = broadcast(1); assert!(!s.is_full()); s.broadcast(1).await; assert!(s.is_full()); ``` ``` -------------------------------- ### Any for T Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Blanket implementation of the Any trait for any type T. ```APIDOC ## fn type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method `type_id` ### Parameters - `&self`: An immutable reference to the value. ### Returns - `TypeId`: The unique identifier for the type of `self`. ``` -------------------------------- ### Into for T Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Blanket implementation of the Into trait for any type T. ```APIDOC ## fn into(self) -> U ### Description Calls `U::from(self)`. This conversion is whatever the implementation of `From for U` chooses to do. ### Method `into` ### Parameters - `self`: The value to convert. ### Returns - `U`: The converted value. ``` -------------------------------- ### broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast Creates a new broadcast channel with a specified buffer capacity. The channel allows multiple producers and consumers, and each message is cloned for every active receiver. The channel can only broadcast types that implement `Clone`. ```APIDOC ## broadcast ### Description Creates a new broadcast channel. ### Parameters #### Path Parameters - **capacity** (usize) - Required - The buffer capacity of the channel. ### Returns - `(Sender, Receiver)` - A tuple containing the sender and receiver halves of the channel. ``` -------------------------------- ### From for T Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Blanket implementation of the From trait for any type T. ```APIDOC ## fn from(t: T) -> T ### Description Returns the argument unchanged. ### Method `from` ### Parameters - `t`: The value to convert. ### Returns - `T`: The converted value. ``` -------------------------------- ### Receive Message (Direct) - async-broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Receives a message from the channel without pinning the future. This is suitable for direct awaiting. It handles channel emptiness, closure, and missed messages. ```rust use async_broadcast::{broadcast, RecvError}; let (s, mut r1) = broadcast(1); let mut r2 = r1.clone(); assert_eq!(s.broadcast(1).await, Ok(None)); drop(s); assert_eq!(r1.recv_direct().await, Ok(1)); assert_eq!(r1.recv_direct().await, Err(RecvError::Closed)); assert_eq!(r2.recv_direct().await, Ok(1)); assert_eq!(r2.recv_direct().await, Err(RecvError::Closed)); ``` -------------------------------- ### Broadcast Direct Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Broadcasts a message on the channel without pinning the future to the heap. ```APIDOC ## broadcast_direct ### Description Broadcasts a message on the channel without pinning the future to the heap. The future returned by this method is not `Unpin` and must be pinned before use. This is the desired behavior if you just `.await` on the future. ### Method `broadcast_direct(&self, msg: T) -> Send<'_, T>` ### Parameters - `msg` (T): The message to broadcast. ### Response - `Ok(None)`: Message broadcast successfully. - `Err(SendError(T))`: Failed to broadcast message (e.g., channel closed or full without overflow). ### Request Example ```rust # use async_broadcast::{broadcast, SendError}; # futures_lite::future::block_on(async { let (s, r) = broadcast(1); assert_eq!(s.broadcast_direct(1).await, Ok(None)); drop(r); assert_eq!(s.broadcast_direct(2).await, Err(SendError(2))); # }); ``` ``` -------------------------------- ### Clone for Receiver Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Provides the ability to clone a Receiver, creating a new receiver that shares the same queued messages. ```APIDOC ## impl Clone for Receiver ### Description Produce a clone of this Receiver that has the same messages queued. ### Method `clone` ### Parameters None ### Response - `Self`: A cloned `Receiver` instance. ### Examples ```rust use async_broadcast::{broadcast, RecvError}; let (s, mut r1) = broadcast(1); assert_eq!(s.broadcast(1).await, Ok(None)); drop(s); let mut r2 = r1.clone(); assert_eq!(r1.recv().await, Ok(1)); assert_eq!(r1.recv().await, Err(RecvError::Closed)); assert_eq!(r2.recv().await, Ok(1)); assert_eq!(r2.recv().await, Err(RecvError::Closed)); ``` ``` -------------------------------- ### TryFrom for T Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Blanket implementation of the TryFrom trait for any type T. ```APIDOC ## fn try_from(value: U) -> Result>::Error> ### Description Performs the conversion. ### Method `try_from` ### Parameters - `value`: The value to convert. ### Returns - `Result>::Error>`: Ok(T) if the conversion was successful, or an error if it failed. ``` -------------------------------- ### SendError Error::source Implementation Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.SendError.html Returns the lower-level source of the error, if any. ```rust fn source(&self) -> Option<&(dyn Error + 'static)> ``` -------------------------------- ### Check if the channel is full Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns `true` if the channel has reached its capacity and cannot hold more messages. This is a snapshot in time. ```rust # futures_lite::future::block_on(async { use async_broadcast::broadcast; let (s, r) = broadcast(1); assert!(!s.is_full()); s.broadcast(1).await; assert!(s.is_full()); # }); ``` -------------------------------- ### broadcast Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Creates a new broadcast channel with a specified capacity. The channel can hold at most `cap` messages. ```APIDOC ## broadcast(cap: usize) -> (Sender, Receiver) ### Description Creates a new broadcast channel. The created channel has space to hold at most `cap` messages at a time. ### Panics Capacity must be a positive number. If `cap` is zero, this function will panic. ### Examples ```rust # futures_lite::future::block_on(async { use async_broadcast::{broadcast, TryRecvError, TrySendError}; let (s, mut r1) = broadcast(1); let mut r2 = r1.clone(); assert_eq!(s.broadcast(10).await, Ok(None)); assert_eq!(s.try_broadcast(20), Err(TrySendError::Full(20))); assert_eq!(r1.recv().await, Ok(10)); assert_eq!(r2.recv().await, Ok(10)); assert_eq!(r1.try_recv(), Err(TryRecvError::Empty)); assert_eq!(r2.try_recv(), Err(TryRecvError::Empty)); # }); ``` ``` -------------------------------- ### Receive Message Directly (Unpinned) Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Receives a message from the channel without pinning the future to the heap. This is suitable for direct `.await` usage. The returned future is not `Unpin` and must be pinned if used in other contexts. ```rust pub fn recv_direct(&mut self) -> Recv<'_, T> { Recv::_new(RecvInner { receiver: self, listener: None, _pin: PhantomPinned, }) } ``` ```rust # futures_lite::future::block_on(async { use async_broadcast::{broadcast, RecvError}; let (s, mut r1) = broadcast(1); let mut r2 = r1.clone(); assert_eq!(s.broadcast(1).await, Ok(None)); drop(s); assert_eq!(r1.recv_direct().await, Ok(1)); assert_eq!(r1.recv_direct().await, Err(RecvError::Closed)); assert_eq!(r2.recv_direct().await, Ok(1)); assert_eq!(r2.recv_direct().await, Err(RecvError::Closed)); # }); ``` -------------------------------- ### broadcast Function Source: https://docs.rs/async-broadcast/latest/index.html Creates a new broadcast channel with a specified buffer capacity. Returns a sender and a receiver pair. ```APIDOC ## broadcast(capacity: usize) -> (Sender, Receiver) ### Description Creates a new broadcast channel with a specified buffer capacity. Each message sent on the channel will be cloned and delivered to all active receivers. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Returns A tuple containing the `Sender` and `Receiver` sides of the broadcast channel. ### Example ```rust use async_broadcast::broadcast; let (sender, receiver) = broadcast(10).await; ``` ``` -------------------------------- ### TrySendError Methods Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TrySendError.html Provides methods to inspect the TrySendError, such as unwrapping the inner value or checking the error condition. ```APIDOC ## impl TrySendError ### Methods * **`fn into_inner(self) -> T`**: Unwraps the message that couldn’t be sent. * **`fn is_full(&self) -> bool`**: Returns `true` if the channel is full but not closed. * **`fn is_closed(&self) -> bool`**: Returns `true` if the channel is closed. * **`fn is_disconnected(&self) -> bool`**: Returns `true` if there are currently no active receivers, only inactive ones. ``` -------------------------------- ### len Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns the number of messages currently in the channel. ```APIDOC ## len ### Description Returns the number of messages in the channel. ### Method `len() -> usize` ``` -------------------------------- ### broadcast_direct Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Broadcasts a message on the channel without pinning the future. ```APIDOC ## broadcast_direct ### Description Broadcasts a message on the channel without pinning the future to the heap. The future returned by this method is not `Unpin` and must be pinned before use. ### Method `&self.broadcast_direct(msg: T) -> Send<'_, T>` ``` -------------------------------- ### new_sender Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Produces a new Sender for this channel. This does not re-open the channel if it was closed due to all senders being dropped. ```APIDOC ## pub fn new_sender(&self) -> Sender ### Description Produce a new Sender for this channel. This will not re-open the channel if it was closed due to all senders being dropped. ### Method `new_sender` ### Parameters None ### Response - `Sender`: A new sender instance for the channel. ### Examples ```rust use async_broadcast::{broadcast, RecvError}; let (s1, mut r) = broadcast(2); assert_eq!(s1.broadcast(1).await, Ok(None)); let mut s2 = r.new_sender(); assert_eq!(s2.broadcast(2).await, Ok(None)); drop(s1); drop(s2); assert_eq!(r.recv().await, Ok(1)); assert_eq!(r.recv().await, Ok(2)); assert_eq!(r.recv().await, Err(RecvError::Closed)); ``` ``` -------------------------------- ### recv_direct Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Receives a message from the channel without pinning the future to the heap. The returned future is not `Unpin`. ```APIDOC ## recv_direct ### Description Receives a message from the channel without pinning the future to the heap. The future returned by this method is not `Unpin` and must be pinned before use. This is the desired behavior if you just `.await` on the future. For other uses cases, use the [`recv`] method instead. ### Method `recv_direct(&mut self) -> Recv<'_, T>` ### Parameters None ### Response - `Recv<'_, T>`: A future that resolves to a `Result`. ``` -------------------------------- ### Receiver Methods Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Methods available on the Receiver struct for interacting with the stream. ```APIDOC ## fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll> ### Description Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning `None` if the stream is exhausted. ### Method `poll_next` ### Parameters - `self`: A mutable reference to the pinned `Receiver`. - `cx`: A mutable reference to the `Context` for the current task. ### Returns A `Poll` indicating whether an item is available, or if the stream is exhausted. ``` ```APIDOC ## fn size_hint(&self) -> (usize, Option) ### Description Returns the bounds on the remaining length of the stream. ### Method `size_hint` ### Parameters - `self`: An immutable reference to the `Receiver`. ### Returns A tuple containing the lower bound and an optional upper bound on the number of elements remaining in the stream. ``` -------------------------------- ### Clone TryRecvError Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TryRecvError.html Implementations for `Clone` allow creating a copy of a `TryRecvError` value. ```rust fn clone(&self) -> TryRecvError ``` ```rust fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### len Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Returns the number of messages currently in the broadcast channel. ```APIDOC ## len ### Description Returns the number of messages currently in the broadcast channel. ### Method `len(&self) -> usize` ### Examples ```rust use async_broadcast::broadcast; let (s, r) = broadcast(2); assert_eq!(s.len(), 0); s.broadcast(1).await; s.broadcast(2).await; assert_eq!(s.len(), 2); ``` ``` -------------------------------- ### Check if Channel is Full Source: https://docs.rs/async-broadcast/latest/async_broadcast/enum.TrySendError.html Returns `true` if the error indicates that the channel is full but not closed. Use this to differentiate between a full channel and a closed one. ```rust pub fn is_full(&self) -> bool ``` -------------------------------- ### Borrow for T Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Blanket implementation of the Borrow trait for any type T. ```APIDOC ## fn borrow(&self) -> &T ### Description Immutably borrows from an owned value. ### Method `borrow` ### Parameters - `&self`: An immutable reference to the owned value. ### Returns - `&T`: An immutable reference to the borrowed value. ``` -------------------------------- ### Sender::capacity Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Returns the current capacity of the broadcast channel. ```APIDOC ## Sender::capacity ### Description Returns the channel capacity. ### Method `capacity(&self) -> usize` ### Parameters None ### Returns `usize` - The capacity of the channel. ### Example ```rust use async_broadcast::broadcast; let (s, r) = broadcast::(5); assert_eq!(s.capacity(), 5); ``` ``` -------------------------------- ### Check Await Active Setting Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Returns whether the sender will wait for active receivers to acknowledge a broadcast. If `false`, `Send` operations may fail immediately if no receivers are active. ```rust use async_broadcast::broadcast; let (_, r) = broadcast::(5); assert!(r.await_active()); ``` -------------------------------- ### Receive Message (Pinned) - async-broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Receives a message from the channel, waiting if necessary. This method returns a pinned future. It handles cases where the channel is empty, closed, or if a message was missed due to overflow. ```rust use async_broadcast::{broadcast, RecvError}; let (s, mut r1) = broadcast(1); let mut r2 = r1.clone(); assert_eq!(s.broadcast(1).await, Ok(None)); drop(s); assert_eq!(r1.recv().await, Ok(1)); assert_eq!(r1.recv().await, Err(RecvError::Closed)); assert_eq!(r2.recv().await, Ok(1)); assert_eq!(r2.recv().await, Err(RecvError::Closed)); ``` -------------------------------- ### Activate InactiveReceiver to Receiver Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Demonstrates converting an `InactiveReceiver` back into an active `Receiver`. This is useful when a channel needs to be reactivated after being deactivated. ```rust /// Convert to an activate [`Receiver`]. /// /// Consumes `self`. Use [`InactiveReceiver::activate_cloned`] if you want to keep `self`. /// /// # Examples /// /// ``` /// use async_broadcast::{broadcast, TrySendError}; /// /// let (s, r) = broadcast(1); /// let inactive = r.deactivate(); /// assert_eq!(s.try_broadcast(10), Err(TrySendError::Inactive(10))); /// /// let mut r = inactive.activate(); /// assert_eq!(s.try_broadcast(10), Ok(None)); /// assert_eq!(r.try_recv(), Ok(10)); /// ``` pub fn activate(self) -> Receiver { self.activate_cloned() } ``` -------------------------------- ### Clone for InactiveReceiver Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.InactiveReceiver.html Provides methods for cloning an InactiveReceiver instance. ```APIDOC ## fn clone(&self) -> Self ### Description Returns a duplicate of the value. ### Method `clone` ### Parameters - `&self`: An immutable reference to the `InactiveReceiver` instance. ### Returns - `Self`: A new `InactiveReceiver` instance that is a duplicate of the original. ``` ```APIDOC ## fn clone_from(&mut self, source: &Self) ### Description Performs copy-assignment from `source`. ### Method `clone_from` ### Parameters - `&mut self`: A mutable reference to the `InactiveReceiver` instance to be updated. - `source`: An immutable reference to the source `InactiveReceiver` instance. ``` -------------------------------- ### try_broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Attempts to broadcast a message on the channel without blocking. ```APIDOC ## try_broadcast ### Description Attempts to broadcast a message on the channel. If the channel is full, this method returns an error unless overflow mode is enabled. If the channel is closed, this method returns an error. ### Method `&self.try_broadcast(msg: T) -> Result, TrySendError>` ``` -------------------------------- ### Set Await Active Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Configures whether senders should wait for active receivers. Setting this to `false` can cause `broadcast` to return `Err` immediately if no receivers are present, rather than waiting. ```rust use async_broadcast::broadcast; let (s, mut r) = broadcast::(2); s.broadcast(1).await.unwrap(); r.set_await_active(false); let _ = r.deactivate(); assert!(s.broadcast(2).await.is_err()); ``` -------------------------------- ### Broadcast Message - async-broadcast Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Sender.html Broadcasts a message on the channel. Waits if the channel is full, unless overflow mode is enabled. Returns an error if the channel is closed. ```rust use async_broadcast::{broadcast, SendError}; let (s, r) = broadcast(1); assert_eq!(s.broadcast(1).await, Ok(None)); drop(r); assert_eq!(s.broadcast(2).await, Err(SendError(2))); ``` -------------------------------- ### Broadcast Message Directly (Unpinned Future) Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html Broadcasts a message on the channel without pinning the future to the heap. The returned future is not Unpin and must be pinned before use. ```rust pub fn broadcast_direct(&self, msg: T) -> Send<'_, T> { ``` -------------------------------- ### Sync Implementation for Send Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Send.html Implements the Sync trait for the Send struct, indicating that it is safe to share across threads. ```rust impl<'a, T> Sync for Send<'a, T> where T: Sync + Send, ``` -------------------------------- ### Send Future for Asynchronous Broadcasting Source: https://docs.rs/async-broadcast/latest/src/async_broadcast/lib.rs.html The `Send` future is returned by `Sender::broadcast()`. It facilitates sending a message asynchronously, waiting if the channel is full or inactive. Ensure the future is awaited, as futures do nothing on their own. ```rust easy_wrapper! { /// A future returned by [`Sender::broadcast()`]. #[derive(Debug)] #[must_use = "futures do nothing unless .awaited"] pub struct Send<'a, T: Clone>(SendInner<'a, T> => Result, SendError>); #[cfg(not(target_family = "wasm"))] pub(crate) wait(); } ``` -------------------------------- ### Receiver::capacity Source: https://docs.rs/async-broadcast/latest/async_broadcast/struct.Receiver.html Returns the current capacity of the channel. ```APIDOC ## Receiver::capacity ### Description Returns the channel capacity. ### Method `receiver.capacity()` ### Parameters None ### Response - `usize`: The capacity of the channel. ### Example ```rust use async_broadcast::broadcast; let (_s, r) = broadcast::(5); assert_eq!(r.capacity(), 5); ``` ```