use acteur::{Acteur, Actor, ActorAssistant, Receive, Respond};
use async_trait::async_trait;
#[derive(Debug)]
struct Employee {
id: u32,
salary: u32,
}
#[async_trait]
impl Actor for Employee {
type Id = u32;
async fn activate(id: Self::Id, _: &ActorAssistant<Self>) -> Self {
println!("Employee {:?} activated!", id);
Employee {
id,
salary: 0, }
}
async fn deactivate(&mut self) {
println!("Employee {:?} deactivated!", self.id);
}
}
#[derive(Debug)]
struct SalaryChanged(u32);
#[async_trait]
impl Respond<SalaryChanged> for Employee {
type Response = String;
async fn handle(
&mut self,
message: SalaryChanged,
assistant: &ActorAssistant<Employee>,
) -> String {
assistant
.send_to_actor::<Employee, SalaryChanged>(self.id, message)
.await;
String::from("Thanks!")
}
}
#[async_trait]
impl Receive<SalaryChanged> for Employee {
async fn handle(&mut self, message: SalaryChanged, _: &ActorAssistant<Employee>) {
self.salary = message.0;
}
}
fn main() {
let sys = Acteur::new();
let response = sys.call_actor_sync::<Employee, SalaryChanged>(42, SalaryChanged(55000));
println!("Response is: {:?}", response);
sys.stop();
sys.wait_until_stopped();
}