[][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 ServiceAssistant<Self>
    ) -> 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 Listen trait.

This trait is compatible with Listen 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 ActorAssistant

Keep in mind that if someone waits for this service to respond and this service has a long queue of messages to process, the response can take long time, slowing down who is calling this service. Preffer always to use the Serve trait if you can.

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

#[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, _: &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();
}

Associated Types

Loading content...

Required methods

#[must_use]fn handle<'life0, 'life1, 'async_trait>(
    &'life0 self,
    message: M,
    system: &'life1 ServiceAssistant<Self>
) -> 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...