[][src]Trait acteur::Respond

pub trait Respond<M: Debug>: Sized + Actor {
    type Response: Send;
#[must_use]    pub fn handle<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        message: M,
        assistant: &'life1 ActorAssistant<Self>
    ) -> Pin<Box<dyn Future<Output = Self::Response> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: 'async_trait
; }

This Trait allow Actors to receive messages and, additionally, respond to them.

This trait is like the Receive trait but additionally allows responding to messages

This trait is compatible with Receive trait as you can implement, for the same message, both traits. This trait will be executed when using the "call_actor" or "call_actor_sync" method from System or the "call_actor" method from ActorAssistant.

Note about concurrency and performance

If there is no reason to respond to a message, prefer to use the Receive trait trait, as this may delay you Actors or even deadlock them. For example:

  1. Actor A-51 calls Actor B-32
  2. Actor B-32 calls Actor A-51

As Actor A-51 is blocked waiting for B-32, this will keep waiting forever.

Additionally, all the time waiting for a response, is time that the Actor instance won't be processing messages. Keep this in mind as if you call to a very busy instance, that may slow down other instance, making the first wait until the busy ones processes all the messages before yours.

use async_trait::async_trait;
use acteur::{ActorAssistant, Respond, Acteur};

#[derive(Debug)]
struct SalaryChanged(u32);

#[async_trait]
impl Respond<SalaryChanged> for Employee {
    type Response = String;
    async fn handle(&mut self, message: SalaryChanged, _: &ActorAssistant<Employee>) -> String {
        self.salary = message.0;
        String::from("Thanks!")
    }
}

fn main() {
    let sys = Acteur::new();

    let response = sys.call_actor_sync::<Employee, SalaryChanged>(42, SalaryChanged(55000));

    println!("Response is: {:?}", response); // Response is: Thanks!

    sys.wait_until_stopped();
}

You can use the ActorAssistant in order to interact with other actors and the system.

Associated Types

Loading content...

Required methods

#[must_use]pub fn handle<'life0, 'life1, 'async_trait>(
    &'life0 mut self,
    message: M,
    assistant: &'life1 ActorAssistant<Self>
) -> Pin<Box<dyn Future<Output = Self::Response> + Send + 'async_trait>> where
    'life0: 'async_trait,
    'life1: 'async_trait,
    Self: 'async_trait, 
[src]

This method is called each time a message is received. You can use the ActorAssistant to send messages from actors, System to send messages from services and Acteur to send messages from outside the framework

Loading content...

Implementors

Loading content...