Struct Address

Source
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>

Source

pub async fn send<IN>(&mut self, message: IN) -> Result<A::Result, SendError>
where A: Actor + Send + Handler<IN> + 'static, IN: Send + 'static, A::Result: Send + Sync + 'static,

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.

Source

pub async fn notify<IN>(&mut self, message: IN) -> Result<(), SendError>
where A: Actor + Send + Notifiable<IN> + 'static, IN: Send + 'static,

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.

Source

pub async fn into_stream_forwarder<IN, S>( self, stream: S, ) -> Result<(), SendError>
where A: Actor + Send + Notifiable<IN> + 'static, S: Send + Stream<Item = IN> + Unpin, IN: Send + 'static,

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.

Source

pub fn connected(&self) -> bool

Returns true if Address is still connected to the Actor.

Source

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.

Source

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>

Source

pub fn spawn_stream_forwarder<IN, S>( self, stream: S, ) -> JoinHandle<Result<(), SendError>>
where A: Actor + Send + Notifiable<IN> + 'static, S: Send + Stream<Item = IN> + Unpin + 'static, IN: Send + 'static,

Available 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.

Source

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,

Available 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§

Source§

impl<A> Clone for Address<A>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A> Debug for Address<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> Freeze for Address<A>

§

impl<A> !RefUnwindSafe for Address<A>

§

impl<A> Send for Address<A>

§

impl<A> Sync for Address<A>

§

impl<A> Unpin for Address<A>

§

impl<A> !UnwindSafe for Address<A>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.