[][src]Struct xtra::MessageChannel

pub struct MessageChannel<M: Message> { /* fields omitted */ }

A message channel is a channel through which you can send only one kind of message, but to any actor that can handle it. It is like Address, but associated with the message type rather than the actor type. Any existing MessageChannels will prevent the dropping of the actor. If this is undesirable, then the WeakMessageChannel struct should be used instead. This struct is created by calling Address::channel, Address::into_channel, or the similar methods on WeakAddress.

Example

struct WhatsYourName;

impl Message for WhatsYourName {
    type Result = &'static str;
}

struct Alice;
struct Bob;

impl Actor for Alice {}
impl Actor for Bob {}

impl SyncHandler<WhatsYourName> for Alice {
    fn handle(&mut self, _: WhatsYourName, _ctx: &mut Context<Self>) -> &'static str {
        "Alice"
    }
}

impl SyncHandler<WhatsYourName> for Bob {
    fn handle(&mut self, _: WhatsYourName, _ctx: &mut Context<Self>) -> &'static str {
        "Bob"
    }
}

#[smol_potat::main]
async fn main() {
    let channels = [Alice.spawn().into_channel(), Bob.spawn().into_channel()];
    let name = ["Alice", "Bob"];

    for (channel, name) in channels.iter().zip(&name) {
        assert_eq!(*name, channel.send(WhatsYourName).await.unwrap());
    }
}

Implementations

impl<M: Message> MessageChannel<M>[src]

pub fn downgrade(&self) -> WeakMessageChannel<M>[src]

Create a weak message channel to the actor. Unlike with the strong variety of channel (this kind), an actor will not be prevented from being dropped if only weak channels exist.

pub fn into_downgraded(self) -> WeakMessageChannel<M>[src]

Converts this message channel into a weak channel to the actor. Unlike with the strong variety of channel (this kind), an actor will not be prevented from being dropped if only weak channels exist.

Trait Implementations

impl<M: Message> MessageChannelExt<M> for MessageChannel<M>[src]

impl<M: Message> Sink<M> for MessageChannel<M>[src]

type Error = Disconnected

The type of value produced by the sink when an error occurs.

Auto Trait Implementations

impl<M> !RefUnwindSafe for MessageChannel<M>

impl<M> Send for MessageChannel<M>

impl<M> !Sync for MessageChannel<M>

impl<M> Unpin for MessageChannel<M>

impl<M> !UnwindSafe for MessageChannel<M>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, Item> SinkExt<Item> for T where
    T: Sink<Item> + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.