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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use core::{any::TypeId, marker::PhantomData, ptr::NonNull};
use crate::{archetype::Archetype, epoch::EpochId};
use super::{phantom::PhantomQuery, Access, Fetch, IntoQuery, Query};
pub struct FetchWrite<'a, T> {
ptr: NonNull<T>,
entity_epochs: NonNull<EpochId>,
chunk_epochs: NonNull<EpochId>,
epoch: EpochId,
marker: PhantomData<&'a mut [T]>,
}
unsafe impl<'a, T> Fetch<'a> for FetchWrite<'a, T>
where
T: Send + 'a,
{
type Item = &'a mut T;
#[inline]
fn dangling() -> Self {
FetchWrite {
ptr: NonNull::dangling(),
entity_epochs: NonNull::dangling(),
chunk_epochs: NonNull::dangling(),
epoch: EpochId::start(),
marker: PhantomData,
}
}
#[inline]
unsafe fn skip_chunk(&mut self, _: usize) -> bool {
false
}
#[inline]
unsafe fn skip_item(&mut self, _: usize) -> bool {
false
}
#[inline]
unsafe fn visit_chunk(&mut self, chunk_idx: usize) {
let chunk_epoch = &mut *self.chunk_epochs.as_ptr().add(chunk_idx);
chunk_epoch.bump(self.epoch);
}
#[inline]
unsafe fn get_item(&mut self, idx: usize) -> &'a mut T {
let entity_epoch = &mut *self.entity_epochs.as_ptr().add(idx);
entity_epoch.bump(self.epoch);
&mut *self.ptr.as_ptr().add(idx)
}
}
impl<T> IntoQuery for &mut T
where
T: Send + 'static,
{
type Query = PhantomData<fn() -> Self>;
}
unsafe impl<T> PhantomQuery for &mut T
where
T: Send + 'static,
{
type Item<'a> = &'a mut T;
type Fetch<'a> = FetchWrite<'a, T>;
#[inline]
fn access(ty: TypeId) -> Option<Access> {
if ty == TypeId::of::<T>() {
Some(Access::Write)
} else {
None
}
}
#[inline]
fn skip_archetype(archetype: &Archetype) -> bool {
!archetype.has_component(TypeId::of::<T>())
}
#[inline]
unsafe fn access_archetype(_archetype: &Archetype, f: &dyn Fn(TypeId, Access)) {
f(TypeId::of::<T>(), Access::Write)
}
#[inline]
unsafe fn fetch<'a>(archetype: &'a Archetype, epoch: EpochId) -> FetchWrite<'a, T> {
debug_assert_ne!(archetype.len(), 0, "Empty archetypes must be skipped");
let component = archetype.component(TypeId::of::<T>()).unwrap_unchecked();
debug_assert_eq!(component.id(), TypeId::of::<T>());
let data = component.data_mut();
data.epoch.bump(epoch);
FetchWrite {
ptr: data.ptr.cast(),
entity_epochs: NonNull::new_unchecked(data.entity_epochs.as_mut_ptr()),
chunk_epochs: NonNull::new_unchecked(data.chunk_epochs.as_mut_ptr()),
epoch,
marker: PhantomData,
}
}
}
pub fn write<T>() -> PhantomData<fn() -> &'static mut T>
where
T: Send,
for<'a> PhantomData<fn() -> &'a mut T>: Query,
{
PhantomData
}