use super::Address;
use crate::actor_runtime::Actor;
use crate::handlers::{Action, ActionHandler, Interact, Interaction, InteractionTask};
use crate::ids::Id;
use anyhow::Error;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
pub trait ActionRecipient<T: Action>: Debug + Send + 'static {
fn act(&mut self, msg: T) -> Result<(), Error>;
#[doc(hidden)]
fn id_ref(&self) -> &Id;
#[doc(hidden)]
fn dyn_clone(&self) -> Box<dyn ActionRecipient<T>>;
#[doc(hidden)]
fn dyn_hash(&self, state: &mut dyn Hasher);
}
impl<T: Action> Clone for Box<dyn ActionRecipient<T>> {
fn clone(&self) -> Self {
self.dyn_clone()
}
}
impl<T: Action> PartialEq for Box<dyn ActionRecipient<T>> {
fn eq(&self, other: &Self) -> bool {
PartialEq::eq(self.id_ref(), other.id_ref())
}
}
impl<T: Action> Eq for Box<dyn ActionRecipient<T>> {}
impl<T: Action> Hash for Box<dyn ActionRecipient<T>> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.dyn_hash(state);
}
}
impl<T, A> ActionRecipient<T> for Address<A>
where
T: Action,
A: Actor + ActionHandler<T>,
{
fn act(&mut self, msg: T) -> Result<(), Error> {
Address::act(self, msg)
}
fn id_ref(&self) -> &Id {
self.raw_id()
}
fn dyn_clone(&self) -> Box<dyn ActionRecipient<T>> {
Box::new(self.clone())
}
fn dyn_hash(&self, state: &mut dyn Hasher) {
ActionRecipient::id_ref(self).hash(&mut Box::new(state));
}
}
pub trait InteractionRecipient<T: Interaction>: Debug + Send + 'static {
fn interact(&mut self, msg: T) -> InteractionTask<T>;
#[doc(hidden)]
fn id_ref(&self) -> &Id;
#[doc(hidden)]
fn dyn_clone(&self) -> Box<dyn InteractionRecipient<T>>;
#[doc(hidden)]
fn dyn_hash(&self, state: &mut dyn Hasher);
}
impl<T: Interaction> Clone for Box<dyn InteractionRecipient<T>> {
fn clone(&self) -> Self {
self.dyn_clone()
}
}
impl<T: Interaction> PartialEq for Box<dyn InteractionRecipient<T>> {
fn eq(&self, other: &Self) -> bool {
PartialEq::eq(self.id_ref(), other.id_ref())
}
}
impl<T: Interaction> Eq for Box<dyn InteractionRecipient<T>> {}
impl<T: Interaction> Hash for Box<dyn InteractionRecipient<T>> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.dyn_hash(state);
}
}
impl<T, A> InteractionRecipient<T> for Address<A>
where
T: Interaction,
A: Actor + ActionHandler<Interact<T>>,
{
fn interact(&mut self, msg: T) -> InteractionTask<T> {
Address::interact(self, msg)
}
fn id_ref(&self) -> &Id {
self.raw_id()
}
fn dyn_clone(&self) -> Box<dyn InteractionRecipient<T>> {
Box::new(self.clone())
}
fn dyn_hash(&self, state: &mut dyn Hasher) {
InteractionRecipient::id_ref(self).hash(&mut Box::new(state));
}
}