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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::components::Component;
use crate::group::GroupInfo;
use crate::query::{Contains, QueryElementFilter};
use crate::storage::{Entity, SparseArrayView};
use crate::utils::{ChangeTicks, Ticks};
use std::marker::PhantomData;
pub unsafe trait QueryElement<'a> {
type Item: 'a;
type Component: Component;
type Filter: QueryElementFilter<Self::Component>;
fn get(self, entity: Entity) -> Option<Self::Item>;
fn get_with_ticks(&self, entity: Entity) -> Option<(&Self::Component, &ChangeTicks)>;
fn contains(&self, entity: Entity) -> bool;
fn group_info(&self) -> Option<GroupInfo<'a>>;
fn world_tick(&self) -> Ticks;
fn change_tick(&self) -> Ticks;
fn split(self) -> SplitQueryElement<'a, Self::Component, Self::Filter>;
unsafe fn get_from_parts(
component: *mut Self::Component,
ticks: *mut ChangeTicks,
world_tick: Ticks,
change_tick: Ticks,
) -> Self::Item;
}
pub unsafe trait ImmutableQueryElement<'a>
where
Self: QueryElement<'a>,
{
}
pub trait UnfilteredQueryElement<'a>
where
Self: QueryElement<'a, Filter = Contains>,
{
}
impl<'a, E> UnfilteredQueryElement<'a> for E
where
E: QueryElement<'a, Filter = Contains>,
{
}
pub unsafe trait UnfilteredImmutableQueryElement<'a>
where
Self: ImmutableQueryElement<'a> + UnfilteredQueryElement<'a>,
{
fn entities(&self) -> &'a [Entity];
fn components(&self) -> &'a [Self::Component];
}
#[non_exhaustive]
pub struct SplitQueryElement<'a, T, F> {
pub sparse: SparseArrayView<'a>,
pub entities: &'a [Entity],
pub components: *mut T,
pub ticks: *mut ChangeTicks,
pub filter: F,
}
impl<'a, T, F> SplitQueryElement<'a, T, F> {
pub fn new(
sparse: SparseArrayView<'a>,
entities: &'a [Entity],
components: *mut T,
ticks: *mut ChangeTicks,
filter: F,
) -> Self {
Self {
sparse,
entities,
components,
ticks,
filter,
}
}
pub fn into_sparse_split(self) -> (&'a [Entity], SparseSplitQueryElement<'a, T, F>) {
(
self.entities,
SparseSplitQueryElement {
sparse: self.sparse,
components: self.components,
ticks: self.ticks,
filter: self.filter,
},
)
}
pub fn into_dense_split(self) -> (&'a [Entity], DenseSplitQueryElement<'a, T, F>) {
(
self.entities,
DenseSplitQueryElement {
components: self.components,
ticks: self.ticks,
filter: self.filter,
lifetime: PhantomData,
},
)
}
pub fn into_modifier_split(self) -> (&'a [Entity], SparseArrayView<'a>) {
(self.entities, self.sparse)
}
}
#[non_exhaustive]
pub struct SparseSplitQueryElement<'a, T, F> {
pub sparse: SparseArrayView<'a>,
pub components: *mut T,
pub ticks: *mut ChangeTicks,
pub filter: F,
}
impl<'a, T, F> SparseSplitQueryElement<'a, T, F> {
pub unsafe fn get<E>(
&mut self,
entity: Entity,
world_tick: Ticks,
change_tick: Ticks,
) -> Option<E::Item>
where
T: Component,
E: QueryElement<'a, Component = T>,
F: QueryElementFilter<T>,
{
let index = self.sparse.get_index(entity)?;
let component = self.components.add(index);
let ticks = self.ticks.add(index);
self.filter
.matches(&*component, &*ticks, world_tick, change_tick)
.then(|| E::get_from_parts(component, ticks, world_tick, change_tick))
}
}
pub struct DenseSplitQueryElement<'a, T, F> {
pub components: *mut T,
pub ticks: *mut ChangeTicks,
pub filter: F,
lifetime: PhantomData<&'a ()>,
}
impl<'a, T, F> DenseSplitQueryElement<'a, T, F> {
pub unsafe fn get<E>(
&mut self,
index: usize,
world_tick: Ticks,
change_tick: Ticks,
) -> Option<E::Item>
where
T: Component,
E: QueryElement<'a, Component = T>,
F: QueryElementFilter<T>,
{
let component = self.components.add(index);
let ticks = self.ticks.add(index);
self.filter
.matches(&*component, &*ticks, world_tick, change_tick)
.then(|| E::get_from_parts(component, ticks, world_tick, change_tick))
}
}