[][src]Trait acteur::Handle

pub trait Handle<M: Debug> where
    Self: Sized + Actor
{ 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.

Following the example in the Actor documentation...

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

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

#[async_trait]
impl Handle<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();
}

This actor will send messages counting until one million and then stop the system.

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

Note: You usually don't call stop_system in there, as it will stop the whole actor framework.

Required methods

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