pub struct Address<A> { /* private fields */ }
Expand description
Address
is an object used to communicate with Actor
s.
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 await
ed,
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.
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.
pub fn spawn_stream_forwarder<IN, S>(
self,
stream: S
) -> JoinHandle<Result<(), SendError>>ⓘNotable traits for JoinHandle<T>impl<T> Future for JoinHandle<T> type Output = Result<T, JoinError>;
where
A: Actor + Send + Notifiable<IN> + 'static,
S: Send + Stream<Item = IN> + Unpin + 'static,
IN: Send + 'static,
This is supported on crate features runtime-tokio
or runtime-async-std
only.
pub fn spawn_stream_forwarder<IN, S>(
self,
stream: S
) -> JoinHandle<Result<(), SendError>>ⓘNotable traits for JoinHandle<T>impl<T> Future for JoinHandle<T> type Output = Result<T, JoinError>;
where
A: Actor + Send + Notifiable<IN> + 'static,
S: Send + Stream<Item = IN> + Unpin + 'static,
IN: Send + 'static,
impl<T> Future for JoinHandle<T> type Output = Result<T, JoinError>;
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.
pub async fn calculate<IN>(&self, message: IN) -> Result<A::Result, SendError> where
A: Actor + Send + Coroutine<IN> + 'static,
IN: Send + 'static,
A::Result: Send + Sync + 'static,
This is supported on crate features runtime-tokio
or runtime-async-std
only.
pub async fn calculate<IN>(&self, message: IN) -> Result<A::Result, SendError> where
A: Actor + Send + Coroutine<IN> + 'static,
IN: Send + 'static,
A::Result: Send + Sync + 'static,
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
Auto Trait Implementations
impl<A> !RefUnwindSafe for Address<A>
impl<A> !UnwindSafe for Address<A>
Blanket Implementations
Mutably borrows from an owned value. Read more