Skip to content

Commit

Permalink
Don't clutter caller namespace with derive(Query) internals
Browse files Browse the repository at this point in the history
Rather than giving the Fetch/State types names unlikely to collide, we
can introduce fully hygenic names by moving the type definitions and
the trait impls that depend on them into an anonymous scope.
  • Loading branch information
Ralith committed Jun 24, 2023
1 parent 1a56ae2 commit d41ca1d
Showing 1 changed file with 59 additions and 59 deletions.
118 changes: 59 additions & 59 deletions macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn derive(input: DeriveInput) -> Result<TokenStream2> {
.iter()
.map(|ty| quote! { <#ty as ::hecs::Query>::Fetch })
.collect::<Vec<_>>();
let fetch_ident = Ident::new(&format!("__HecsInternal{}Fetch", ident), Span::call_site());
let fetch_ident = Ident::new(&format!("{}Fetch", ident), Span::call_site());
let fetch = match data.fields {
syn::Fields::Named(_) => quote! {
#vis struct #fetch_ident {
Expand All @@ -83,7 +83,7 @@ pub fn derive(input: DeriveInput) -> Result<TokenStream2> {
#vis struct #fetch_ident;
},
};
let state_ident = Ident::new(&format!("__HecsInternal{}State", ident), Span::call_site());
let state_ident = Ident::new(&format!("{}State", ident), Span::call_site());
let state = match data.fields {
syn::Fields::Named(_) => quote! {
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -114,81 +114,81 @@ pub fn derive(input: DeriveInput) -> Result<TokenStream2> {
.collect::<Vec<_>>();

Ok(quote! {
impl<'a> ::hecs::Query for #ident<'a> {
type Item<'q> = #ident<'q>;
const _: () = {
#fetch

type Fetch = #fetch_ident;
impl<'a> ::hecs::Query for #ident<'a> {
type Item<'q> = #ident<'q>;

#[allow(unused_variables)]
unsafe fn get<'q>(fetch: &Self::Fetch, n: usize) -> Self::Item<'q> {
#(
let #intermediates: <#queries as ::hecs::Query>::Item<'q> = <#queries as ::hecs::Query>::get(&fetch.#fields, n);
)*
#ident {#(#fields: #intermediates,)*}
type Fetch = #fetch_ident;

#[allow(unused_variables)]
unsafe fn get<'q>(fetch: &Self::Fetch, n: usize) -> Self::Item<'q> {
#(
let #intermediates: <#queries as ::hecs::Query>::Item<'q> = <#queries as ::hecs::Query>::get(&fetch.#fields, n);
)*
#ident {#(#fields: #intermediates,)*}
}
}
}

#[doc(hidden)]
#fetch
#state

#[doc(hidden)]
#state
unsafe impl ::hecs::Fetch for #fetch_ident {
type State = #state_ident;

unsafe impl ::hecs::Fetch for #fetch_ident {
type State = #state_ident;
fn dangling() -> Self {
Self {
#(
#fields: #fetches::dangling(),
)*
}
}

fn dangling() -> Self {
Self {
#[allow(unused_variables, unused_mut)]
fn access(archetype: &::hecs::Archetype) -> ::std::option::Option<::hecs::Access> {
let mut access = ::hecs::Access::Iterate;
#(
#fields: #fetches::dangling(),
access = ::core::cmp::max(access, #fetches::access(archetype)?);
)*
::std::option::Option::Some(access)
}
}

#[allow(unused_variables, unused_mut)]
fn access(archetype: &::hecs::Archetype) -> ::std::option::Option<::hecs::Access> {
let mut access = ::hecs::Access::Iterate;
#(
access = ::core::cmp::max(access, #fetches::access(archetype)?);
)*
::std::option::Option::Some(access)
}
#[allow(unused_variables)]
fn borrow(archetype: &::hecs::Archetype, state: Self::State) {
#(#fetches::borrow(archetype, state.#fields);)*
}

#[allow(unused_variables)]
fn borrow(archetype: &::hecs::Archetype, state: Self::State) {
#(#fetches::borrow(archetype, state.#fields);)*
}
#[allow(unused_variables)]
fn prepare(archetype: &::hecs::Archetype) -> ::std::option::Option<Self::State> {
::std::option::Option::Some(#state_ident {
#(
#fields: #fetches::prepare(archetype)?,
)*
})
}

#[allow(unused_variables)]
fn prepare(archetype: &::hecs::Archetype) -> ::std::option::Option<Self::State> {
::std::option::Option::Some(#state_ident {
#(
#fields: #fetches::prepare(archetype)?,
)*
})
}
#[allow(unused_variables)]
fn execute(archetype: &::hecs::Archetype, state: Self::State) -> Self {
Self {
#(
#fields: #fetches::execute(archetype, state.#fields),
)*
}
}

#[allow(unused_variables)]
fn release(archetype: &::hecs::Archetype, state: Self::State) {
#(#fetches::release(archetype, state.#fields);)*
}

#[allow(unused_variables)]
fn execute(archetype: &::hecs::Archetype, state: Self::State) -> Self {
Self {
#[allow(unused_variables, unused_mut)]
fn for_each_borrow(mut f: impl ::core::ops::FnMut(::core::any::TypeId, bool)) {
#(
#fields: #fetches::execute(archetype, state.#fields),
<#fetches as ::hecs::Fetch>::for_each_borrow(&mut f);
)*
}
}

#[allow(unused_variables)]
fn release(archetype: &::hecs::Archetype, state: Self::State) {
#(#fetches::release(archetype, state.#fields);)*
}

#[allow(unused_variables, unused_mut)]
fn for_each_borrow(mut f: impl ::core::ops::FnMut(::core::any::TypeId, bool)) {
#(
<#fetches as ::hecs::Fetch>::for_each_borrow(&mut f);
)*
}
}
};
})
}

Expand Down

0 comments on commit d41ca1d

Please sign in to comment.