pub enum EventReceiver {
Unbounded(UnboundedReceiver<Msg>),
Bounded(Receiver<Msg>),
}Expand description
Event receiver abstraction that can be either bounded or unbounded.
EventReceiver provides a unified interface for receiving messages from
either bounded or unbounded channels. This abstraction allows the framework
to switch between different channel types without changing the API.
§Examples
use bubbletea_rs::event::{EventReceiver, EventSender, Msg};
use tokio::sync::mpsc;
async fn example() {
let (tx, rx) = mpsc::unbounded_channel::<Msg>();
let mut receiver = EventReceiver::Unbounded(rx);
let sender = EventSender::from_unbounded(tx);
// Send and receive a message
sender.send(Box::new(42)).unwrap();
if let Some(msg) = receiver.recv().await {
// Process the message
}
}Variants§
Unbounded(UnboundedReceiver<Msg>)
Unbounded channel receiver counterpart for unlimited-capacity channels.
Bounded(Receiver<Msg>)
Bounded channel receiver that may yield None when closed and drained.
Implementations§
Source§impl EventReceiver
impl EventReceiver
Sourcepub async fn recv(&mut self) -> Option<Msg>
pub async fn recv(&mut self) -> Option<Msg>
Receive the next message from the channel.
Asynchronously waits for the next message from the channel. Returns None
when the sender side has been dropped and all messages have been received.
§Returns
Some(Msg)if a message was receivedNoneif the channel is closed and empty
§Examples
use bubbletea_rs::event::{EventReceiver, Msg};
use tokio::sync::mpsc;
async fn example() {
let (tx, rx) = mpsc::unbounded_channel::<Msg>();
let mut receiver = EventReceiver::Unbounded(rx);
// Send a message
tx.send(Box::new("Hello")).unwrap();
// Receive the message
match receiver.recv().await {
Some(msg) => {
if let Some(text) = msg.downcast_ref::<&str>() {
println!("Received: {}", text);
}
}
None => println!("Channel closed"),
}
}Auto Trait Implementations§
impl Freeze for EventReceiver
impl RefUnwindSafe for EventReceiver
impl Send for EventReceiver
impl Sync for EventReceiver
impl Unpin for EventReceiver
impl UnwindSafe for EventReceiver
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more