Skip to content

Commit c723e60

Browse files
committed
chore(ECS): fix ECS related changes
- added `IS_ARCHETYPAL = false` everywhere - added empty `iter_access` everywhere - `fetch` now returns `Option<Item>` - because of this `QueryFilter` needed an update via `is_some_and`
1 parent 2b1c1a8 commit c723e60

File tree

7 files changed

+92
-30
lines changed

7 files changed

+92
-30
lines changed

bevy-trait-query-impl/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
154154
type ReadOnly = Self;
155155

156156
const IS_READ_ONLY: bool = true;
157+
const IS_ARCHETYPAL: bool = false;
157158

158159
type Item<'__w, '__s> = #my_crate::ReadTraits<'__w, #trait_object>;
159160

@@ -170,14 +171,20 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
170171
fetch: &mut Self::Fetch<'w>,
171172
entity: #imports::Entity,
172173
table_row: #imports::TableRow,
173-
) -> Self::Item<'w, 's> {
174+
) -> Option<Self::Item<'w, 's>> {
174175
<#my_crate::All<&#trait_object> as #imports::QueryData>::fetch(
175176
state,
176177
fetch,
177178
entity,
178179
table_row,
179180
)
180181
}
182+
183+
fn iter_access(
184+
_state: &Self::State,
185+
) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
186+
std::iter::empty()
187+
}
181188
}
182189
unsafe impl #impl_generics #imports::ReadOnlyQueryData for &#trait_object
183190
#where_clause
@@ -270,6 +277,7 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
270277
type Item<'__w, '__s> = #my_crate::WriteTraits<'__w, #trait_object>;
271278

272279
const IS_READ_ONLY: bool = false;
280+
const IS_ARCHETYPAL: bool = false;
273281

274282
#[inline]
275283
fn shrink<'wlong: 'wshort, 'wshort, 's>(
@@ -284,14 +292,20 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
284292
fetch: &mut Self::Fetch<'w>,
285293
entity: #imports::Entity,
286294
table_row: #imports::TableRow,
287-
) -> Self::Item<'w, 's> {
295+
) -> Option<Self::Item<'w, 's>> {
288296
<#my_crate::All<&mut #trait_object> as #imports::QueryData>::fetch(
289297
state,
290298
fetch,
291299
entity,
292300
table_row,
293301
)
294302
}
303+
304+
fn iter_access(
305+
_state: &Self::State,
306+
) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
307+
std::iter::empty()
308+
}
295309
}
296310

297311
unsafe impl #impl_generics_with_lifetime #imports::WorldQuery for &'__a mut #trait_object

bevy-trait-query/src/all/core/read.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for ReadSparseTraitsIter<'a, Trait
111111
unsafe { zip_exact(&mut self.components, &mut self.meta) }.find_map(
112112
|(&component, meta)| {
113113
let set = self.sparse_sets.get(component)?;
114-
let (ptr, ticks, location) = set.get_with_ticks(self.entity)?;
115-
Some((ptr, ticks, meta, location))
114+
let (ptr, ticks) = set.get_with_ticks(self.entity)?;
115+
Some((ptr, ticks, meta, ticks.changed_by))
116116
},
117117
)?;
118118
let trait_object = unsafe { meta.dyn_ctor.cast(ptr) };

bevy-trait-query/src/all/core/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for WriteSparseTraitsIter<'a, Trai
122122
unsafe { zip_exact(&mut self.components, &mut self.meta) }.find_map(
123123
|(&component, meta)| {
124124
let set = self.sparse_sets.get(component)?;
125-
let (ptr, ticks, location) = set.get_with_ticks(self.entity)?;
126-
Some((ptr, ticks, meta, location))
125+
let (ptr, ticks) = set.get_with_ticks(self.entity)?;
126+
Some((ptr, ticks, meta, ticks.changed_by))
127127
},
128128
)?;
129129

bevy-trait-query/src/all/impls/all.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for All<&Trait> {
3131
type ReadOnly = Self;
3232

3333
const IS_READ_ONLY: bool = true;
34+
const IS_ARCHETYPAL: bool = false;
3435

3536
type Item<'w, 's> = ReadTraits<'w, Trait>;
3637

@@ -47,19 +48,25 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for All<&Trait> {
4748
fetch: &mut Self::Fetch<'w>,
4849
_entity: Entity,
4950
table_row: TableRow,
50-
) -> Self::Item<'w, 's> {
51+
) -> Option<Self::Item<'w, 's>> {
5152
let table = fetch
5253
.table
5354
.unwrap_or_else(|| unsafe { debug_unreachable() });
5455

55-
ReadTraits {
56+
Some(ReadTraits {
5657
registry: fetch.registry,
5758
table,
5859
table_row,
5960
sparse_sets: fetch.sparse_sets,
6061
last_run: fetch.last_run,
6162
this_run: fetch.this_run,
62-
}
63+
})
64+
}
65+
66+
fn iter_access(
67+
_state: &Self::State,
68+
) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
69+
std::iter::empty()
6370
}
6471
}
6572
unsafe impl<Trait: ?Sized + TraitQuery> ReadOnlyQueryData for All<&Trait> {}
@@ -166,6 +173,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for All<&'a mut Trait> {
166173
type ReadOnly = All<&'a Trait>;
167174

