Created
August 10, 2022 19:59
-
-
Save ItsDoot/c6a6289749963b81b3dc57b6cb559eb0 to your computer and use it in GitHub Desktop.
Original PoC for filtering by component value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Query Filter | |
pub struct Equals<C: Component, Pred: PartialEq<C> + Default> { | |
_c: PhantomData<*const C>, | |
_pred: PhantomData<*const Pred>, | |
} | |
unsafe impl<C: Component, Pred: PartialEq<C> + Default> WorldQuery for Equals<C, Pred> { | |
// All the icky internals that I don't know how to write :( | |
} | |
// Const-generics predicate for the query filter | |
#[derive(Default)] | |
pub struct Is<const VALUE: bool>; | |
impl<C, const VALUE: bool> PartialEq<C> for Is<VALUE> | |
where | |
C: Component + Deref<Target = bool>, | |
{ | |
fn eq(&self, other: &C) -> bool { | |
VALUE == **other | |
} | |
} | |
// User Component | |
#[derive(Component, Deref)] | |
pub struct Poisoned(bool); | |
// We could reduce the type system word salad with some aliases: | |
pub type IsTrue<C: Component> = Equals<C, Is<true>>; | |
pub type IsFalse<C: Component> = Equals<C, Is<false>>; | |
// Example in a query | |
pub fn all_poisoned(entities: Query<&Name, Equals<Poisoned, Is<true>>>) { | |
// ... | |
} | |
// Example with type alias | |
pub fn all_poisoned2(entities: Query<&Name, IsTrue<Poisoned>>) { | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment