1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::fmt::Debug;

pub trait CloneableFnMutWithArg<Arg>: FnMut(Arg) {
    fn clone_box(&self) -> Box<dyn CloneableFnMutWithArg<Arg>>;
}

impl<F, Arg> CloneableFnMutWithArg<Arg> for F
where
    F: FnMut(Arg) + Clone + 'static,
{
    fn clone_box(&self) -> Box<dyn CloneableFnMutWithArg<Arg>> {
        Box::new(self.clone())
    }
}

impl<Arg> Clone for Box<dyn CloneableFnMutWithArg<Arg>> {
    fn clone(&self) -> Self {
        (**self).clone_box()
    }
}

impl<Arg> Default for Box<dyn CloneableFnMutWithArg<Arg>> {
    fn default() -> Self {
        Box::new(|_| {})
    }
}

impl<Arg> Debug for Box<dyn CloneableFnMutWithArg<Arg>> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Box<dyn CloneableFnMutWithArg<{}>>",
            std::any::type_name::<Arg>()
        )
    }
}