Skip to main content

naia_shared/world/component/
replica_ref.rs

1use std::ops::{Deref, DerefMut};
2
3use crate::world::component::replicate::Replicate;
4
5/// Shared reference to a type-erased replicated component.
6pub struct ReplicaDynRef<'b> {
7    inner: &'b dyn Replicate,
8}
9
10impl<'b> ReplicaDynRef<'b> {
11    /// Creates a `ReplicaDynRef` wrapping the given reference.
12    pub fn new(inner: &'b dyn Replicate) -> Self {
13        Self { inner }
14    }
15}
16
17impl Deref for ReplicaDynRef<'_> {
18    type Target = dyn Replicate;
19
20    #[inline]
21    fn deref(&self) -> &dyn Replicate {
22        self.inner
23    }
24}
25
26impl<'a> ReplicaDynRefTrait for ReplicaDynRef<'a> {
27    fn to_dyn_ref(&self) -> &dyn Replicate {
28        self.inner
29    }
30}
31
32/// Mutable reference to a type-erased replicated component.
33pub struct ReplicaDynMut<'b> {
34    inner: &'b mut dyn Replicate,
35}
36
37impl<'b> ReplicaDynMut<'b> {
38    /// Creates a `ReplicaDynMut` wrapping the given mutable reference.
39    pub fn new(inner: &'b mut dyn Replicate) -> Self {
40        Self { inner }
41    }
42}
43
44impl Deref for ReplicaDynMut<'_> {
45    type Target = dyn Replicate;
46
47    #[inline]
48    fn deref(&self) -> &dyn Replicate {
49        self.inner
50    }
51}
52
53impl DerefMut for ReplicaDynMut<'_> {
54    #[inline]
55    fn deref_mut(&mut self) -> &mut dyn Replicate {
56        self.inner
57    }
58}
59
60impl<'a> ReplicaDynRefTrait for ReplicaDynMut<'a> {
61    fn to_dyn_ref(&self) -> &dyn Replicate {
62        self.inner
63    }
64}
65
66impl<'a> ReplicaDynMutTrait for ReplicaDynMut<'a> {
67    fn to_dyn_mut(&mut self) -> &mut dyn Replicate {
68        self.inner
69    }
70}
71
72/// Trait for typed shared access to a concrete replicated component.
73pub trait ReplicaRefTrait<R: Replicate> {
74    /// Returns a shared reference to the concrete component.
75    fn to_ref(&self) -> &R;
76}
77
78/// Type-erasing wrapper around a `ReplicaRefTrait<R>` implementation.
79pub struct ReplicaRefWrapper<'a, R: Replicate> {
80    inner: Box<dyn ReplicaRefTrait<R> + 'a>,
81}
82
83impl<'a, R: Replicate> ReplicaRefWrapper<'a, R> {
84    /// Creates a `ReplicaRefWrapper` from any `ReplicaRefTrait<R>` implementor.
85    pub fn new<I: ReplicaRefTrait<R> + 'a>(inner: I) -> Self {
86        Self {
87            inner: Box::new(inner),
88        }
89    }
90}
91
92impl<'a, R: Replicate> Deref for ReplicaRefWrapper<'a, R> {
93    type Target = R;
94
95    fn deref(&self) -> &R {
96        self.inner.to_ref()
97    }
98}
99
100/// Trait for typed mutable access to a concrete replicated component.
101pub trait ReplicaMutTrait<R: Replicate>: ReplicaRefTrait<R> {
102    /// Returns a mutable reference to the concrete component.
103    fn to_mut(&mut self) -> &mut R;
104}
105
106/// Type-erasing wrapper around a `ReplicaMutTrait<R>` implementation.
107pub struct ReplicaMutWrapper<'a, R: Replicate> {
108    inner: Box<dyn ReplicaMutTrait<R> + 'a>,
109}
110
111impl<'a, R: Replicate> ReplicaMutWrapper<'a, R> {
112    /// Creates a `ReplicaMutWrapper` from any `ReplicaMutTrait<R>` implementor.
113    pub fn new<I: ReplicaMutTrait<R> + 'a>(inner: I) -> Self {
114        Self {
115            inner: Box::new(inner),
116        }
117    }
118}
119
120impl<'a, R: Replicate> Deref for ReplicaMutWrapper<'a, R> {
121    type Target = R;
122
123    fn deref(&self) -> &R {
124        self.inner.to_ref()
125    }
126}
127
128impl<'a, R: Replicate> DerefMut for ReplicaMutWrapper<'a, R> {
129    fn deref_mut(&mut self) -> &mut R {
130        self.inner.to_mut()
131    }
132}
133
134/// Trait for shared access to a type-erased replicated component.
135pub trait ReplicaDynRefTrait {
136    /// Returns a shared `dyn Replicate` reference.
137    fn to_dyn_ref(&self) -> &dyn Replicate;
138}
139
140/// Type-erasing wrapper for a `ReplicaDynRefTrait` implementor.
141pub struct ReplicaDynRefWrapper<'a> {
142    inner: Box<dyn ReplicaDynRefTrait + 'a>,
143}
144
145impl<'a> ReplicaDynRefWrapper<'a> {
146    /// Creates a `ReplicaDynRefWrapper` from any `ReplicaDynRefTrait` implementor.
147    pub fn new<I: ReplicaDynRefTrait + 'a>(inner: I) -> Self {
148        Self {
149            inner: Box::new(inner),
150        }
151    }
152}
153
154impl<'a> Deref for ReplicaDynRefWrapper<'a> {
155    type Target = dyn Replicate;
156
157    fn deref(&self) -> &dyn Replicate {
158        self.inner.to_dyn_ref()
159    }
160}
161
162/// Trait for mutable access to a type-erased replicated component.
163pub trait ReplicaDynMutTrait: ReplicaDynRefTrait {
164    /// Returns a mutable `dyn Replicate` reference.
165    fn to_dyn_mut(&mut self) -> &mut dyn Replicate;
166}
167
168/// Type-erasing wrapper for a `ReplicaDynMutTrait` implementor.
169pub struct ReplicaDynMutWrapper<'a> {
170    inner: Box<dyn ReplicaDynMutTrait + 'a>,
171}
172
173impl<'a> ReplicaDynMutWrapper<'a> {
174    /// Creates a `ReplicaDynMutWrapper` from any `ReplicaDynMutTrait` implementor.
175    pub fn new<I: ReplicaDynMutTrait + 'a>(inner: I) -> Self {
176        Self {
177            inner: Box::new(inner),
178        }
179    }
180}
181
182impl<'a> Deref for ReplicaDynMutWrapper<'a> {
183    type Target = dyn Replicate;
184
185    fn deref(&self) -> &dyn Replicate {
186        self.inner.to_dyn_ref()
187    }
188}
189
190impl<'a> DerefMut for ReplicaDynMutWrapper<'a> {
191    fn deref_mut(&mut self) -> &mut dyn Replicate {
192        self.inner.to_dyn_mut()
193    }
194}