Skip to content

Commit 3e1c5b0

Browse files
committed
fix: conditional and unconditional yield of optional filtered queries
1 parent 9c564c3 commit 3e1c5b0

21 files changed

+147
-18
lines changed

flax-derive/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,15 @@ fn derive_union(params: &Params) -> TokenStream {
211211

212212
let prep_ty = params.w_ty();
213213

214+
// Make sure not to *or* ignored fields
214215
let filter_fields = fields.iter().filter(|v| !v.attrs.ignore).map(|v| v.ident);
216+
let filter_types = fields.iter().filter(|v| !v.attrs.ignore).map(|v| v.ty);
215217

216218
quote! {
217219
#[automatically_derived]
218220
impl #impl_generics #crate_name::fetch::UnionFilter for #prepared_name #prep_ty where #prepared_name #prep_ty: #crate_name::fetch::PreparedFetch<'q> {
221+
const HAS_UNION_FILTER: bool = #(<<#filter_types as #crate_name::fetch::Fetch<'w>>::Prepared as #crate_name::fetch::PreparedFetch<'q>>::HAS_FILTER)&&*;
222+
219223
unsafe fn filter_union(&mut self, slots: #crate_name::archetype::Slice) -> #crate_name::archetype::Slice {
220224
#crate_name::fetch::PreparedFetch::filter_slots(&mut #crate_name::filter::Union((#(&mut self.#filter_fields,)*)), slots)
221225
}
@@ -386,6 +390,8 @@ fn derive_prepared_struct(params: &Params) -> TokenStream {
386390
type Item = #item_name #item_ty;
387391
type Chunk = (#(<<#field_types as #crate_name::fetch::Fetch<'w>>::Prepared as #crate_name::fetch::PreparedFetch<'q>>::Chunk,)*);
388392

393+
const HAS_FILTER: bool = #(<<#field_types as #crate_name::fetch::Fetch<'w>>::Prepared as #crate_name::fetch::PreparedFetch<'q>>::HAS_FILTER)||*;
394+
389395
#[inline]
390396
unsafe fn fetch_next(chunk: &mut Self::Chunk) -> Self::Item {
391397
Self::Item {

src/fetch/as_deref.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ where
5656
V: 'static + Deref,
5757
{
5858
type Item = &'q V::Target;
59-
6059
type Chunk = F::Chunk;
6160

61+
const HAS_FILTER: bool = F::HAS_FILTER;
62+
6263
unsafe fn filter_slots(&mut self, slots: crate::archetype::Slice) -> crate::archetype::Slice {
6364
self.0.filter_slots(slots)
6465
}

src/fetch/cloned.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ where
7373
type Item = V;
7474
type Chunk = F::Chunk;
7575

76+
const HAS_FILTER: bool = F::HAS_FILTER;
77+
7678
unsafe fn create_chunk(&'q mut self, slots: Slice) -> Self::Chunk {
7779
self.0.create_chunk(slots)
7880
}

src/fetch/component.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ impl<'w, 'q, T: 'q> PreparedFetch<'q> for ReadComponent<'w, T> {
1414

1515
type Chunk = Ptr<'q, T>;
1616

17+
const HAS_FILTER: bool = false;
18+
1719
#[inline]
1820
unsafe fn create_chunk(&'q mut self, slots: Slice) -> Self::Chunk {
1921
Ptr::new(self.borrow[slots.as_range()].as_ptr())

src/fetch/component_mut.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ pub struct WriteComponent<'a, T> {
7878
impl<'w, 'q, T: 'q + ComponentValue> PreparedFetch<'q> for WriteComponent<'w, T> {
7979
type Item = &'q mut T;
8080
type Chunk = PtrMut<'q, T>;
81+
82+
const HAS_FILTER: bool = false;
8183

8284
unsafe fn create_chunk(&'q mut self, slots: Slice) -> Self::Chunk {
8385
self.guard

src/fetch/copied.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ where
7070
type Item = V;
7171
type Chunk = F::Chunk;
7272

73+
const HAS_FILTER: bool = F::HAS_FILTER;
74+
7375
unsafe fn create_chunk(&'q mut self, slots: Slice) -> Self::Chunk {
7476
self.0.create_chunk(slots)
7577
}

src/fetch/entity_ref.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub struct Batch<'a> {
7474
impl<'w, 'q> PreparedFetch<'q> for PreparedEntityRef<'w> {
7575
type Item = EntityRef<'q>;
7676
type Chunk = Batch<'q>;
77+
const HAS_FILTER: bool = false;
7778

7879
unsafe fn create_chunk(&'q mut self, slice: crate::archetype::Slice) -> Self::Chunk {
7980
Batch {

src/fetch/ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::{
1919
pub trait FetchExt: Sized {
2020
/// Transform the fetch into an optional fetch, yielding Some or None
2121
fn opt(self) -> Opt<Self> {
22-
Opt(self)
22+
Opt { fetch: self }
2323
}
2424

2525
/// Transform the fetch into a fetch with a provided default.

src/fetch/map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ where
7272

7373
type Chunk = (&'q F, Q::Chunk);
7474

75+
const HAS_FILTER: bool = Q::HAS_FILTER;
76+
7577
unsafe fn create_chunk(&'q mut self, slots: crate::archetype::Slice) -> Self::Chunk {
7678
(self.func, self.query.create_chunk(slots))
7779
}

src/fetch/maybe_mut.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ impl<'w, 'q, T: ComponentValue> PreparedFetch<'q> for PreparedMaybeMut<'w, T> {
9090
type Item = MutGuard<'q, T>;
9191
type Chunk = Batch<'q>;
9292

93+
const HAS_FILTER: bool = false;
94+
9395
unsafe fn create_chunk(&'q mut self, slice: crate::archetype::Slice) -> Self::Chunk {
9496
Batch {
9597
cell: self.cell,

0 commit comments

Comments
 (0)