Enum EventReceiver

Source
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

Source

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 received
  • None if 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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.