naia_shared/world/component/
property_mutate.rs

1use std::ops::{Deref, DerefMut};
2
3/// Tracks which Properties have changed and need to be queued for syncing with
4/// the Client
5pub trait PropertyMutate: PropertyMutateClone + Send + Sync + 'static {
6    /// Given the index of the Property whose value has changed, queue that
7    /// Property for transmission to the Client
8    fn mutate(&mut self, property_index: u8) -> bool;
9}
10
11pub trait PropertyMutateClone {
12    /// Clone ..
13    fn clone_box(&self) -> Box<dyn PropertyMutate>;
14}
15
16impl<T: 'static + Clone + PropertyMutate> PropertyMutateClone for T {
17    fn clone_box(&self) -> Box<dyn PropertyMutate> {
18        Box::new(self.clone())
19    }
20}
21
22impl Clone for Box<dyn PropertyMutate> {
23    fn clone(&self) -> Box<dyn PropertyMutate> {
24        PropertyMutateClone::clone_box(self.as_ref())
25    }
26}
27
28#[derive(Clone)]
29pub struct PropertyMutator {
30    inner: Box<dyn PropertyMutate>,
31}
32
33impl PropertyMutator {
34    pub fn new<M: PropertyMutate>(mutator: M) -> Self {
35        let inner = Box::new(mutator);
36        Self { inner }
37    }
38
39    pub fn clone_new(&self) -> Self {
40        //let current_inner: &dyn PropertyMutateClone = self.inner.as_ref() as &dyn
41        // PropertyMutateClone;
42        let new_inner = self.inner.as_ref().clone_box();
43
44        Self { inner: new_inner }
45    }
46}
47
48impl Deref for PropertyMutator {
49    type Target = dyn PropertyMutate;
50
51    fn deref(&self) -> &Self::Target {
52        self.inner.deref()
53    }
54}
55
56impl DerefMut for PropertyMutator {
57    fn deref_mut(&mut self) -> &mut Self::Target {
58        self.inner.deref_mut()
59    }
60}