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§
Source§impl<A> Address<A>
impl<A> Address<A>
Sourcepub async fn send<IN>(&mut self, message: IN) -> Result<A::Result, SendError>
pub async fn send<IN>(&mut self, message: IN) -> Result<A::Result, SendError>
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.
Sourcepub async fn notify<IN>(&mut self, message: IN) -> Result<(), SendError>
pub async fn notify<IN>(&mut self, message: IN) -> Result<(), SendError>
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.
Sourcepub async fn into_stream_forwarder<IN, S>(
self,
stream: S,
) -> Result<(), SendError>
pub async fn into_stream_forwarder<IN, S>( self, stream: S, ) -> Result<(), SendError>
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.
Sourcepub async fn stop(&mut self)
pub async fn stop(&mut self)
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.
Sourcepub async fn wait_for_stop(&self)
pub async fn wait_for_stop(&self)
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.
Source§impl<A> Address<A>
impl<A> Address<A>
Sourcepub fn spawn_stream_forwarder<IN, S>(
self,
stream: S,
) -> JoinHandle<Result<(), SendError>> ⓘ
Available on crate features runtime-tokio or runtime-async-std only.
pub fn spawn_stream_forwarder<IN, S>( self, stream: S, ) -> JoinHandle<Result<(), SendError>> ⓘ
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.
Sourcepub async fn calculate<IN>(&self, message: IN) -> Result<A::Result, SendError>
Available on crate features runtime-tokio or runtime-async-std only.
pub async fn calculate<IN>(&self, message: IN) -> Result<A::Result, SendError>
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);
}