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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::ops::{Deref, DerefMut};
pub trait PropertyMutate: PropertyMutateClone + Send + Sync + 'static {
fn mutate(&mut self, property_index: u8);
}
pub trait PropertyMutateClone {
fn clone_box(&self) -> Box<dyn PropertyMutate>;
}
impl<T: 'static + Clone + PropertyMutate> PropertyMutateClone for T {
fn clone_box(&self) -> Box<dyn PropertyMutate> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn PropertyMutate> {
fn clone(&self) -> Box<dyn PropertyMutate> {
PropertyMutateClone::clone_box(self.as_ref())
}
}
#[derive(Clone)]
pub struct PropertyMutator {
inner: Box<dyn PropertyMutate>,
}
impl PropertyMutator {
pub fn new<M: PropertyMutate>(mutator: M) -> Self {
let inner = Box::new(mutator);
return Self { inner };
}
pub fn clone_new(&self) -> Self {
let new_inner = self.inner.as_ref().clone_box();
return Self { inner: new_inner };
}
}
impl Deref for PropertyMutator {
type Target = dyn PropertyMutate;
fn deref(&self) -> &Self::Target {
self.inner.deref()
}
}
impl DerefMut for PropertyMutator {
fn deref_mut(&mut self) -> &mut Self::Target {
self.inner.deref_mut()
}
}