Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Feb 12, 2021
1 parent 9d7ad63 commit 4cc8092
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ fn derive_bundle_(input: DeriveInput) -> Result<TokenStream2> {
};
let (tys, field_members) = struct_fields(&data.fields);

let crate_path = bevy_ecs_path();
let ecs_path = bevy_ecs_path();
let field_idents = member_as_idents(&field_members);
let generics = add_additional_bounds_to_generic_params(&crate_path, input.generics);
let generics = add_additional_bounds_to_generic_params(&ecs_path, input.generics);

let dyn_bundle_code =
gen_dynamic_bundle_impl(&crate_path, &ident, &generics, &field_members, &tys);
gen_dynamic_bundle_impl(&ecs_path, &ident, &generics, &field_members, &tys);
let bundle_code = if tys.is_empty() {
gen_unit_struct_bundle_impl(&crate_path, ident, &generics)
gen_unit_struct_bundle_impl(&ecs_path, ident, &generics)
} else {
gen_bundle_impl(
&crate_path,
&ecs_path,
&ident,
&generics,
&field_members,
Expand All @@ -79,21 +79,21 @@ fn derive_bundle_(input: DeriveInput) -> Result<TokenStream2> {
}

fn gen_dynamic_bundle_impl(
crate_path: &syn::Path,
ecs_path: &syn::Path,
ident: &syn::Ident,
generics: &syn::Generics,
field_members: &[syn::Member],
tys: &[&syn::Type],
) -> TokenStream2 {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
impl #impl_generics ::#crate_path::DynamicBundle for #ident #ty_generics #where_clause {
impl #impl_generics ::#ecs_path::DynamicBundle for #ident #ty_generics #where_clause {
fn with_ids<__hecs__T>(&self, f: impl ::std::ops::FnOnce(&[::std::any::TypeId]) -> __hecs__T) -> __hecs__T {
<Self as ::#crate_path::Bundle>::with_static_ids(f)
<Self as ::#ecs_path::Bundle>::with_static_ids(f)
}

fn type_info(&self) -> ::std::vec::Vec<::#crate_path::TypeInfo> {
<Self as ::#crate_path::Bundle>::static_type_info()
fn type_info(&self) -> ::std::vec::Vec<::#ecs_path::TypeInfo> {
<Self as ::#ecs_path::Bundle>::static_type_info()
}

#[allow(clippy::forget_copy)]
Expand All @@ -110,7 +110,7 @@ fn gen_dynamic_bundle_impl(
}

fn gen_bundle_impl(
crate_path: &syn::Path,
ecs_path: &syn::Path,
ident: &syn::Ident,
generics: &syn::Generics,
field_members: &[syn::Member],
Expand All @@ -136,7 +136,7 @@ fn gen_bundle_impl(
};
let with_static_ids_body = if generics.params.is_empty() {
quote! {
::#crate_path::lazy_static::lazy_static! {
::#ecs_path::lazy_static::lazy_static! {
static ref ELEMENTS: [::std::any::TypeId; #num_tys] = {
#with_static_ids_inner
};
Expand All @@ -149,24 +149,24 @@ fn gen_bundle_impl(
}
};
quote! {
impl #impl_generics ::#crate_path::Bundle for #ident #ty_generics #where_clause {
impl #impl_generics ::#ecs_path::Bundle for #ident #ty_generics #where_clause {
#[allow(non_camel_case_types)]
fn with_static_ids<__hecs__T>(f: impl ::std::ops::FnOnce(&[::std::any::TypeId]) -> __hecs__T) -> __hecs__T {
#with_static_ids_body
}

fn static_type_info() -> ::std::vec::Vec<::#crate_path::TypeInfo> {
let mut info = ::std::vec![#(::#crate_path::TypeInfo::of::<#tys>()),*];
fn static_type_info() -> ::std::vec::Vec<::#ecs_path::TypeInfo> {
let mut info = ::std::vec![#(::#ecs_path::TypeInfo::of::<#tys>()),*];
info.sort_unstable();
info
}

unsafe fn get(
mut f: impl ::std::ops::FnMut(::std::any::TypeId, usize) -> ::std::option::Option<::std::ptr::NonNull<u8>>,
) -> ::std::result::Result<Self, ::#crate_path::MissingComponent> {
) -> ::std::result::Result<Self, ::#ecs_path::MissingComponent> {
#(
let #field_idents = f(::std::any::TypeId::of::<#tys>(), ::std::mem::size_of::<#tys>())
.ok_or_else(::#crate_path::MissingComponent::new::<#tys>)?
.ok_or_else(::#ecs_path::MissingComponent::new::<#tys>)?
.cast::<#tys>()
.as_ptr();
)*
Expand All @@ -178,44 +178,42 @@ fn gen_bundle_impl(

// no reason to generate a static for unit structs
fn gen_unit_struct_bundle_impl(
crate_path: &syn::Path,
ecs_path: &syn::Path,
ident: syn::Ident,
generics: &syn::Generics,
) -> TokenStream2 {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
impl #impl_generics ::#crate_path::Bundle for #ident #ty_generics #where_clause {
impl #impl_generics ::#ecs_path::Bundle for #ident #ty_generics #where_clause {
#[allow(non_camel_case_types)]
fn with_static_ids<__hecs__T>(f: impl ::std::ops::FnOnce(&[::std::any::TypeId]) -> __hecs__T) -> __hecs__T { f(&[]) }
fn static_type_info() -> ::std::vec::Vec<::#crate_path::TypeInfo> { ::std::vec::Vec::new() }
fn static_type_info() -> ::std::vec::Vec<::#ecs_path::TypeInfo> { ::std::vec::Vec::new() }

unsafe fn get(
f: impl ::std::ops::FnMut(::std::any::TypeId, usize) -> ::std::option::Option<::std::ptr::NonNull<u8>>,
) -> Result<Self, ::#crate_path::MissingComponent> {
) -> Result<Self, ::#ecs_path::MissingComponent> {
Ok(Self {/* for some reason this works for all unit struct variations */})
}
}
}
}

fn make_component_trait_bound(crate_path: &syn::Path) -> syn::TraitBound {
fn make_component_trait_bound(ecs_path: &syn::Path) -> syn::TraitBound {
syn::TraitBound {
paren_token: None,
modifier: syn::TraitBoundModifier::None,
lifetimes: None,
path: syn::parse_quote!(::#crate_path::Component),
path: syn::parse_quote!(::#ecs_path::Component),
}
}

fn add_additional_bounds_to_generic_params(
crate_path: &syn::Path,
ecs: &syn::Path,
mut generics: syn::Generics,
) -> syn::Generics {
generics.type_params_mut().for_each(|tp| {
tp.bounds
.push(syn::TypeParamBound::Trait(make_component_trait_bound(
crate_path,
)))
.push(syn::TypeParamBound::Trait(make_component_trait_bound(ecs)))
});
generics
}
Expand Down Expand Up @@ -351,7 +349,7 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
_ => panic!("Expected a struct with named fields."),
};

let crate_path = bevy_ecs_path();
let ecs_path = bevy_ecs_path();

let field_attributes = fields
.iter()
Expand Down Expand Up @@ -428,31 +426,31 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {

TokenStream::from(quote! {
pub struct #fetch_struct_name<#punctuated_generics>(#phantoms);
impl #impl_generics #crate_path::SystemParam for #struct_name#ty_generics #where_clause {
impl #impl_generics #ecs_path::SystemParam for #struct_name#ty_generics #where_clause {
type Fetch = #fetch_struct_name <#punctuated_generic_idents>;
}

impl #impl_generics #crate_path::FetchSystemParam<'a> for #fetch_struct_name<#punctuated_generic_idents> {
impl #impl_generics #ecs_path::FetchSystemParam<'a> for #fetch_struct_name<#punctuated_generic_idents> {
type Item = #struct_name#ty_generics;
fn init(system_state: &mut #crate_path::SystemState, world: &#crate_path::World, resources: &mut #crate_path::Resources) {
#(<<#field_types as SystemParam>::Fetch as #crate_path::FetchSystemParam>::init(system_state, world, resources);)*
fn init(system_state: &mut #ecs_path::SystemState, world: &#ecs_path::World, resources: &mut #ecs_path::Resources) {
#(<<#field_types as SystemParam>::Fetch as #ecs_path::FetchSystemParam>::init(system_state, world, resources);)*
}

unsafe fn get_param(
system_state: &'a #crate_path::SystemState,
world: &'a #crate_path::World,
resources: &'a #crate_path::Resources,
system_state: &'a #ecs_path::SystemState,
world: &'a #ecs_path::World,
resources: &'a #ecs_path::Resources,
) -> Option<Self::Item> {
Some(#struct_name {
#(#fields: <<#field_types as SystemParam>::Fetch as #crate_path::FetchSystemParam>::get_param(system_state, world, resources)?,)*
#(#fields: <<#field_types as SystemParam>::Fetch as #ecs_path::FetchSystemParam>::get_param(system_state, world, resources)?,)*
#(#ignored_fields: <#ignored_field_types>::default(),)*
})
}
}
})
}

fn bevy_ecs_path() -> Path {
fn bevy_ecs_path() -> syn::Path {
fn find_in_manifest(manifest: &mut Manifest, dependencies: Dependencies) -> Option<String> {
manifest.dependencies = dependencies;
if let Some(package) = manifest.find(|name| name == "bevy") {
Expand Down

0 comments on commit 4cc8092

Please sign in to comment.