use std::borrow::Cow;
use crate::{error::Result, lifecycle::LifeCycle, Addr, Context};
pub trait Message: 'static + Send {
type Result: 'static + Send;
}
#[async_trait::async_trait]
pub trait Handler<T: Message>: Actor
where
Self: std::marker::Sized,
{
async fn handle(&mut self, ctx: &mut Context<Self>, msg: T) -> T::Result;
}
#[async_trait::async_trait]
#[allow(unused_variables)]
pub trait StreamHandler<T: 'static>: Actor {
async fn handle(&mut self, ctx: &mut Context<Self>, msg: T);
async fn started(&mut self, ctx: &mut Context<Self>) {}
async fn finished(&mut self, ctx: &mut Context<Self>) {
ctx.stop(None);
}
}
#[async_trait::async_trait]
#[allow(unused_variables)]
pub trait Actor: Sized + Send + 'static {
const NAME: &'static str = "hannibal::Actor";
async fn started(&mut self, ctx: &mut Context<Self>) -> Result<()> {
Ok(())
}
async fn stopped(&mut self, ctx: &mut Context<Self>) {}
async fn start_default() -> Result<Addr<Self>>
where
Self: Default,
{
Ok(Self::default().start().await?)
}
async fn start(self) -> Result<Addr<Self>> {
LifeCycle::new().start_actor(self).await
}
fn name(&self) -> Cow<'static, str> {
let x = Cow::from(Self::NAME);
x
}
}