pub struct Address<A, Rc = Strong>where
A: Actor,
Rc: RefCounter,{ /* private fields */ }Expand description
An Address is a reference to an actor through which Messages can be
sent. It can be cloned to create more addresses to the same actor.
By default (i.e without specifying the second type parameter, Rc, to be
weak), Addresses are strong. Therefore, when all Addresses
are dropped, the actor will be stopped. In other words, any existing Addresses will inhibit
the dropping of an actor. If this is undesirable, then a WeakAddress
should be used instead. An address is created by calling the
Actor::create or
Context::run methods, or by cloning another Address.
Implementations§
Source§impl<A> Address<A>where
A: Actor,
Functions which apply only to strong addresses (the default kind).
impl<A> Address<A>where
A: Actor,
Functions which apply only to strong addresses (the default kind).
Source§impl<A> Address<A, Either>where
A: Actor,
Functions which apply only to addresses which can either be strong or weak.
impl<A> Address<A, Either>where
A: Actor,
Functions which apply only to addresses which can either be strong or weak.
Source§impl<A, Rc> Address<A, Rc>where
A: Actor,
Rc: RefCounter,
Functions which apply to any kind of address, be they strong or weak.
impl<A, Rc> Address<A, Rc>where
A: Actor,
Rc: RefCounter,
Functions which apply to any kind of address, be they strong or weak.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns whether the actor referred to by this address is running and accepting messages.
struct Shutdown;
impl Message for Shutdown {
type Result = ();
}
#[async_trait::async_trait]
impl Handler<Shutdown> for MyActor {
async fn handle(&mut self, _: Shutdown, ctx: &mut Context<Self>) {
ctx.stop();
}
}
smol::block_on(async {
let addr = MyActor.create(None).spawn(&mut Smol::Global);
assert!(addr.is_connected());
addr.send(Shutdown).await;
Timer::after(Duration::from_secs(1)).await; // Give it time to shut down
assert!(!addr.is_connected());
})Sourcepub fn as_either(&self) -> Address<A, Either>
pub fn as_either(&self) -> Address<A, Either>
Convert this address into a generic address which can be weak or strong.
Sourcepub fn do_send<M>(&self, message: M) -> Result<(), Disconnected>
pub fn do_send<M>(&self, message: M) -> Result<(), Disconnected>
Send a Message to the actor without waiting for a response.
If the actor’s mailbox is full, it will block. 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.
Sourcepub fn do_send_async<M>(&self, message: M) -> DoSendFuture<A>
pub fn do_send_async<M>(&self, message: M) -> DoSendFuture<A>
Send a Message to the actor without waiting for a response.
If the actor’s mailbox is full, it will asynchronously wait. 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.
Sourcepub fn send<M>(&self, message: M) -> SendFuture<A, M>
pub fn send<M>(&self, message: M) -> SendFuture<A, M>
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. Like most
futures, this must be polled to actually send the message.
Sourcepub async fn attach_stream<S, M, K>(self, stream: S)
pub async fn attach_stream<S, M, K>(self, stream: S)
Attaches a stream to this actor 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 and checking when to stop forwarding.
Often, this should be spawned onto an executor to run in the background. Do not await this inside of an actor - this will cause it to await forever and never receive any messages.
Note: if this stream’s continuation should prevent the actor from being dropped, this
method should be called on Address. Otherwise, it should be called
on WeakAddress.
Sourcepub fn into_sink(self) -> AddressSink<A, Rc>
pub fn into_sink(self) -> AddressSink<A, Rc>
Converts this address into a futures Sink.
Trait Implementations§
Source§impl<A, M, Rc> MessageChannel<M> for Address<A, Rc>
impl<A, M, Rc> MessageChannel<M> for Address<A, Rc>
Source§fn is_connected(&self) -> bool
fn is_connected(&self) -> bool
Source§fn do_send(&self, message: M) -> Result<(), Disconnected>
fn do_send(&self, message: M) -> Result<(), Disconnected>
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.Source§fn send(&self, message: M) -> SendFuture<M>
fn send(&self, message: M) -> SendFuture<M>
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.Source§fn attach_stream(
self,
stream: Pin<Box<dyn Stream<Item = M> + Send + '_>>,
) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>
fn attach_stream( self, stream: Pin<Box<dyn Stream<Item = M> + Send + '_>>, ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>
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). Read moreSource§fn clone_channel(&self) -> Box<dyn MessageChannel<M>>
fn clone_channel(&self) -> Box<dyn MessageChannel<M>>
Source§fn sink(&self) -> Box<dyn MessageSink<M, Error = Disconnected>>
fn sink(&self) -> Box<dyn MessageSink<M, Error = Disconnected>>
Sink
and asynchronously send messages through it.Source§impl<M> PublishExt<M> for Address<Broker<M>>
impl<M> PublishExt<M> for Address<Broker<M>>
Source§impl<A, M> StrongMessageChannel<M> for Address<A>
impl<A, M> StrongMessageChannel<M> for Address<A>
Source§fn upcast(self) -> Box<dyn MessageChannel<M>>
fn upcast(self) -> Box<dyn MessageChannel<M>>
Upcasts this strong message channel into a boxed generic
MessageChannel trait object
Source§fn upcast_ref(&self) -> &dyn MessageChannel<M>
fn upcast_ref(&self) -> &dyn MessageChannel<M>
Upcasts this strong message channel into a reference to the generic
MessageChannel trait object
Source§fn downgrade(&self) -> Box<dyn WeakMessageChannel<M>>
fn downgrade(&self) -> Box<dyn WeakMessageChannel<M>>
Source§fn clone_channel(&self) -> Box<dyn StrongMessageChannel<M>>
fn clone_channel(&self) -> Box<dyn StrongMessageChannel<M>>
Source§fn sink(&self) -> Box<dyn StrongMessageSink<M, Error = Disconnected>>
fn sink(&self) -> Box<dyn StrongMessageSink<M, Error = Disconnected>>
Sink
and asynchronously send messages through it.Source§impl<T, M> SubscribeExt<M> for Address<T>
impl<T, M> SubscribeExt<M> for Address<T>
Source§impl<A, M> WeakMessageChannel<M> for Address<A, Weak>
impl<A, M> WeakMessageChannel<M> for Address<A, Weak>
Source§fn upcast(self) -> Box<dyn MessageChannel<M>>
fn upcast(self) -> Box<dyn MessageChannel<M>>
Upcasts this weak message channel into a boxed generic
MessageChannel trait object
Source§fn upcast_ref(&self) -> &dyn MessageChannel<M>
fn upcast_ref(&self) -> &dyn MessageChannel<M>
Upcasts this weak message channel into a reference to the generic
MessageChannel trait object
Source§fn clone_channel(&self) -> Box<dyn WeakMessageChannel<M>>
fn clone_channel(&self) -> Box<dyn WeakMessageChannel<M>>
Source§fn sink(&self) -> Box<dyn WeakMessageSink<M, Error = Disconnected>>
fn sink(&self) -> Box<dyn WeakMessageSink<M, Error = Disconnected>>
Sink
and asynchronously send messages through it.