[][src]Trait acteur::Serve

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

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

If you don't need to respond messages, use the Notify trait.

This trait is compatible with Notify trait as you can implement, for the same message, both traits. This trait will be executed when using the "notify" or "notify_sync" method from Acteur or the "notify" method from Assistant

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

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

#[async_trait::async_trait]
impl Service for EmployeeTaxesCalculator {
    async fn initialize() -> (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) -> 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();
}

Associated Types

Loading content...

Required methods

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

Loading content...

Implementors

Loading content...