pub trait CloneableFn<I, R>: Fn(I) -> R + Send {
fn clone_box(&self) -> Box<dyn CloneableFn<I, R>>;
}
impl<F, I, R> CloneableFn<I, R> for F
where
F: Fn(I) -> R + Send + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFn<I, R>> {
Box::new(self.clone())
}
}
pub struct WrappedCloneableFnMut<I, R>(Box<dyn CloneableFn<I, R>>);
impl<I, R> WrappedCloneableFnMut<I, R> {
#[must_use]
pub fn new(elem: Box<dyn CloneableFn<I, R>>) -> Self {
Self(elem)
}
pub fn call(&mut self, input: I) -> R {
(self.0)(input)
}
}
impl<I: 'static, R: 'static> Clone for WrappedCloneableFnMut<I, R> {
fn clone(&self) -> Self {
Self(self.0.clone_box())
}
}