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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use core::{any::TypeId, marker::PhantomData, ptr::NonNull};
use crate::{
archetype::Archetype,
epoch::EpochId,
query::{Access, Fetch, ImmutableQuery, IntoQuery, QueryFetch},
system::{QueryArg, QueryArgCache, QueryArgGet},
Modified, PhantomQuery, Query, World,
};
use super::ModifiedCache;
pub struct ModifiedFetchRead<'a, T> {
after_epoch: EpochId,
ptr: NonNull<T>,
entity_epochs: NonNull<EpochId>,
chunk_epochs: NonNull<EpochId>,
marker: PhantomData<&'a [T]>,
}
unsafe impl<'a, T> Fetch<'a> for ModifiedFetchRead<'a, T>
where
T: Sync + 'a,
{
type Item = &'a T;
#[inline]
fn dangling() -> Self {
ModifiedFetchRead {
after_epoch: EpochId::start(),
ptr: NonNull::dangling(),
entity_epochs: NonNull::dangling(),
chunk_epochs: NonNull::dangling(),
marker: PhantomData,
}
}
#[inline]
unsafe fn skip_chunk(&mut self, chunk_idx: usize) -> bool {
let chunk_epoch = *self.chunk_epochs.as_ptr().add(chunk_idx);
!chunk_epoch.after(self.after_epoch)
}
#[inline]
unsafe fn skip_item(&mut self, idx: usize) -> bool {
let epoch = *self.entity_epochs.as_ptr().add(idx);
!epoch.after(self.after_epoch)
}
#[inline]
unsafe fn visit_chunk(&mut self, _: usize) {}
#[inline]
unsafe fn get_item(&mut self, idx: usize) -> &'a T {
&*self.ptr.as_ptr().add(idx)
}
}
impl<'a, T> QueryFetch<'a> for Modified<&T>
where
T: Sync + 'a,
{
type Item = &'a T;
type Fetch = ModifiedFetchRead<'a, T>;
}
impl<T> IntoQuery for Modified<&T>
where
T: Sync + 'static,
{
type Query = Self;
}
unsafe impl<T> Query for Modified<&T>
where
T: Sync + 'static,
{
#[inline]
fn access(&self, ty: TypeId) -> Option<Access> {
<&T as PhantomQuery>::access(ty)
}
#[inline]
fn skip_archetype(&self, archetype: &Archetype) -> bool {
match archetype.component(TypeId::of::<T>()) {
None => true,
Some(component) => unsafe {
debug_assert_eq!(<&T as PhantomQuery>::skip_archetype(archetype), false);
debug_assert_eq!(component.id(), TypeId::of::<T>());
let data = component.data();
!data.epoch.after(self.after_epoch)
},
}
}
#[inline]
unsafe fn access_archetype(&self, _archetype: &Archetype, f: &dyn Fn(TypeId, Access)) {
f(TypeId::of::<T>(), Access::Read)
}
#[inline]
unsafe fn fetch<'a>(
&mut self,
archetype: &'a Archetype,
_epoch: EpochId,
) -> ModifiedFetchRead<'a, T> {
debug_assert_ne!(archetype.len(), 0, "Empty archetypes must be skipped");
let component = archetype.component(TypeId::of::<T>()).unwrap_unchecked();
let data = component.data();
debug_assert!(data.epoch.after(self.after_epoch));
ModifiedFetchRead {
after_epoch: self.after_epoch,
ptr: data.ptr.cast(),
entity_epochs: NonNull::new_unchecked(data.entity_epochs.as_ptr() as *mut EpochId),
chunk_epochs: NonNull::new_unchecked(data.chunk_epochs.as_ptr() as *mut EpochId),
marker: PhantomData,
}
}
}
unsafe impl<T> ImmutableQuery for Modified<&T> where T: Sync + 'static {}
impl<'a, T> QueryArgGet<'a> for ModifiedCache<&'static T>
where
T: Sync + 'static,
{
type Arg = Modified<&'a T>;
type Query = Modified<&'a T>;
#[inline]
fn get(&mut self, world: &'a World) -> Modified<&'a T> {
let after_epoch = core::mem::replace(&mut self.after_epoch, world.epoch());
Modified {
after_epoch,
marker: PhantomData,
}
}
}
impl<T> QueryArgCache for ModifiedCache<&'static T>
where
T: Sync + 'static,
{
fn access_component(&self, id: TypeId) -> Option<Access> {
<&T as PhantomQuery>::access(id)
}
fn skips_archetype(&self, archetype: &Archetype) -> bool {
<&T as PhantomQuery>::skip_archetype(archetype)
}
}
impl<'a, T> QueryArg for Modified<&'a T>
where
T: Sync + 'static,
{
type Cache = ModifiedCache<&'static T>;
}