use super::{rt::Rt, service::Service, ActorResult, Channel, SupHandle};
use async_trait::async_trait;
#[async_trait]
pub trait Actor<S>: Sized + Send
where
S: Send,
{
const VERSION: usize = 1;
type Data: Send + 'static;
type Channel: Channel;
async fn init(&mut self, rt: &mut Rt<Self, S>) -> ActorResult<Self::Data>;
async fn run(&mut self, rt: &mut Rt<Self, S>, data: Self::Data) -> ActorResult<()>;
fn type_name() -> &'static str {
std::any::type_name::<Self>()
}
}
#[async_trait::async_trait]
pub trait Shutdown: Send + 'static + Sync + dyn_clone::DynClone {
async fn shutdown(&self);
fn scope_id(&self) -> super::ScopeId;
}
dyn_clone::clone_trait_object!(Shutdown);
pub trait ShutdownEvent: Send {
fn shutdown_event() -> Self;
}
pub struct NullSupervisor;
#[async_trait::async_trait]
impl<T: Send + 'static> SupHandle<T> for NullSupervisor {
type Event = ();
async fn report(&self, _scope_id: super::ScopeId, _service: Service) -> Option<()>
where
T: Actor<Self>,
Self: SupHandle<T>,
{
Some(())
}
async fn eol(
self,
_scope_id: super::ScopeId,
_service: super::Service,
_actor: T,
_r: super::ActorResult<()>,
) -> Option<()> {
Some(())
}
}
#[cfg(test)]
mod tests {
use crate::core::{Actor, ActorResult, IntervalChannel, Rt, StreamExt};
struct PrintHelloEveryFewMs;
#[async_trait::async_trait]
impl<S> Actor<S> for PrintHelloEveryFewMs
where
S: super::SupHandle<Self>,
{
type Data = ();
type Channel = IntervalChannel<100>;
async fn init(&mut self, _rt: &mut Rt<Self, S>) -> ActorResult<Self::Data> {
Ok(())
}
async fn run(&mut self, rt: &mut Rt<Self, S>, _data: Self::Data) -> ActorResult<()> {
while let Some(_) = rt.inbox_mut().next().await {
println!("HelloWorld")
}
Ok(())
}
}
}