xecs/group/partial_owning/
query.rs

1use std::any::TypeId;
2use parking_lot::{RwLockReadGuard, RwLockWriteGuard};
3use crate::{component::{Component, ComponentStorage}, entity::EntityId, group::{Group, partial_owning}, query::{QueryIterator, Queryable}, sparse_set::SparseSet, world::World};
4use super::PartialOwning;
5
6pub struct IterRefRef<'a,A,B> {
7    index: usize,
8    length: usize,
9    sparse_set_a: *const SparseSet<EntityId,A>,
10    sparse_set_b: *const SparseSet<EntityId,B>,
11    #[allow(unused)]
12    borrow_a: RwLockReadGuard<'a,Box<dyn ComponentStorage>>,
13    #[allow(unused)]
14    borrow_b: RwLockReadGuard<'a,Box<dyn ComponentStorage>>,
15    #[allow(unused)]
16    borrow_group: RwLockReadGuard<'a,Group>
17}
18
19impl<'a,A : Component,B : Component> Queryable<'a> for PartialOwning<&'a A,&'a B> {
20    type Item = (&'a A,&'a B);
21
22    fn query(world : &'a World) -> Box<(dyn QueryIterator<Item = Self::Item> + 'a)> {
23        assert!(world.has_registered::<A>() && world.has_registered::<B>(),
24                "Queryable for PartialOwning: Component was not registered in world");
25        let type_id_a = TypeId::of::<A>();
26        let type_id_b = TypeId::of::<B>();
27        // Unwrap here
28        // assert before ensures this
29        let storage_a = world.raw_storage_read(type_id_a).unwrap();
30        let storage_b = world.raw_storage_read(type_id_b).unwrap();
31        // Safety:
32        // storage is SparseSet<EntityId,...>
33        let sparse_set_a = unsafe {
34            storage_a.downcast_ref::<SparseSet<EntityId,A>>()
35        };
36        let sparse_set_b = unsafe {
37            storage_b.downcast_ref::<SparseSet<EntityId,B>>()
38        };
39        let ptr_a = &*sparse_set_a;
40        let ptr_b = &*sparse_set_b;
41        assert!(world.has_group(partial_owning::<A,B>()),"Queryable for PartialOwning: Group is not in world");
42        let group = world.group(partial_owning::<A,B>());
43        let length = group.len();
44        Box::new(IterRefRef {
45            index: 0,
46            length,
47            sparse_set_a: ptr_a,
48            sparse_set_b: ptr_b,
49            borrow_a: storage_a,
50            borrow_b: storage_b,
51            borrow_group: group,
52        })
53    }
54}
55
56impl<'a,A : Component,B : Component> Iterator for IterRefRef<'a,A,B> {
57    type Item = (&'a A,&'a B);
58
59    fn next(&mut self) -> Option<Self::Item> {
60        if self.index < self.length {
61            // Safety:
62            // Safe here, because self.sparse_set is 
63            // a pointer from borrow,
64            // This pointer is valid now.
65            let sparse_set_a = unsafe { &*self.sparse_set_a };
66            let sparse_set_b = unsafe { &*self.sparse_set_b };
67            // Safety:
68            // Safe here, because we checked before.
69            let id = *unsafe {
70                sparse_set_a.entities()
71                    .get_unchecked(self.index)
72            };
73            let data_a = unsafe {
74                sparse_set_a.data().get_unchecked(self.index)
75            };
76            // Unwrap here
77            // This panics while group is destroyed.
78            // But group is matained by internal.
79            // So it never fails.
80            let data_b = sparse_set_b.get(id).unwrap();
81            self.index += 1;
82            Some((data_a,data_b))
83        } else {
84            None
85        }
86    }
87
88    fn size_hint(&self) -> (usize, Option<usize>) {
89        let rem = self.length - self.index;
90        (rem,Some(rem))
91    }
92}
93
94impl<'a,A: Component,B: Component> ExactSizeIterator for IterRefRef<'a,A,B> {}
95
96impl<'a,A : Component,B : Component> QueryIterator for IterRefRef<'a,A,B> {
97    fn from_id(&mut self,id : EntityId) -> Option<Self::Item> {
98        // Safety:
99        // Safe here, because self.sparse_set is 
100        // a pointer from borrow,
101        // This pointer is valid now.
102        let sparse_set_a = unsafe { &*self.sparse_set_a };
103        let sparse_set_b = unsafe { &*self.sparse_set_b };
104        if let Some(a) = sparse_set_a.get(id) {
105            if let Some(b) = sparse_set_b.get(id) {
106                return Some((a,b))
107            }
108        }
109        None
110    }
111
112    fn next_with_id(&mut self) -> Option<(EntityId,Self::Item)> {
113        if self.index < self.length {
114            // Safety:
115            // Safe here, because self.sparse_set is 
116            // a pointer from borrow,
117            // This pointer is valid now.
118            let sparse_set_a = unsafe { &*self.sparse_set_a };
119            let sparse_set_b = unsafe { &*self.sparse_set_b };
120            // Safety:
121            // Safe here, because we checked before.
122            let id = *unsafe {
123                sparse_set_a.entities()
124                    .get_unchecked(self.index)
125            };
126            let data_a = unsafe {
127                sparse_set_a.data().get_unchecked(self.index)
128            };
129            // Unwrap here
130            // This panics while group is destroyed.
131            // But group is matained by internal.
132            // So it never fails.
133            let data_b = sparse_set_b.get(id).unwrap();
134            self.index += 1;
135            Some((id,(data_a,data_b)))
136        } else {
137            None
138        }
139    }
140}
141
142
143
144
145
146
147pub struct IterRefMut<'a,A,B> {
148    index: usize,
149    length: usize,
150    sparse_set_a: *const SparseSet<EntityId,A>,
151    sparse_set_b: *mut SparseSet<EntityId,B>,
152    #[allow(unused)]
153    borrow_a: RwLockReadGuard<'a,Box<dyn ComponentStorage>>,
154    #[allow(unused)]
155    borrow_b: RwLockWriteGuard<'a,Box<dyn ComponentStorage>>,
156    #[allow(unused)]
157    borrow_group: RwLockReadGuard<'a,Group>
158}
159
160impl<'a,A : Component,B : Component> Queryable<'a> for PartialOwning<&'a A,&'a mut B> {
161    type Item = (&'a A,&'a mut B);
162
163    fn query(world : &'a World) -> Box<(dyn QueryIterator<Item = Self::Item> + 'a)> {
164        assert!(world.has_registered::<A>() && world.has_registered::<B>(),
165                "Queryable for PartialOwning: Component was not registered in world");
166        let type_id_a = TypeId::of::<A>();
167        let type_id_b = TypeId::of::<B>();
168        // Unwrap here
169        // assert before ensures this
170        let storage_a = world.raw_storage_read(type_id_a).unwrap();
171        let mut storage_b = world.raw_storage_write(type_id_b).unwrap();
172        // Safety:
173        // storage is SparseSet<EntityId,...>
174        let sparse_set_a = unsafe {
175            storage_a.downcast_ref::<SparseSet<EntityId,A>>()
176        };
177        let sparse_set_b = unsafe {
178            storage_b.downcast_mut::<SparseSet<EntityId,B>>()
179        };
180        let ptr_a = &*sparse_set_a;
181        let ptr_b = &mut *sparse_set_b;
182        assert!(world.has_group(partial_owning::<A,B>()),"Queryable for PartialOwning: Group is not in world");
183        let group = world.group(partial_owning::<A,B>());
184        let length = group.len();
185        Box::new(IterRefMut {
186            index: 0,
187            length,
188            sparse_set_a: ptr_a,
189            sparse_set_b: ptr_b,
190            borrow_a: storage_a,
191            borrow_b: storage_b,
192            borrow_group: group,
193        })
194    }
195}
196
197impl<'a,A : Component,B : Component> Iterator for IterRefMut<'a,A,B> {
198    type Item = (&'a A,&'a mut B);
199
200    fn next(&mut self) -> Option<Self::Item> {
201        if self.index < self.length {
202            // Safety:
203            // Safe here, because self.sparse_set is 
204            // a pointer from borrow,
205            // This pointer is valid now.
206            let sparse_set_a = unsafe { &*self.sparse_set_a };
207            let sparse_set_b = unsafe { &mut *self.sparse_set_b };
208            // Safety:
209            // Safe here, because we checked before.
210            let id = *unsafe {
211                sparse_set_a.entities()
212                    .get_unchecked(self.index)
213            };
214            let data_a = unsafe {
215                sparse_set_a.data().get_unchecked(self.index)
216            };
217            // Unwrap here
218            // This panics while group is destroyed.
219            // But group is matained by internal.
220            // So it never fails
221            let data_b = sparse_set_b.get_mut(id).unwrap();
222            self.index += 1;
223            Some((data_a,data_b))
224        } else {
225            None
226        }
227    }
228
229    fn size_hint(&self) -> (usize, Option<usize>) {
230        let rem = self.length - self.index;
231        (rem,Some(rem))
232    }
233}
234
235impl<'a,A: Component,B: Component> ExactSizeIterator for IterRefMut<'a,A,B> {}
236
237impl<'a,A : Component,B : Component> QueryIterator for IterRefMut<'a,A,B> {
238    fn from_id(&mut self,id : EntityId) -> Option<Self::Item> {
239        // Safety:
240        // Safe here, because self.sparse_set is 
241        // a pointer from borrow,
242        // This pointer is valid now.
243        let sparse_set_a = unsafe { &*self.sparse_set_a };
244        let sparse_set_b = unsafe { &mut *self.sparse_set_b };
245        if let Some(a) = sparse_set_a.get(id) {
246            if let Some(b) = sparse_set_b.get_mut(id) {
247                return Some((a,b))
248            }
249        }
250        None
251    }
252
253    fn next_with_id(&mut self) -> Option<(EntityId,Self::Item)> {
254        if self.index < self.length {
255            // Safety:
256            // Safe here, because self.sparse_set is 
257            // a pointer from borrow,
258            // This pointer is valid now.
259            let sparse_set_a = unsafe { &*self.sparse_set_a };
260            let sparse_set_b = unsafe { &mut *self.sparse_set_b };
261            // Safety:
262            // Safe here, because we checked before.
263            let id = *unsafe {
264                sparse_set_a.entities()
265                    .get_unchecked(self.index)
266            };
267            let data_a = unsafe {
268                sparse_set_a.data().get_unchecked(self.index)
269            };
270            // Unwrap here
271            // This panics while group is destroyed.
272            // But group is matained by internal.
273            // So it never fails
274            let data_b = sparse_set_b.get_mut(id).unwrap();
275            self.index += 1;
276            Some((id,(data_a,data_b)))
277        } else {
278            None
279        }
280    }
281}
282
283
284
285
286
287
288
289
290pub struct IterMutRef<'a,A,B> {
291    index: usize,
292    length: usize,
293    sparse_set_a: *mut SparseSet<EntityId,A>,
294    sparse_set_b: *const SparseSet<EntityId,B>,
295    #[allow(unused)]
296    borrow_a: RwLockWriteGuard<'a,Box<dyn ComponentStorage>>,
297    #[allow(unused)]
298    borrow_b: RwLockReadGuard<'a,Box<dyn ComponentStorage>>,
299    #[allow(unused)]
300    borrow_group: RwLockReadGuard<'a,Group>
301}
302
303impl<'a,A : Component,B : Component> Queryable<'a> for PartialOwning<&'a mut A,&'a B> {
304    type Item = (&'a mut A,&'a B);
305
306    fn query(world : &'a World) -> Box<(dyn QueryIterator<Item = Self::Item> + 'a)> {
307        assert!(world.has_registered::<A>() && world.has_registered::<B>(),
308                "Queryable for PartialOwning: Component was not registered in world");
309        let type_id_a = TypeId::of::<A>();
310        let type_id_b = TypeId::of::<B>();
311        // Unwrap here
312        // assert before ensures this
313        let mut storage_a = world.raw_storage_write(type_id_a).unwrap();
314        let storage_b = world.raw_storage_read(type_id_b).unwrap();
315        // Safety:
316        // storage is SparseSet<EntityId,...>
317        let sparse_set_a = unsafe {
318            storage_a.downcast_mut::<SparseSet<EntityId,A>>()
319        };
320        let sparse_set_b = unsafe {
321            storage_b.downcast_ref::<SparseSet<EntityId,B>>()
322        };
323        let ptr_a = &mut *sparse_set_a;
324        let ptr_b = &*sparse_set_b;
325        assert!(world.has_group(partial_owning::<A,B>()),"Queryable for PartialOwning: Group is not in world");
326        let group = world.group(partial_owning::<A,B>());
327        let length = group.len();
328        Box::new(IterMutRef {
329            index: 0,
330            length,
331            sparse_set_a: ptr_a,
332            sparse_set_b: ptr_b,
333            borrow_a: storage_a,
334            borrow_b: storage_b,
335            borrow_group: group,
336        })
337    }
338}
339
340impl<'a,A : Component,B : Component> Iterator for IterMutRef<'a,A,B> {
341    type Item = (&'a mut A,&'a B);
342
343    fn next(&mut self) -> Option<Self::Item> {
344        if self.index < self.length {
345            // Safety:
346            // Safe here, because self.sparse_set is 
347            // a pointer from borrow,
348            // This pointer is valid now.
349            let sparse_set_a = unsafe { &mut *self.sparse_set_a };
350            let sparse_set_b = unsafe { &*self.sparse_set_b };
351            // Safety:
352            // Safe here, because we checked before.
353            let id = *unsafe {
354                sparse_set_a.entities()
355                    .get_unchecked(self.index)
356            };
357            let data_a = unsafe {
358                sparse_set_a.data_mut().get_unchecked_mut(self.index)
359            };
360            // Unwrap here
361            // This panics while group is destroyed.
362            // But group is matained by internal.
363            // So it never fails
364            let data_b = sparse_set_b.get(id).unwrap();
365            self.index += 1;
366            Some((data_a,data_b))
367        } else {
368            None
369        }
370    }
371
372    fn size_hint(&self) -> (usize, Option<usize>) {
373        let rem = self.length - self.index;
374        (rem,Some(rem))
375    }
376}
377
378impl<'a,A: Component,B: Component> ExactSizeIterator for IterMutRef<'a,A,B> {}
379
380impl<'a,A : Component,B : Component> QueryIterator for IterMutRef<'a,A,B> {
381    fn from_id(&mut self,id : EntityId) -> Option<Self::Item> {
382        // Safety:
383        // Safe here, because self.sparse_set is 
384        // a pointer from borrow,
385        // This pointer is valid now.
386        let sparse_set_a = unsafe { &mut *self.sparse_set_a };
387        let sparse_set_b = unsafe { &*self.sparse_set_b };
388        if let Some(a) = sparse_set_a.get_mut(id) {
389            if let Some(b) = sparse_set_b.get(id) {
390                return Some((a,b))
391            }
392        }
393        None
394    }
395
396    fn next_with_id(&mut self) -> Option<(EntityId,Self::Item)> {
397        if self.index < self.length {
398            // Safety:
399            // Safe here, because self.sparse_set is 
400            // a pointer from borrow,
401            // This pointer is valid now.
402            let sparse_set_a = unsafe { &mut *self.sparse_set_a };
403            let sparse_set_b = unsafe { &*self.sparse_set_b };
404            // Safety:
405            // Safe here, because we checked before.
406            let id = *unsafe {
407                sparse_set_a.entities()
408                    .get_unchecked(self.index)
409            };
410            let data_a = unsafe {
411                sparse_set_a.data_mut().get_unchecked_mut(self.index)
412            };
413            // Unwrap here
414            // This panics while group is destroyed.
415            // But group is matained by internal.
416            // So it never fails
417            let data_b = sparse_set_b.get(id).unwrap();
418            self.index += 1;
419            Some((id,(data_a,data_b)))
420        } else {
421            None
422        }
423    }
424}
425
426
427
428
429
430
431pub struct IterMutMut<'a,A,B> {
432    index: usize,
433    length: usize,
434    sparse_set_a: *mut SparseSet<EntityId,A>,
435    sparse_set_b: *mut SparseSet<EntityId,B>,
436    #[allow(unused)]
437    borrow_a: RwLockWriteGuard<'a,Box<dyn ComponentStorage>>,
438    #[allow(unused)]
439    borrow_b: RwLockWriteGuard<'a,Box<dyn ComponentStorage>>,
440    #[allow(unused)]
441    borrow_group: RwLockReadGuard<'a,Group>
442}
443
444impl<'a,A : Component,B : Component> Queryable<'a> for PartialOwning<&'a mut A,&'a mut B> {
445    type Item = (&'a mut A,&'a mut B);
446
447    fn query(world : &'a World) -> Box<(dyn QueryIterator<Item = Self::Item> + 'a)> {
448        assert!(world.has_registered::<A>() && world.has_registered::<B>(),
449                "Queryable for PartialOwning: Component was not registered in world");
450        let type_id_a = TypeId::of::<A>();
451        let type_id_b = TypeId::of::<B>();
452        // Unwrap here
453        // assert before ensures this
454        let mut storage_a = world.raw_storage_write(type_id_a).unwrap();
455        let mut storage_b = world.raw_storage_write(type_id_b).unwrap();
456        // Safety:
457        // storage is SparseSet<EntityId,...>
458        let sparse_set_a = unsafe {
459            storage_a.downcast_mut::<SparseSet<EntityId,A>>()
460        };
461        let sparse_set_b = unsafe {
462            storage_b.downcast_mut::<SparseSet<EntityId,B>>()
463        };
464        let ptr_a = &mut *sparse_set_a;
465        let ptr_b = &mut *sparse_set_b;
466        assert!(world.has_group(partial_owning::<A,B>()),"Queryable for PartialOwning: Group is not in world");
467        let group = world.group(partial_owning::<A,B>());
468        let length = group.len();
469        Box::new(IterMutMut {
470            index: 0,
471            length,
472            sparse_set_a: ptr_a,
473            sparse_set_b: ptr_b,
474            borrow_a: storage_a,
475            borrow_b: storage_b,
476            borrow_group: group,
477        })
478    }
479}
480
481impl<'a,A : Component,B : Component> Iterator for IterMutMut<'a,A,B> {
482    type Item = (&'a mut A,&'a mut B);
483
484    fn next(&mut self) -> Option<Self::Item> {
485        if self.index < self.length {
486            // Safety:
487            // Safe here, because self.sparse_set is 
488            // a pointer from borrow,
489            // This pointer is valid now.
490            let sparse_set_a = unsafe { &mut *self.sparse_set_a };
491            let sparse_set_b = unsafe { &mut *self.sparse_set_b };
492            // Safety:
493            // Safe here, because we checked before.
494            let id = *unsafe {
495                sparse_set_a.entities()
496                    .get_unchecked(self.index)
497            };
498            let data_a = unsafe {
499                sparse_set_a.data_mut().get_unchecked_mut(self.index)
500            };
501            // Unwrap here
502            // This panics while group is destroyed.
503            // But group is matained by internal.
504            // So it never fails
505            let data_b = sparse_set_b.get_mut(id).unwrap();
506            self.index += 1;
507            Some((data_a,data_b))
508        } else {
509            None
510        }
511    }
512
513    fn size_hint(&self) -> (usize, Option<usize>) {
514        let rem = self.length - self.index;
515        (rem,Some(rem))
516    }
517}
518
519impl<'a,A: Component,B: Component> ExactSizeIterator for IterMutMut<'a,A,B> {}
520
521impl<'a,A : Component,B : Component> QueryIterator for IterMutMut<'a,A,B> {
522    fn from_id(&mut self,id : EntityId) -> Option<Self::Item> {
523        // Safety:
524        // Safe here, because self.sparse_set is 
525        // a pointer from borrow,
526        // This pointer is valid now.
527        let sparse_set_a = unsafe { &mut *self.sparse_set_a };
528        let sparse_set_b = unsafe { &mut *self.sparse_set_b };
529        if let Some(a) = sparse_set_a.get_mut(id) {
530            if let Some(b) = sparse_set_b.get_mut(id) {
531                return Some((a,b))
532            }
533        }
534        None
535    }
536
537    fn next_with_id(&mut self) -> Option<(EntityId,Self::Item)> {
538        if self.index < self.length {
539            // Safety:
540            // Safe here, because self.sparse_set is 
541            // a pointer from borrow,
542            // This pointer is valid now.
543            let sparse_set_a = unsafe { &mut *self.sparse_set_a };
544            let sparse_set_b = unsafe { &mut *self.sparse_set_b };
545            // Safety:
546            // Safe here, because we checked before.
547            let id = *unsafe {
548                sparse_set_a.entities()
549                    .get_unchecked(self.index)
550            };
551            let data_a = unsafe {
552                sparse_set_a.data_mut().get_unchecked_mut(self.index)
553            };
554            // Unwrap here
555            // This panics while group is destroyed.
556            // But group is matained by internal.
557            // So it never fails
558            let data_b = sparse_set_b.get_mut(id).unwrap();
559            self.index += 1;
560            Some((id,(data_a,data_b)))
561        } else {
562            None
563        }
564    }
565}