1use std::any::{Any, TypeId};
22use std::fmt::{Debug, Display, Formatter};
23use std::hash::{Hash, Hasher};
24use std::marker::PhantomData;
25
26use serde::{Deserialize, Serialize};
27
28use super::entity_store::get_entity_metadata_static;
29
30#[derive(Serialize, Deserialize)]
33#[serde(transparent)]
34pub struct EntityId<E: Entity>(pub(crate) usize, PhantomData<E>);
35impl<E: Entity> PartialEq for EntityId<E> {
40 fn eq(&self, other: &Self) -> bool {
41 self.0 == other.0
42 }
43}
44impl<E: Entity> Eq for EntityId<E> {}
45
46impl<E: Entity> Clone for EntityId<E> {
48 #[inline]
49 fn clone(&self) -> Self {
50 *self
51 }
52}
53
54impl<E: Entity> Copy for EntityId<E> {}
56
57impl<E: Entity> Debug for EntityId<E> {
59 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
60 let name = format!("{}Id", E::name());
61 f.debug_tuple(name.as_str()).field(&self.0).finish()
62 }
63}
64impl<E: Entity> Display for EntityId<E> {
66 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
67 write!(f, "{}", self.0)
68 }
69}
70
71impl<E: Entity> Hash for EntityId<E> {
73 fn hash<H: Hasher>(&self, state: &mut H) {
74 self.0.hash(state);
75 }
76}
77
78impl<E: Entity> EntityId<E> {
79 pub(crate) fn new(index: usize) -> Self {
81 Self(index, PhantomData)
82 }
83}
84
85pub trait Entity: Any + Default {
87 #[must_use]
88 fn name() -> &'static str {
89 let full = std::any::type_name::<Self>();
90 full.rsplit("::").next().unwrap()
92 }
93
94 #[must_use]
95 fn type_id() -> TypeId {
96 TypeId::of::<Self>()
97 }
98
99 #[must_use]
101 fn property_ids() -> &'static [TypeId] {
102 let (property_ids, _) = get_entity_metadata_static(<Self as Entity>::type_id());
103 property_ids
104 }
105
106 #[must_use]
108 fn required_property_ids() -> &'static [TypeId] {
109 let (_, required_property_ids) = get_entity_metadata_static(<Self as Entity>::type_id());
110 required_property_ids
111 }
112
113 #[must_use]
117 fn id() -> usize;
118
119 #[doc(hidden)]
124 fn all_query_identity() -> usize {
125 panic!("Entity implementations must be created with define_entity! or impl_entity!")
126 }
127
128 #[must_use]
130 fn new_boxed() -> Box<Self> {
131 Box::default()
132 }
133
134 fn as_any(&self) -> &dyn Any;
136 fn as_any_mut(&mut self) -> &mut dyn Any;
137}
138
139pub type BxEntity = Box<dyn Entity>;
140
141#[derive(Copy, Clone)]
146pub struct PopulationIterator<E: Entity> {
147 population: usize,
149 entity_id: usize,
151
152 _phantom: PhantomData<E>,
153}
154
155impl<E: Entity> PopulationIterator<E> {
156 pub(crate) fn new(population: usize) -> Self {
159 PopulationIterator::<E> {
160 population,
161 entity_id: 0,
162 _phantom: PhantomData,
163 }
164 }
165
166 #[must_use]
167 pub(crate) fn population(&self) -> usize {
168 self.population
169 }
170}
171
172impl<E: Entity> Iterator for PopulationIterator<E> {
173 type Item = EntityId<E>;
174
175 fn next(&mut self) -> Option<Self::Item> {
176 if self.entity_id < self.population {
177 let current_id = self.entity_id;
178 self.entity_id += 1;
180 Some(EntityId::new(current_id))
181 } else {
182 None
183 }
184 }
185
186 fn size_hint(&self) -> (usize, Option<usize>) {
188 let remaining = self.len();
189 (remaining, Some(remaining))
190 }
191
192 fn count(self) -> usize {
194 self.len()
195 }
196
197 fn nth(&mut self, n: usize) -> Option<Self::Item> {
199 self.entity_id = (self.entity_id + n).min(self.population);
201 self.next()
202 }
203}
204
205impl<E: Entity> ExactSizeIterator for PopulationIterator<E> {
206 fn len(&self) -> usize {
207 self.population - self.entity_id
209 }
210}
211impl<E: Entity> std::iter::FusedIterator for PopulationIterator<E> {}
213
214#[cfg(test)]
215mod tests {
216 use super::*;
217 use crate::define_entity;
218
219 define_entity!(DummyEntity);
220
221 #[test]
222 fn entity_id_debug_display() {
223 let entity_id = DummyEntityId::new(137);
224 assert_eq!(format!("{:?}", entity_id), "DummyEntityId(137)");
225 assert_eq!(format!("{}", entity_id), "137");
226 }
227
228 #[test]
229 fn define_entity_helpers_create_and_downcast() {
230 assert_eq!(DummyEntity::new(), DummyEntity);
231
232 let mut entity = DummyEntity::new();
233 assert!(entity.as_any().downcast_ref::<DummyEntity>().is_some());
234 assert!(entity.as_any_mut().downcast_mut::<DummyEntity>().is_some());
235 }
236
237 #[test]
238 fn test_entity_iterator_basic() {
239 let mut iter = PopulationIterator::<DummyEntity>::new(3);
240
241 assert_eq!(iter.len(), 3);
242 assert_eq!(iter.next(), Some(EntityId::new(0)));
243 assert_eq!(iter.len(), 2);
244 assert_eq!(iter.next(), Some(EntityId::new(1)));
245 assert_eq!(iter.len(), 1);
246 assert_eq!(iter.next(), Some(EntityId::new(2)));
247 assert_eq!(iter.len(), 0);
248 assert_eq!(iter.next(), None);
249 assert_eq!(iter.len(), 0);
250 assert_eq!(iter.next(), None); }
252
253 #[test]
254 fn test_entity_iterator_nth() {
255 let mut iter = PopulationIterator::<DummyEntity>::new(10);
256
257 assert_eq!(iter.nth(2), Some(EntityId::new(2)));
259 assert_eq!(iter.len(), 7);
260
261 assert_eq!(iter.nth(1), Some(EntityId::new(4)));
263
264 assert_eq!(iter.nth(10), None);
266 assert_eq!(iter.len(), 0);
267 assert_eq!(iter.next(), None);
268 }
269
270 #[test]
271 fn test_entity_iterator_size_hint() {
272 let mut iter = PopulationIterator::<DummyEntity>::new(5);
273 assert_eq!(iter.size_hint(), (5, Some(5)));
274
275 iter.next();
276 assert_eq!(iter.size_hint(), (4, Some(4)));
277
278 assert_eq!(iter.nth(10), None);
280 assert_eq!(iter.size_hint(), (0, Some(0)));
281 }
282
283 #[test]
284 fn test_entity_iterator_clonable() {
285 let mut iter = PopulationIterator::<DummyEntity>::new(5);
286 iter.next();
287
288 let mut cloned = iter;
289 assert_eq!(iter.next(), cloned.next());
290 assert_eq!(iter.size_hint(), cloned.size_hint());
291 }
292}