pub struct Address<A> { /* private fields */ }
Expand description

Address is an object used to communicate with Actors.

Assuming that Actor is capable of processing messages of a certain type, the Address can be used to interact with Actor by using either Address::send (for messages) or Address::notify (for notifications).

Implementations

Sends a message to the Actor and receives the response.

Examples

This example assumes that messages is used with rt-tokio feature enabled.


struct Sum;

#[async_trait]
impl Actor for Sum {}

#[async_trait]
impl Handler<(u8, u8)> for Sum {
    type Result = u16;
    // Implementation omitted.
}

#[tokio::main]
async fn main() {
   let mut addr = Sum.spawn();
   let result = addr.send((22, 20)).await.unwrap();
   assert_eq!(result, 42);
}
Errors

Will return an error in case associated actor stopped working.

Sends a notification to the Actor without receiving any kind of response.

Examples

This example assumes that messages is used with rt-tokio feature enabled.


struct Ping;

#[async_trait]
impl Actor for Ping {}

#[async_trait]
impl Notifiable<u8> for Ping {
    async fn notify(&mut self, input: u8, context: &Context<Self>) {
        println!("Received number {}", input);
    }
}

#[tokio::main]
async fn main() {
   let mut addr = Ping.spawn();
   addr.notify(42).await.unwrap();
}
Errors

Will return an error in case associated actor stopped working.

Combines provided stream and this Address object, returning a future that will run while stream yields messages and send them to the server.

Actor associated with this Address must implmenet Notifiable trait to process messages from the stream.

Future returned by this method should not normally be directly awaited, but rather is expected to be used in some kind of spawn function of the used runtime (e.g. tokio::spawn or async_std::task::spawn).

Errors

Will return an error in case associated actor stopped working.

Returns true if Address is still connected to the Actor.

Sends a stop request to the corresponding Actor.

Sending this message does not mean that actor will be stopped immediately. In order to make sure that the actor is stopped, Address::wait_for_stop should be used.

Does nothing if address is disconnected from the actor or actor already has been stopped.

Creates a future that waits for actor to be fully stopped.

Note that this method does not request an actor to stop, it only waits for it in order to stop actor, Address::stop should be used.

This is supported on crate features runtime-tokio or runtime-async-std only.

Version of Address::into_stream_forwarder that automatically spawns the future.

Returned future is the join handle of the spawned task, e.g. it can be awaited if the user is interested in the moment when the stream stopped sending messages.

This is supported on crate features runtime-tokio or runtime-async-std only.

Sends a message to the Actor and receives the response. Unlike in Address::send, calculate supports parallel execution.

Examples

This example assumes that messages is used with rt-tokio feature enabled.

#[derive(Clone)]
struct Sum;

#[async_trait]
impl Actor for Sum {}

#[async_trait]
impl Coroutine<(u8, u8)> for Sum {
    type Result = u16;
    async fn calculate(self, (a, b): (u8, u8)) -> u16 {
        (a as u16) + (b as u16)
    }
}

#[tokio::main]
async fn main() {
   let mut addr = Sum.spawn();
   let result = addr.calculate((22, 20)).await.unwrap();
   assert_eq!(result, 42);
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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.