[][src]Trait acteur::Service

pub trait Service: Sized + Send + Sync + Debug + 'static {
#[must_use]    pub fn initialize<'life0, 'async_trait>(
        system: &'life0 ServiceAssistant<Self>
    ) -> Pin<Box<dyn Future<Output = (Self, ServiceConfiguration)> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
; }

Services are id-less actors that can process messages with certain concurrency. The concurrency level can be configured during the "initialize" method with the ServiceConfiguration struct.

use acteur::{Acteur, Service, Serve, ServiceAssistant, ServiceConfiguration};

#[derive(Debug)]
struct EmployeeTaxesCalculator {
    tax_rate: f32,
}

#[async_trait::async_trait]
impl Service for EmployeeTaxesCalculator {
    async fn initialize(system: &ServiceAssistant<Self>) -> (Self, ServiceConfiguration) {
        let service = EmployeeTaxesCalculator {
            tax_rate: 0.21,
        };

        let service_conf = ServiceConfiguration::default();

        (service, service_conf)
    }
}

#[derive(Debug)]
struct EmployeeSalaryChange(f32);

#[async_trait::async_trait]
impl Serve<EmployeeSalaryChange> for EmployeeTaxesCalculator {
    type Response = f32;

    async fn handle(&self, message: EmployeeSalaryChange, system: &ServiceAssistant<Self>) -> f32 {
        self.tax_rate * message.0
    }
}

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

    let taxes = sys.call_service_sync::<EmployeeTaxesCalculator, _>(EmployeeSalaryChange(55000.0)).unwrap();

    println!("Employee taxes are: {:?}", taxes);

    sys.stop();

    sys.wait_until_stopped();
}

Required methods

#[must_use]pub fn initialize<'life0, 'async_trait>(
    system: &'life0 ServiceAssistant<Self>
) -> Pin<Box<dyn Future<Output = (Self, ServiceConfiguration)> + Send + 'async_trait>> where
    'life0: 'async_trait,
    Self: 'async_trait, 
[src]

Loading content...

Implementors

Loading content...