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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::{
archetype,
component::Component,
query::{
view,
view::{
ParViews,
ParViewsSeal,
RepeatNone,
},
},
registry,
registry::{
contains::{
Contained,
NotContained,
Null,
},
Registry,
},
};
use rayon::{
iter,
iter::{
Either,
IntoParallelRefIterator,
IntoParallelRefMutIterator,
ParallelIterator,
},
};
pub trait CanonicalParViews<'a, V, P>
where
V: ParViews<'a>,
{
/// # Safety
///
/// Each tuple in `columns` must contain the raw parts for a valid `Vec<C>` of size `length`
/// for components `C`, ordered for the archetype identified by `archetype_identifier`.
unsafe fn par_view<R>(
columns: &[(*mut u8, usize)],
length: usize,
archetype_identifier: archetype::identifier::Iter<R>,
) -> V::ParResults
where
R: Registry;
}
impl<'a> CanonicalParViews<'a, view::Null, Null> for registry::Null {
unsafe fn par_view<R>(
_columns: &[(*mut u8, usize)],
length: usize,
_archetype_identifier: archetype::identifier::Iter<R>,
) -> <view::Null as ParViewsSeal<'a>>::ParResults
where
R: Registry,
{
iter::repeatn(view::Null, length)
}
}
impl<'a, C, P, R, V> CanonicalParViews<'a, (&'a C, V), (&'a Contained, P)> for (C, R)
where
C: Component + Sync,
R: CanonicalParViews<'a, V, P>,
V: ParViews<'a>,
{
unsafe fn par_view<R_>(
columns: &[(*mut u8, usize)],
length: usize,
mut archetype_identifier: archetype::identifier::Iter<R_>,
) -> <(&'a C, V) as ParViewsSeal<'a>>::ParResults
where
R_: Registry,
{
archetype_identifier.next();
(
// SAFETY: `columns` is guaranteed to contain raw parts for a valid `Vec<C>` of size
// `length` for the currently viewed component `C`.
unsafe {
core::slice::from_raw_parts::<'a, C>(columns.get_unchecked(0).0.cast::<C>(), length)
}
.par_iter(),
// SAFETY: The remaining components in `columns` are guaranteed to contain raw parts
// for valid `Vec<C>`s of length `length` for each of the remaining components
// identified by `archetype_identifier`.
unsafe { R::par_view(columns.get_unchecked(1..), length, archetype_identifier) },
)
}
}
impl<'a, C, P, R, V> CanonicalParViews<'a, (&'a mut C, V), (&'a mut Contained, P)> for (C, R)
where
C: Component + Send,
R: CanonicalParViews<'a, V, P>,
V: ParViews<'a>,
{
unsafe fn par_view<R_>(
columns: &[(*mut u8, usize)],
length: usize,
mut archetype_identifier: archetype::identifier::Iter<R_>,
) -> <(&'a mut C, V) as ParViewsSeal<'a>>::ParResults
where
R_: Registry,
{
archetype_identifier.next();
(
// SAFETY: `columns` is guaranteed to contain raw parts for a valid `Vec<C>` of size
// `length` for the currently viewed component `C`.
unsafe {
core::slice::from_raw_parts_mut::<'a, C>(
columns.get_unchecked(0).0.cast::<C>(),
length,
)
}
.par_iter_mut(),
// SAFETY: The remaining components in `columns` are guaranteed to contain raw parts
// for valid `Vec<C>`s of length `length` for each of the remaining components
// identified by `archetype_identifier`.
unsafe { R::par_view(columns.get_unchecked(1..), length, archetype_identifier) },
)
}
}
#[allow(clippy::unnecessary_wraps)]
fn wrap_some<T>(val: T) -> Option<T> {
Some(val)
}
impl<'a, C, P, R, V> CanonicalParViews<'a, (Option<&'a C>, V), (Option<&'a Contained>, P)>
for (C, R)
where
C: Component + Sync,
R: CanonicalParViews<'a, V, P>,
V: ParViews<'a>,
{
unsafe fn par_view<R_>(
mut columns: &[(*mut u8, usize)],
length: usize,
mut archetype_identifier: archetype::identifier::Iter<R_>,
) -> <(Option<&'a C>, V) as ParViewsSeal<'a>>::ParResults
where
R_: Registry,
{
(
// SAFETY: `archetype_identifier` is guaranteed to have at least one element remaining.
if unsafe { archetype_identifier.next().unwrap_unchecked() } {
Either::Right(
// SAFETY: `columns` is guaranteed to contain raw parts for a valid `Vec<C>` of
// size `length`. Since `component_map` contains an entry for the given
// component `C`'s entry in `columns`, then the column
// obtained here can be interpreted as a slice of type `C`
// of size `length`.
unsafe {
core::slice::from_raw_parts(
{
let column = columns.get_unchecked(0);
columns = columns.get_unchecked(1..);
column
}
.0
.cast::<C>(),
length,
)
}
.par_iter()
.map(wrap_some),
)
} else {
Either::Left(iter::repeat(None).take(length))
},
// SAFETY: The remaining components in `columns` are guaranteed to contain raw parts
// for valid `Vec<C>`s of length `length` for each of the remaining components
// identified by `archetype_identifier`.
unsafe { R::par_view(columns, length, archetype_identifier) },
)
}
}
impl<'a, C, P, R, V> CanonicalParViews<'a, (Option<&'a mut C>, V), (Option<&'a mut Contained>, P)>
for (C, R)
where
C: Component + Send,
R: CanonicalParViews<'a, V, P>,
V: ParViews<'a>,
{
unsafe fn par_view<R_>(
mut columns: &[(*mut u8, usize)],
length: usize,
mut archetype_identifier: archetype::identifier::Iter<R_>,
) -> <(Option<&'a mut C>, V) as ParViewsSeal<'a>>::ParResults
where
R_: Registry,
{
(
// SAFETY: `archetype_identifier` is guaranteed to have at least one element remaining.
if unsafe { archetype_identifier.next().unwrap_unchecked() } {
Either::Right(
// SAFETY: `columns` is guaranteed to contain raw parts for a valid `Vec<C>` of
// size `length`. Since `component_map` contains an entry for the given
// component `C`'s entry in `columns`, then the column
// obtained here can be interpreted as a slice of type `C`
// of size `length`.
unsafe {
core::slice::from_raw_parts_mut(
{
let column = columns.get_unchecked(0);
columns = columns.get_unchecked(1..);
column
}
.0
.cast::<C>(),
length,
)
}
.par_iter_mut()
.map(wrap_some),
)
} else {
Either::Left(RepeatNone::new(length))
},
// SAFETY: The remaining components in `columns` are guaranteed to contain raw parts
// for valid `Vec<C>`s of length `length` for each of the remaining components
// identified by `archetype_identifier`.
unsafe { R::par_view(columns, length, archetype_identifier) },
)
}
}
impl<'a, C, P, R, V> CanonicalParViews<'a, V, (NotContained, P)> for (C, R)
where
C: Component,
R: CanonicalParViews<'a, V, P>,
V: ParViews<'a>,
{
unsafe fn par_view<R_>(
mut columns: &[(*mut u8, usize)],
length: usize,
mut archetype_identifier: archetype::identifier::Iter<R_>,
) -> V::ParResults
where
R_: Registry,
{
// SAFETY: `archetype_identifier` is guaranteed to have at least one element remaining.
if unsafe { archetype_identifier.next().unwrap_unchecked() } {
// SAFETY: Since `archetype_identifier` has this component set, there is guaranteed to
// be at least one entry in `columns`.
unsafe {
columns = columns.get_unchecked(1..);
}
}
// SAFETY: The remaining components in `columns` are guaranteed to contain raw parts
// for valid `Vec<C>`s of length `length` for each of the remaining components
// identified by `archetype_identifier`.
unsafe { R::par_view(columns, length, archetype_identifier) }
}
}