use super::user_event::UserEvent;
use crate::actor::{Address, BirthMode};
use crate::effects::{Acted, Actions, SendAlgebra};
pub type BehaviorActed<B> = Acted<
<B as Behavior>::Addr,
<B as Behavior>::Ph,
<B as Behavior>::Sends,
<B as Behavior>::Birth,
<B as Behavior>::Error,
>;
pub struct InitializationTurn {
#[allow(dead_code, reason = "private field prevents external construction")]
private: (),
}
impl InitializationTurn {
pub(crate) const fn new() -> Self {
Self { private: () }
}
}
pub struct ActiveTurn {
#[allow(dead_code, reason = "private field prevents external construction")]
private: (),
}
impl ActiveTurn {
pub(crate) const fn new() -> Self {
Self { private: () }
}
}
pub trait Behavior {
type Addr: Address;
type Msg;
type Event: UserEvent<Addr = Self::Addr, Message = Self::Msg>;
type Sends: SendAlgebra;
type Ph;
type Error;
type Birth: BirthMode;
fn init(&mut self, _turn: InitializationTurn) -> BehaviorActed<Self>
where
Self: Sized,
{
Ok(Actions::cont())
}
fn transition(&mut self, _turn: ActiveTurn, event: Self::Event) -> BehaviorActed<Self>;
}
pub trait BehaviorBase {
type Base;
fn base(&self) -> &Self::Base;
}
pub(crate) fn initialize<B: Behavior>(behavior: &mut B) -> BehaviorActed<B> {
B::init(behavior, InitializationTurn::new())
}
pub(crate) fn delegate_transition<B: Behavior>(
behavior: &mut B,
event: B::Event,
) -> BehaviorActed<B> {
B::transition(behavior, ActiveTurn::new(), event)
}