Skip to content

Commit

Permalink
- Queries now fetch their archetypes directly by field rather than us…
Browse files Browse the repository at this point in the history
…ing the turbofish accessor, this allows a sneaky trick in queries that enables cross-archetype nested mutability (including create/destroy!)
  • Loading branch information
recatek committed Jul 20, 2024
1 parent 0d6d9ef commit 881ef1e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
28 changes: 22 additions & 6 deletions macros/src/generate/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned};

use crate::data::{DataArchetype, DataWorld};
use crate::generate::util::to_snake;

use crate::parse::{
ParseQueryFind, //.
Expand Down Expand Up @@ -75,10 +76,15 @@ pub fn generate_query_find(
.map(|p| to_type(p, &archetype))
.collect::<Vec<_>>(); // Bind-dependent!

// Variables
let archetype = format_ident!("{}", to_snake(&archetype.name));

// Fetch the archetype directly to allow queries to be sneaky with
// direct archetype access to get cross-archetype nested mutability
#[rustfmt::skip]
let get_archetype = match mode {
FetchMode::Borrow => quote!(#world.archetype::<#Archetype>()),
FetchMode::Mut => quote!(#world.archetype_mut::<#Archetype>()),
FetchMode::Borrow => quote!(&#world.#archetype),
FetchMode::Mut => quote!(&mut #world.#archetype),
};

#[rustfmt::skip]
Expand Down Expand Up @@ -248,10 +254,15 @@ pub fn generate_query_iter(
.map(|p| to_type(p, &archetype))
.collect::<Vec<_>>(); // Bind-dependent!

// Variables
let archetype = format_ident!("{}", to_snake(&archetype.name));

// Fetch the archetype directly to allow queries to be sneaky with
// direct archetype access to get cross-archetype nested mutability
#[rustfmt::skip]
let get_archetype = match mode {
FetchMode::Borrow => quote!(#world.archetype::<#Archetype>()),
FetchMode::Mut => quote!(#world.archetype_mut::<#Archetype>()),
FetchMode::Borrow => quote!(&#world.#archetype),
FetchMode::Mut => quote!(&mut #world.#archetype),
};

#[rustfmt::skip]
Expand Down Expand Up @@ -342,10 +353,15 @@ pub fn generate_query_iter_destroy(
.map(|p| to_type(p, &archetype))
.collect::<Vec<_>>(); // Bind-dependent!

// Variables
let archetype = format_ident!("{}", to_snake(&archetype.name));

// Fetch the archetype directly to allow queries to be sneaky with
// direct archetype access to get cross-archetype nested mutability
#[rustfmt::skip]
let get_archetype = match mode {
FetchMode::Borrow => panic!("borrow unsupported for iter_remove"),
FetchMode::Mut => quote!(#world.archetype_mut::<#Archetype>()),
FetchMode::Borrow => quote!(&#world.#archetype),
FetchMode::Mut => quote!(&mut #world.#archetype),
};

#[rustfmt::skip]
Expand Down
2 changes: 1 addition & 1 deletion macros/src/generate/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pub fn generate_ecs_component_id(util: ParseEcsComponentId) -> TokenStream {
quote!(#archetype::#get_id_component())
}

fn to_snake(name: &String) -> String {
pub fn to_snake(name: &String) -> String {
name.from_case(Case::Pascal).to_case(Case::Snake)
}
6 changes: 1 addition & 5 deletions macros/src/generate/world.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use xxhash_rust::xxh3::xxh3_128;

use crate::data::{DataArchetype, DataCapacity, DataWorld};
use crate::generate::util::to_snake;

#[allow(non_snake_case)] // Allow for type-like names to make quote!() clearer
#[allow(unused_variables)] // For unused feature-controlled generation elements
Expand Down Expand Up @@ -881,7 +881,3 @@ fn with_capacity_new(archetype_data: &DataArchetype) -> TokenStream {
DataCapacity::Dynamic => quote!(with_capacity(#archetype_capacity)),
}
}

fn to_snake(name: &String) -> String {
name.from_case(Case::Pascal).to_case(Case::Snake)
}

0 comments on commit 881ef1e

Please sign in to comment.