Skip to content

Commit

Permalink
feat: support Entity and EntityRef directly in #[system]
Browse files Browse the repository at this point in the history
feat: neq fetch filter
  • Loading branch information
ten3roberts committed Nov 26, 2024
1 parent 1ebafa2 commit 4ffc4d9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion benches/common/serialize_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Benchmark {
pub fn run_row(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
Expand Down
53 changes: 33 additions & 20 deletions flax-derive/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,30 +206,43 @@ fn component_ctor_from_type(
path: Path { segments, .. },
..
}) => {
if segments.len() == 1 && segments[0].ident == "Option" {
let inner = match &segments[0].arguments {
syn::PathArguments::AngleBracketed(args) => {
let GenericArgument::Type(ty) = &args.args[0] else {
match segments.last().map(|v| v.ident.to_string()).as_deref() {
Some("Option") => {
let inner = match &segments[0].arguments {
syn::PathArguments::AngleBracketed(args) => {
let GenericArgument::Type(ty) = &args.args[0] else {
return Err(syn::Error::new(
ident.span(),
"Malformed option generic argument list",
));
};

component_ctor_from_type(crate_name, ident, ty)?
}
_ => {
return Err(syn::Error::new(
ident.span(),
"Malformed option generic argument list",
));
};
"Expected a single angle bracketed type",
))
}
};

component_ctor_from_type(crate_name, ident, ty)?
}
_ => {
return Err(syn::Error::new(
ident.span(),
"Expected a single angle bracketed type",
))
}
};

quote!(#crate_name::fetch::FetchExt::opt(#inner))
} else {
quote!(#crate_name::fetch::FetchExt::copied(#ident()))
quote!(#crate_name::fetch::FetchExt::opt(#inner))
}
Some("Entity") => {
quote!(#crate_name::entity_ids())
}
Some("EntityRef") => {
quote!(#crate_name::fetch::entity_refs())
}
_ => {
quote!(#crate_name::fetch::FetchExt::copied(#ident()))
}
}

// if segments.len() == 1 && segments[0].ident == "Option" {
// } else {
// }
}
_ => return Err(syn::Error::new(ident.span(), "Unsupported type")),
};
Expand Down
10 changes: 9 additions & 1 deletion src/fetch/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::borrow::Cow;

use crate::{
component::ComponentValue,
filter::{Cmp, Equal, Filtered, Greater, GreaterEq, Less, LessEq},
filter::{Cmp, Equal, Filtered, Greater, GreaterEq, Less, LessEq, NotEqual},
relation::RelationExt,
Fetch, FetchItem,
};
Expand Down Expand Up @@ -120,6 +120,14 @@ pub trait FetchExt: Sized {
Cmp::new(self, Equal(other))
}

/// Filter any component equal to `other`.
fn neq<T>(self, other: T) -> Cmp<Self, NotEqual<T>>
where
for<'x> Cmp<Self, NotEqual<T>>: Fetch<'x>,
{
Cmp::new(self, NotEqual(other))
}

/// Set the source entity for the fetch.
///
/// This allows fetching or joining queries
Expand Down
17 changes: 17 additions & 0 deletions src/filter/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ trait CmpMethod<L> {
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct Less<R>(pub R);

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct Greater<R>(pub R);

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct Equal<R>(pub R);

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct NotEqual<R>(pub R);

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct LessEq<R>(pub R);

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct GreaterEq<R>(pub R);
Expand Down Expand Up @@ -94,6 +102,15 @@ where
}
}

impl<L, R> CmpMethod<L> for NotEqual<R>
where
L: for<'x> PartialEq<&'x R>,
{
fn compare(&self, lhs: L) -> bool {
!lhs.eq(&&self.0)
}
}

impl<T, F> CmpMethod<T> for F
where
F: Fn(T) -> bool,
Expand Down
2 changes: 1 addition & 1 deletion src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
};

pub use change::ChangeFilter;
pub use cmp::{Cmp, Equal, Greater, GreaterEq, Less, LessEq};
pub use cmp::{Cmp, Equal, Greater, GreaterEq, Less, LessEq, NotEqual};
pub(crate) use constant::NoEntities;
pub use constant::{All, Nothing};
pub use set::{And, Not, Or, Union};
Expand Down

0 comments on commit 4ffc4d9

Please sign in to comment.