use crate::rc::FwdRc;
use crate::{Actor, Cx};
use static_assertions::assert_eq_size;
pub struct Fwd<M: 'static>(FwdRc<M>);
assert_eq_size!(Fwd<()>, [usize; 2]);
impl<M> Fwd<M> {
pub fn fwd(&self, msg: M) {
self.0.inner()(msg);
}
#[inline]
pub fn new(f: impl Fn(M) + 'static) -> Self {
Self(FwdRc::new(f))
}
#[allow(clippy::wrong_self_convention)]
#[inline]
pub fn to_actor<A: 'static>(
actor: Actor<A>,
f: impl Fn(&mut A, &mut Cx<'_, A>, M) + Copy + 'static,
) -> Self {
Self::new(move |m| {
let actor2 = actor.clone();
actor.defer(move |s| actor2.apply(s, move |a, cx| f(a, cx, m)));
})
}
#[allow(clippy::wrong_self_convention)]
#[inline]
pub fn to_actor_prep<A: 'static>(
actor: Actor<A>,
f: impl Fn(&mut Cx<'_, A>, M) -> Option<A> + Copy + 'static,
) -> Self {
Self::new(move |m| {
let actor2 = actor.clone();
actor.defer(move |s| actor2.apply_prep(s, move |cx| f(cx, m)));
})
}
#[inline]
pub fn panic(msg: impl Into<String>) -> Self {
let msg: String = msg.into();
Self::new(move |_| panic!("{}", msg))
}
}
impl<M> Clone for Fwd<M> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}