use alloc::sync::Arc;
use crate::{Delegate, Fluxion, Message};
pub trait Actor: Send + Sync + 'static {
type Error;
fn initialize(&mut self) -> impl core::future::Future<Output = Result<(), Self::Error>> + Send {async {
Ok(())
}}
fn deinitialize(&self) -> impl core::future::Future<Output = ()> + Send {async {
}}
}
pub struct ActorContext<D> {
pub(crate) system: Fluxion<D>,
pub(crate) id: u64,
}
impl<D: Delegate> ActorContext<D> {
#[must_use]
pub fn get_id(&self) -> u64 {
self.id
}
#[must_use]
pub fn system(&self) -> &Fluxion<D> {
&self.system
}
}
pub trait Handler<M: Message>: Actor {
fn handle_message<D: Delegate>(&self, message: M, context: &ActorContext<D>) -> impl core::future::Future<Output = M::Result> + Send;
}
pub(crate) struct ActorWrapper<T: Actor, D: Delegate>(pub T, pub Arc<ActorContext<D>>);
impl<R: Actor, D: Delegate> slacktor::Actor for ActorWrapper<R, D> {
fn destroy(&self) -> impl core::future::Future<Output = ()> + Send {
self.0.deinitialize()
}
}
impl<R: Handler<M>, M: Message, D: Delegate> slacktor::actor::Handler<M> for ActorWrapper<R, D> {
#[inline]
fn handle_message(&self, message: M) -> impl core::future::Future<Output = <M as Message>::Result> + Send {
self.0.handle_message(message, &self.1)
}
}