pub trait Service: Actor + Default {
fn from_registry<'async_trait>(
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>
where
Self: Send + 'async_trait,
{ ... }
}
Expand description
Trait define a global service.
The service is a global actor.
You can use Actor::from_registry
to get the address Addr<A>
of the service.
Examples
use hannibal::*;
#[message(result = "i32")]
struct AddMsg(i32);
#[derive(Default)]
struct MyService(i32);
impl Actor for MyService {}
impl Service for MyService {}
#[async_trait::async_trait]
impl Handler<AddMsg> for MyService {
async fn handle(&mut self, ctx: &mut Context<Self>, msg: AddMsg) -> i32 {
self.0 += msg.0;
self.0
}
}
#[hannibal::main]
async fn main() -> Result<()> {
let mut addr = MyService::from_registry().await?;
assert_eq!(addr.call(AddMsg(1)).await?, 1);
assert_eq!(addr.call(AddMsg(5)).await?, 6);
Ok(())
}