[][src]Trait acteur::Actor

pub trait Actor: Sized + Debug + Send + Sync + 'static {
    type Id: Eq + Hash + Send + Sync + Clone + Debug;
#[must_use]    fn activate<'life0, 'async_trait>(
        id: Self::Id,
        assistant: &'life0 ActorAssistant<Self>
    ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
; #[must_use] fn deactivate<'life0, 'async_trait>(
        &'life0 mut self
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
, { ... } }

The main Trait from this crate.

This Trait enable your structs to be used as actors. You will need to use Handle or Respond Traits in order to accept messages.

Actors are required to have an Id which type is defined by the developer. The constrains for such Id are Eq + Hash + Send + Sync + Clone + Debug

use acteur::{Actor, ActorAssistant};
use async_trait::async_trait;

// You can use any normal struct as an actor. It will contain the actor state. No Arc/Mutex
// is required as only one message per instance (different Id) will be handled.
#[derive(Debug)]
struct Employee {
    id: u32,
    salary: u32,
}

#[async_trait]
impl Actor for Employee {
    type Id = u32;

    // You can use or not the actor Id, still, it will be kept by the framework.
    // This method allows you to acquire any resource you need and save it.
    async fn activate(id: Self::Id, _: &ActorAssistant<Self>) -> Self {
        println!("Employee {:?} activated!", id);
        Employee {
            id,
            salary: 0 //Load from DB, set a default, etc
        }
    }

    // This method is optional and allows you to delete resources, close sockets, etc.
    async fn deactivate(&mut self) {
        println!("Employee {:?} deactivated!", self.id);
    }
}

Associated Types

type Id: Eq + Hash + Send + Sync + Clone + Debug

The Id type for the actor. This Id will be used to identify the actor internally.

Loading content...

Required methods

#[must_use]fn activate<'life0, 'async_trait>(
    id: Self::Id,
    assistant: &'life0 ActorAssistant<Self>
) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>> where
    'life0: 'async_trait,
    Self: 'async_trait, 

This method will be called automatically when the actor is activated. Normally, actors are activated when the first message is received.

Loading content...

Provided methods

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

This method will be called when the framework decided to unload the actor. The concrete algorithms to decide that can change in the future. As for now, this method is never called and actors are never unloaded.

Loading content...

Implementors

Loading content...