use std::any::Any;
use std::marker::PhantomData;
use std::mem;
use crate::handler::MessageResponse;
use crate::prelude::*;
#[allow(clippy::type_complexity)]
pub struct Mocker<T: Sized + Unpin + 'static> {
phantom: PhantomData<T>,
mock: Box<dyn FnMut(Box<dyn Any>, &mut Context<Mocker<T>>) -> Box<dyn Any>>,
}
impl<T: Unpin> Mocker<T> {
#[allow(clippy::type_complexity)]
pub fn mock(
mock: Box<dyn FnMut(Box<dyn Any>, &mut Context<Mocker<T>>) -> Box<dyn Any>>,
) -> Mocker<T> {
Mocker::<T> {
phantom: PhantomData,
mock,
}
}
}
impl<T: SystemService> SystemService for Mocker<T> {}
impl<T: ArbiterService> ArbiterService for Mocker<T> {}
impl<T: Unpin> Supervised for Mocker<T> {}
impl<T: Unpin> Default for Mocker<T> {
fn default() -> Self {
panic!("Mocker actor used before set")
}
}
impl<T: Sized + Unpin + 'static> Actor for Mocker<T> {
type Context = Context<Self>;
}
impl<M: 'static, T: Sized + Unpin + 'static> Handler<M> for Mocker<T>
where
M: Message,
<M as Message>::Result: MessageResponse<Mocker<T>, M>,
{
type Result = M::Result;
fn handle(&mut self, msg: M, ctx: &mut Self::Context) -> M::Result {
let mut ret = (self.mock)(Box::new(msg), ctx);
let out = mem::replace(
ret.downcast_mut::<Option<M::Result>>()
.expect("wrong return type for message"),
None,
);
match out {
Some(a) => a,
_ => panic!(),
}
}
}