[−][src]Trait xtra_addons::MessageChannel
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
pub fn is_connected(&self) -> bool
[src]
Returns whether the actor referred to by this address is running and accepting messages.
pub fn do_send(&self, message: M) -> Result<(), Disconnected>
[src]
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.
pub fn send(&self, message: M) -> SendFuture<M>
[src]
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
.
pub fn attach_stream(
self,
stream: Pin<Box<dyn Stream<Item = M> + Send, Global>>
) -> Pin<Box<dyn Future<Output = ()> + Send, Global>> where
<M as Message>::Result: Into<KeepRunning>,
<M as Message>::Result: Send,
[src]
self,
stream: Pin<Box<dyn Stream<Item = M> + Send, Global>>
) -> Pin<Box<dyn Future<Output = ()> + Send, Global>> where
<M as Message>::Result: Into<KeepRunning>,
<M as Message>::Result: Send,
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 map
ped). 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
.
pub fn clone_channel(&self) -> Box<dyn MessageChannel<M> + 'static, Global>
[src]
Clones this channel as a boxed trait object.
pub fn sink(
&self
) -> Box<dyn MessageSink<M, Error = Disconnected> + 'static, Global>
[src]
&self
) -> Box<dyn MessageSink<M, Error = Disconnected> + 'static, Global>
Use this message channel as a futures Sink
and asynchronously send messages through it.
Implementors
impl<A, M, Rc> MessageChannel<M> for Address<A, Rc> where
Rc: RefCounter,
A: Handler<M>,
M: Message,
[src]
Rc: RefCounter,
A: Handler<M>,
M: Message,
pub fn is_connected(&self) -> bool
[src]
pub fn do_send(&self, message: M) -> Result<(), Disconnected>
[src]
pub fn send(&self, message: M) -> SendFuture<M>
[src]
pub fn attach_stream(
self,
stream: Pin<Box<dyn Stream<Item = M> + Send, Global>>
) -> Pin<Box<dyn Future<Output = ()> + Send, Global>> where
<M as Message>::Result: Into<KeepRunning>,
<M as Message>::Result: Send,
[src]
self,
stream: Pin<Box<dyn Stream<Item = M> + Send, Global>>
) -> Pin<Box<dyn Future<Output = ()> + Send, Global>> where
<M as Message>::Result: Into<KeepRunning>,
<M as Message>::Result: Send,
pub fn clone_channel(&self) -> Box<dyn MessageChannel<M> + 'static, Global>
[src]
pub fn sink(
&self
) -> Box<dyn MessageSink<M, Error = Disconnected> + 'static, Global>
[src]
&self
) -> Box<dyn MessageSink<M, Error = Disconnected> + 'static, Global>