use std::future::Future;
use crate::{context::Ctx, message::AskResult, ActorRef, Message};
pub enum Scheduler {
Blocking,
NonBlocking,
}
pub trait Actor: Send + Sync + Sized + 'static {
fn name() -> &'static str {
std::any::type_name::<Self>()
}
fn max_anonymous_actors() -> usize {
usize::MAX >> 3
}
fn mailbox_size() -> usize {
6
}
fn start(self) -> ActorRef<Self>
where
Self: Actor,
{
Ctx::new().run(self)
}
fn on_start(&mut self, _: &mut Ctx<Self>)
where
Self: Actor,
{
}
fn pre_run(&mut self, _: &mut Ctx<Self>) {}
fn post_run(&mut self, _: &mut Ctx<Self>) {}
fn on_stopping(&mut self, _: &mut Ctx<Self>) {}
fn on_stopped(&mut self, _: &mut Ctx<Self>) {}
fn on_end(&mut self, _: &mut Ctx<Self>) {}
}
pub trait InternalHandler<M: Message>: Actor {
fn private_handler(&mut self, message: M, context: &mut Ctx<Self>);
}
pub trait Handler<M: Message>: Actor {
fn handle(&mut self, message: M, context: &mut Ctx<Self>);
fn scheduler() -> Scheduler {
Scheduler::NonBlocking
}
}
pub trait Ask<M: Message>: Actor {
type Result: Message;
fn handle(&mut self, message: M, context: &mut Ctx<Self>) -> AskResult<Self::Result>;
fn scheduler() -> Scheduler {
Scheduler::NonBlocking
}
}
pub trait AsyncAsk<M: Message>: Actor {
type Output: Message;
type Future<'a>: Future<Output = Self::Output> + Send + Sync + 'a;
fn handle<'a>(&'a mut self, message: M, context: &mut Ctx<Self>) -> Self::Future<'a>;
fn scheduler() -> Scheduler {
Scheduler::NonBlocking
}
}