168175
const IS_READ_ONLY: bool = false;
176+
const IS_ARCHETYPAL: bool = false;
169177

170178
type Item<'w, 's> = WriteTraits<'w, Trait>;
171179

@@ -182,19 +190,25 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for All<&'a mut Trait> {
182190
fetch: &mut Self::Fetch<'w>,
183191
_entity: Entity,
184192
table_row: TableRow,
185-
) -> Self::Item<'w, 's> {
193+
) -> Option<Self::Item<'w, 's>> {
186194
let table = fetch
187195
.table
188196
.unwrap_or_else(|| unsafe { debug_unreachable() });
189197

190-
WriteTraits {
198+
Some(WriteTraits {
191199
registry: fetch.registry,
192200
table,
193201
table_row,
194202
sparse_sets: fetch.sparse_sets,
195203
last_run: fetch.last_run,
196204
this_run: fetch.this_run,
197-
}
205+
})
206+
}
207+
208+
fn iter_access(
209+
_state: &Self::State,
210+
) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
211+
std::iter::empty()
198212
}
199213
}
200214

bevy-trait-query/src/one/impls/one.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for One<&Trait> {
2727
type ReadOnly = Self;
2828

2929
const IS_READ_ONLY: bool = true;
30+
const IS_ARCHETYPAL: bool = false;
3031

3132
type Item<'w, 's> = Ref<'w, Trait>;
3233

@@ -43,7 +44,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for One<&Trait> {
4344
fetch: &mut Self::Fetch<'w>,
4445
entity: Entity,
4546
table_row: TableRow,
46-
) -> Self::Item<'w, 's> {
47+
) -> Option<Self::Item<'w, 's>> {
4748
unsafe {
4849
let table_row = table_row.index();
4950
let (dyn_ctor, ptr, added, changed, location) = match fetch.storage {
@@ -69,7 +70,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for One<&Trait> {
6970
)
7071
}
7172
FetchStorage::SparseSet { components, meta } => {
72-
let (ptr, ticks, location) = components
73+
let (ptr, ticks) = components
7374
.get_with_ticks(entity)
7475
.unwrap_or_else(|| debug_unreachable());
7576
(
@@ -79,21 +80,27 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for One<&Trait> {
7980
// we have access to the corresponding `ComponentTicks`.
8081
ticks.added.deref(),
8182
ticks.changed.deref(),
82-
location,
83+
ticks.changed_by,
8384
)
8485
}
8586
};
8687

87-
Ref::new(
88+
Some(Ref::new(
8889
dyn_ctor.cast(ptr),
8990
added,
9091
changed,
9192
fetch.last_run,
9293
fetch.this_run,
9394
location.map(|loc| loc.deref()),
94-
)
95+
))
9596
}
9697
}
98+
99+
fn iter_access(
100+
_state: &Self::State,
101+
) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
102+
std::iter::empty()
103+
}
97104
}
98105

99106
unsafe impl<Trait: ?Sized + TraitQuery> ReadOnlyQueryData for One<&Trait> {}
@@ -236,6 +243,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for One<&'a mut Trait> {
236243
type ReadOnly = One<&'a Trait>;
237244

238245
const IS_READ_ONLY: bool = false;
246+
const IS_ARCHETYPAL: bool = false;
239247

240248
type Item<'w, 's> = Mut<'w, Trait>;
241249

@@ -252,7 +260,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for One<&'a mut Trait> {
252260
fetch: &mut Self::Fetch<'w>,
253261
entity: Entity,
254262
table_row: TableRow,
255-
) -> Mut<'w, Trait> {
263+
) -> Option<Mut<'w, Trait>> {
256264
unsafe {
257265
let table_row = table_row.index();
258266
let (dyn_ctor, ptr, added, changed, location) = match fetch.storage {
@@ -281,7 +289,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for One<&'a mut Trait> {
281289
)
282290
}
283291
FetchStorage::SparseSet { components, meta } => {
284-
let (ptr, ticks, location) = components
292+
let (ptr, ticks) = components
285293
.get_with_ticks(entity)
286294
.unwrap_or_else(|| debug_unreachable());
287295
(
@@ -294,21 +302,27 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for One<&'a mut Trait> {
294302
// we have exclusive access to the corresponding `ComponentTicks`.
295303
ticks.added.deref_mut(),
296304
ticks.changed.deref_mut(),
297-
location,
305+
ticks.changed_by,
298306
)
299307
}
300308
};
301309

