pub trait MessageChannel<M: Message>: Unpin + Send + Sync {
    fn is_connected(&self) -> bool;
    fn do_send(&self, message: M) -> Result<(), Disconnected>;
    fn send(&self, message: M) -> SendFuture<M>Notable traits for SendFuture<M>impl<M: Message> Future for SendFuture<M>    type Output = Result<M::Result, Disconnected>;;
    fn attach_stream(self, stream: BoxStream<'_, M>) -> BoxFuture<'_, ()>
    where
        M::Result: Into<KeepRunning> + Send
; fn clone_channel(&self) -> Box<dyn MessageChannel<M>>; fn sink(&self) -> Box<dyn MessageSink<M>>; }
Expand description

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. This trait represents any kind of message channel. There are two traits which inherit from it - one for weak message channels, and one for strong message channels. Both of these traits may be downcasted to this trait using their respective downcast methods. Therefore, this trait is most useful when you want to be generic over both strong and weak message channels. If this is undesireable or not needed, simply use their respective trait objects instead.

Example

struct WhatsYourName;

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

struct Alice;
struct Bob;

#[async_trait::async_trait]
impl Actor for Alice {
    async fn stopped(&mut self) {
        println!("Oh no");
    }
}
impl Actor for Bob {}

#[async_trait::async_trait]
impl Handler<WhatsYourName> for Alice {
    async fn handle(&mut self, _: WhatsYourName, _ctx: &mut Context<Self>) -> &'static str {
        "Alice"
    }
}

#[async_trait::async_trait]
impl Handler<WhatsYourName> for Bob {
    async fn handle(&mut self, _: WhatsYourName, _ctx: &mut Context<Self>) -> &'static str {
        "Bob"
    }
}

fn main() {
smol::block_on(async {
        let channels: [Box<dyn StrongMessageChannel<WhatsYourName>>; 2] = [
            Box::new(Alice.create(None).spawn(&mut Smol::Global)),
            Box::new(Bob.create(None).spawn(&mut Smol::Global))
        ];
        let name = ["Alice", "Bob"];
        for (channel, name) in channels.iter().zip(&name) {
            assert_eq!(*name, channel.send(WhatsYourName).await.unwrap());
        }
    })
}

Required Methods

Returns whether the actor referred to by this address is running and accepting messages.

Send a Message to the actor without waiting for a response. If this returns Err(Disconnected), then the actor is stopped and not accepting messages. If this returns Ok(()), the will be delivered, but may not be handled in the event that the actor stops itself (by calling Context::stop) before it was handled.

Send a Message to the actor and asynchronously wait for a response. If this returns Err(Disconnected), then the actor is stopped and not accepting messages. This, unlike Address::send will block if the actor’s mailbox is full. If this is undesired, consider using a MessageSink.

Attaches a stream to this channel such that all messages produced by it are forwarded to the actor. This could, for instance, be used to forward messages from a socket to the actor (after the messages have been appropriately mapped). This is a convenience method over explicitly forwarding a stream to this address, spawning that future onto the executor, and mapping the error away (because disconnects are expected and will simply mean that the stream is no longer being forwarded).

Note: if this stream’s continuation should prevent the actor from being dropped, this method should be called on MessageChannel. Otherwise, it should be called on WeakMessageChannel.

Clones this channel as a boxed trait object.

Use this message channel as a futures Sink and asynchronously send messages through it.

Implementors