use messages::prelude::*;
#[derive(Debug, Default)]
pub struct Service {
value: u64,
}
#[async_trait]
impl Actor for Service {
async fn started(&mut self) {
println!("Service was started");
}
async fn stopping(&mut self) -> ActorAction {
println!("Service is stopping");
ActorAction::Stop
}
fn stopped(&mut self) {
println!("Service has stopped");
}
}
#[derive(Debug)]
pub struct Request(pub u64);
#[async_trait]
impl Handler<Request> for Service {
type Result = u64;
async fn handle(&mut self, input: Request, _: &Context<Self>) -> u64 {
self.value + input.0
}
}
#[derive(Debug)]
pub struct Notification(pub u64);
#[async_trait]
impl Notifiable<Notification> for Service {
async fn notify(&mut self, input: Notification, _: &Context<Self>) {
self.value = input.0;
}
}
impl Service {
pub fn new() -> Self {
Self::default()
}
}
#[tokio::main]
async fn main() {
let mut address = Service::new().spawn();
address.notify(Notification(10)).await.unwrap();
let response: u64 = address.send(Request(1)).await.unwrap();
assert_eq!(response, 11);
address.stop().await;
address.wait_for_stop().await;
assert!(!address.connected());
}