302-
Mut::new(
310+
Some(Mut::new(
303311
dyn_ctor.cast_mut(ptr),
304312
added,
305313
changed,
306314
fetch.last_run,
307315
fetch.this_run,
308316
location.map(|loc| loc.deref_mut()),
309-
)
317+
))
310318
}
311319
}
320+
321+
fn iter_access(
322+
_state: &Self::State,
323+
) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
324+
std::iter::empty()
325+
}
312326
}
313327

314328
// SAFETY: We only access the components registered in TraitQueryState.

bevy-trait-query/src/one/impls/one_added.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for OneAdded<Trait> {
2525
type ReadOnly = Self;
2626

2727
const IS_READ_ONLY: bool = true;
28+
const IS_ARCHETYPAL: bool = false;
2829

2930
type Item<'w, 's> = bool;
3031

@@ -40,7 +41,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for OneAdded<Trait> {
4041
fetch: &mut Self::Fetch<'w>,
4142
entity: Entity,
4243
table_row: TableRow,
43-
) -> Self::Item<'w, 's> {
44+
) -> Option<Self::Item<'w, 's>> {
4445
unsafe {
4546
let ticks_ptr = match fetch.storage {
4647
ChangeDetectionStorage::Uninit => {
@@ -53,11 +54,19 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for OneAdded<Trait> {
5354
.unwrap_or_else(|| debug_unreachable()),
5455
};
5556

56-
(*ticks_ptr)
57-
.deref()
58-
.is_newer_than(fetch.last_run, fetch.this_run)
57+
Some(
58+
(*ticks_ptr)
59+
.deref()
60+
.is_newer_than(fetch.last_run, fetch.this_run),
61+
)
5962
}
6063
}
64+
65+
fn iter_access(
66+
_state: &Self::State,
67+
) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
68+
std::iter::empty()
69+
}
6170
}
6271

6372
unsafe impl<Trait: ?Sized + TraitQuery> WorldQuery for OneAdded<Trait> {
@@ -182,5 +191,6 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryFilter for OneAdded<Trait> {
182191
table_row: TableRow,
183192
) -> bool {
184193
unsafe { <Self as QueryData>::fetch(state, fetch, entity, table_row) }
194+
.is_some_and(|inner_true| inner_true)
185195
}
186196
}

bevy-trait-query/src/one/impls/one_changed.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for OneChanged<Trait> {
2626

2727
/// SAFETY: read-only access
2828
const IS_READ_ONLY: bool = true;
29+
const IS_ARCHETYPAL: bool = false;
2930

3031
type Item<'w, 's> = bool;
3132

@@ -41,7 +42,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for OneChanged<Trait> {
4142
fetch: &mut Self::Fetch<'w>,
4243
entity: Entity,
4344
table_row: TableRow,
44-
) -> Self::Item<'w, 's> {
45+
) -> Option<Self::Item<'w, 's>> {
4546
unsafe {
4647
let ticks_ptr = match fetch.storage {
4748
ChangeDetectionStorage::Uninit => {
@@ -54,11 +55,19 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryData for OneChanged<Trait> {
5455
.unwrap_or_else(|| debug_unreachable()),
5556
};
5657

57-
(*ticks_ptr)
58-
.deref()
59-
.is_newer_than(fetch.last_run, fetch.this_run)
58+
Some(
59+
(*ticks_ptr)
60+
.deref()
61+
.is_newer_than(fetch.last_run, fetch.this_run),
62+
)
6063
}
6164
}
65+
66+
fn iter_access(
67+
_state: &Self::State,
68+
) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
69+
std::iter::empty()
70+
}
6271
}
6372

6473
unsafe impl<Trait: ?Sized + TraitQuery> WorldQuery for OneChanged<Trait> {
@@ -182,5 +191,6 @@ unsafe impl<Trait: ?Sized + TraitQuery> QueryFilter for OneChanged<Trait> {
182191
table_row: TableRow,
183192
) -> bool {
184193
unsafe { <Self as QueryData>::fetch(state, fetch, entity, table_row) }
194+
.is_some_and(|inner_true| inner_true)
185195
}
186196
}

0 commit comments

Comments
 (0)