Struct xtra::address::Address

source ·
pub struct Address<A: Actor, Rc: RefCounter = Strong> { /* 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§

Functions which apply only to strong addresses (the default kind).

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

Functions which apply only to addresses which can either be strong or weak.

Converts this address into a weak address.

Functions which apply to any kind of address, be they strong or weak.

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());
})

Convert this address into a generic address which can be weak or strong.

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.

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.

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.

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.

Converts this address into a futures Sink.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Executes the destructor for this type. Read more
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). Read more
Clones this channel as a boxed trait object.
Use this message channel as a futures Sink and asynchronously send messages through it.

Upcasts this strong message channel into a boxed generic MessageChannel trait object

Upcasts this strong message channel into a reference to the generic MessageChannel trait object

Create a weak message channel. Unlike with the strong variety of message channel (this kind), an actor will not be prevented from being dropped if only weak sinks, channels, and addresses exist.
Clones this channel as a boxed trait object.
Use this message channel as a futures Sink and asynchronously send messages through it.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.