[][src]Trait acteur::Receive

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

This Trait allow Actors to receive messages.

If you want to respond to messages, use the Respond trait.

This trait is compatible with Respond trait as you can implement, for the same message, both traits. This trait will be executed when using the "send" or "send_sync" method from System or the "send" method from Assistant.

use async_trait::async_trait;
use acteur::{Assistant, Receive, Acteur};

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

#[async_trait]
impl Receive<SalaryChanged> for Employee {
    async fn handle(&mut self, message: SalaryChanged, _: &Assistant<Employee>) {
        self.salary = message.0;
    }
}

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

    sys.send_sync::<Employee, SalaryChanged>(42, SalaryChanged(55000));

    sys.wait_until_stopped();
}

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

Required methods

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

This method is called each time a message is received. You can use the Assistant to send messages

Loading content...

Implementors

Loading content...