derive: allow manually forcing modules to "crate", which prevents the need for ambiguous crates
this fixes a failing doc test
This commit is contained in:
		
							parent
							
								
									0dc810a37a
								
							
						
					
					
						commit
						50335e21e2
					
				@ -16,7 +16,7 @@ static VERTEX_ATTRIBUTE_NAME: &'static str = "vertex";
 | 
			
		||||
 | 
			
		||||
pub fn derive_as_vertex_buffer_descriptor(input: TokenStream) -> TokenStream {
 | 
			
		||||
    let ast = parse_macro_input!(input as DeriveInput);
 | 
			
		||||
    let modules = get_modules();
 | 
			
		||||
    let modules = get_modules(&ast.attrs);
 | 
			
		||||
 | 
			
		||||
    let bevy_render_path: Path = get_path(&modules.bevy_render);
 | 
			
		||||
    let fields = match &ast.data {
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@ pub fn derive_bytes(input: TokenStream) -> TokenStream {
 | 
			
		||||
        _ => panic!("expected a struct with named fields"),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let modules = get_modules();
 | 
			
		||||
    let modules = get_modules(&ast.attrs);
 | 
			
		||||
    let bevy_core_path = get_path(&modules.bevy_core);
 | 
			
		||||
 | 
			
		||||
    let fields = fields
 | 
			
		||||
 | 
			
		||||
@ -1,49 +0,0 @@
 | 
			
		||||
use crate::modules::{get_modules, get_path};
 | 
			
		||||
use proc_macro::TokenStream;
 | 
			
		||||
use quote::quote;
 | 
			
		||||
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, Ident};
 | 
			
		||||
 | 
			
		||||
pub fn derive_component_set(input: TokenStream) -> TokenStream {
 | 
			
		||||
    let ast = parse_macro_input!(input as DeriveInput);
 | 
			
		||||
    let fields = match &ast.data {
 | 
			
		||||
        Data::Struct(DataStruct {
 | 
			
		||||
            fields: Fields::Named(fields),
 | 
			
		||||
            ..
 | 
			
		||||
        }) => &fields.named,
 | 
			
		||||
        _ => panic!("expected a struct with named fields"),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let modules = get_modules();
 | 
			
		||||
    let bevy_app_path = get_path(&modules.bevy_app);
 | 
			
		||||
    let legion_path = get_path(&modules.legion);
 | 
			
		||||
 | 
			
		||||
    let component_fields = fields
 | 
			
		||||
        .iter()
 | 
			
		||||
        .map(|field| field.ident.as_ref().unwrap())
 | 
			
		||||
        .collect::<Vec<&Ident>>();
 | 
			
		||||
 | 
			
		||||
    let generics = ast.generics;
 | 
			
		||||
    let (impl_generics, ty_generics, _where_clause) = generics.split_for_impl();
 | 
			
		||||
 | 
			
		||||
    let struct_name = &ast.ident;
 | 
			
		||||
 | 
			
		||||
    TokenStream::from(quote! {
 | 
			
		||||
        impl #impl_generics #bevy_app_path::ComponentSet for #struct_name#ty_generics {
 | 
			
		||||
            fn insert(self, world: &mut #legion_path::prelude::World) -> #legion_path::prelude::Entity {
 | 
			
		||||
                *world.insert((),
 | 
			
		||||
                    vec![(
 | 
			
		||||
                        #(self.#component_fields,)*
 | 
			
		||||
                    )
 | 
			
		||||
                ]).first().unwrap()
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            fn insert_command_buffer(self, command_buffer: &mut #legion_path::prelude::CommandBuffer) -> #legion_path::prelude::Entity {
 | 
			
		||||
                *command_buffer.insert((),
 | 
			
		||||
                    vec![(
 | 
			
		||||
                        #(self.#component_fields,)*
 | 
			
		||||
                    )
 | 
			
		||||
                ]).first().unwrap()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
@ -3,7 +3,6 @@ extern crate proc_macro;
 | 
			
		||||
mod app_plugin;
 | 
			
		||||
mod as_vertex_buffer_descriptor;
 | 
			
		||||
mod bytes;
 | 
			
		||||
mod component_set;
 | 
			
		||||
mod modules;
 | 
			
		||||
mod render_resource;
 | 
			
		||||
mod render_resources;
 | 
			
		||||
@ -12,41 +11,36 @@ mod shader_defs;
 | 
			
		||||
 | 
			
		||||
use proc_macro::TokenStream;
 | 
			
		||||
 | 
			
		||||
#[proc_macro_derive(FromResources, attributes(module))]
 | 
			
		||||
#[proc_macro_derive(FromResources, attributes(as_crate))]
 | 
			
		||||
pub fn derive_from_resources(input: TokenStream) -> TokenStream {
 | 
			
		||||
    resource::derive_from_resources(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[proc_macro_derive(Bytes, attributes(module))]
 | 
			
		||||
#[proc_macro_derive(Bytes, attributes(as_crate))]
 | 
			
		||||
pub fn derive_bytes(input: TokenStream) -> TokenStream {
 | 
			
		||||
    bytes::derive_bytes(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[proc_macro_derive(RenderResources, attributes(render_resources, module))]
 | 
			
		||||
#[proc_macro_derive(RenderResources, attributes(render_resources, as_crate))]
 | 
			
		||||
pub fn derive_render_resources(input: TokenStream) -> TokenStream {
 | 
			
		||||
    render_resources::derive_render_resources(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[proc_macro_derive(RenderResource, attributes(module))]
 | 
			
		||||
#[proc_macro_derive(RenderResource, attributes(as_crate))]
 | 
			
		||||
pub fn derive_render_resource(input: TokenStream) -> TokenStream {
 | 
			
		||||
    render_resource::derive_render_resource(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[proc_macro_derive(ShaderDefs, attributes(shader_def, module))]
 | 
			
		||||
#[proc_macro_derive(ShaderDefs, attributes(shader_def, as_crate))]
 | 
			
		||||
pub fn derive_shader_defs(input: TokenStream) -> TokenStream {
 | 
			
		||||
    shader_defs::derive_shader_defs(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[proc_macro_derive(AsVertexBufferDescriptor, attributes(vertex, module))]
 | 
			
		||||
#[proc_macro_derive(AsVertexBufferDescriptor, attributes(vertex, as_crate))]
 | 
			
		||||
pub fn derive_as_vertex_buffer_descriptor(input: TokenStream) -> TokenStream {
 | 
			
		||||
    as_vertex_buffer_descriptor::derive_as_vertex_buffer_descriptor(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[proc_macro_derive(ComponentSet, attributes(tag, module))]
 | 
			
		||||
pub fn derive_component_set(input: TokenStream) -> TokenStream {
 | 
			
		||||
    component_set::derive_component_set(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[proc_macro_derive(DynamicAppPlugin)]
 | 
			
		||||
pub fn derive_app_plugin(input: TokenStream) -> TokenStream {
 | 
			
		||||
    app_plugin::derive_dynamic_app_plugin(input)
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
use proc_macro::TokenStream;
 | 
			
		||||
use proc_macro_crate::crate_name;
 | 
			
		||||
use syn::Path;
 | 
			
		||||
use syn::{Attribute, Path};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct Modules {
 | 
			
		||||
@ -8,7 +8,6 @@ pub struct Modules {
 | 
			
		||||
    pub bevy_asset: String,
 | 
			
		||||
    pub bevy_core: String,
 | 
			
		||||
    pub bevy_app: String,
 | 
			
		||||
    pub legion: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Modules {
 | 
			
		||||
@ -18,7 +17,6 @@ impl Modules {
 | 
			
		||||
            bevy_render: "bevy::render".to_string(),
 | 
			
		||||
            bevy_core: "bevy::core".to_string(),
 | 
			
		||||
            bevy_app: "bevy::app".to_string(),
 | 
			
		||||
            legion: "bevy".to_string(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -28,7 +26,6 @@ impl Modules {
 | 
			
		||||
            bevy_render: "bevy_render".to_string(),
 | 
			
		||||
            bevy_core: "bevy_core".to_string(),
 | 
			
		||||
            bevy_app: "bevy_app".to_string(),
 | 
			
		||||
            legion: "legion".to_string(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -37,12 +34,25 @@ fn use_meta() -> bool {
 | 
			
		||||
    crate_name("bevy").is_ok()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn get_modules() -> Modules {
 | 
			
		||||
    if use_meta() {
 | 
			
		||||
const AS_CRATE_ATTRIBUTE_NAME: &str = "as_crate";
 | 
			
		||||
 | 
			
		||||
pub fn get_modules(attributes: &[Attribute]) -> Modules {
 | 
			
		||||
    let mut modules = if use_meta() {
 | 
			
		||||
        Modules::meta()
 | 
			
		||||
    } else {
 | 
			
		||||
        Modules::external()
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    for attribute in attributes.iter() {
 | 
			
		||||
        if attribute.path.get_ident().as_ref().unwrap().to_string() == AS_CRATE_ATTRIBUTE_NAME {
 | 
			
		||||
            let value = attribute.tokens.to_string();
 | 
			
		||||
            if &value[1..value.len() - 1] == modules.bevy_render {
 | 
			
		||||
              modules.bevy_render = "crate".to_string();  
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    modules
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn get_path(path_str: &str) -> Path {
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@ use syn::{parse_macro_input, DeriveInput, Path};
 | 
			
		||||
 | 
			
		||||
pub fn derive_render_resource(input: TokenStream) -> TokenStream {
 | 
			
		||||
    let ast = parse_macro_input!(input as DeriveInput);
 | 
			
		||||
    let modules = get_modules();
 | 
			
		||||
    let modules = get_modules(&ast.attrs);
 | 
			
		||||
 | 
			
		||||
    let bevy_render_path: Path = get_path(&modules.bevy_render);
 | 
			
		||||
    let bevy_asset_path: Path = get_path(&modules.bevy_asset);
 | 
			
		||||
 | 
			
		||||
@ -20,7 +20,7 @@ static RENDER_RESOURCE_ATTRIBUTE_NAME: &'static str = "render_resources";
 | 
			
		||||
 | 
			
		||||
pub fn derive_render_resources(input: TokenStream) -> TokenStream {
 | 
			
		||||
    let ast = parse_macro_input!(input as DeriveInput);
 | 
			
		||||
    let modules = get_modules();
 | 
			
		||||
    let modules = get_modules(&ast.attrs);
 | 
			
		||||
 | 
			
		||||
    let bevy_render_path: Path = get_path(&modules.bevy_render);
 | 
			
		||||
    let attributes = ast
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@ pub fn derive_from_resources(input: TokenStream) -> TokenStream {
 | 
			
		||||
        _ => panic!("expected a struct with named fields"),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let modules = get_modules();
 | 
			
		||||
    let modules = get_modules(&ast.attrs);
 | 
			
		||||
    let bevy_app_path = get_path(&modules.bevy_app);
 | 
			
		||||
 | 
			
		||||
    let field_types = fields.iter().map(|field| &field.ty);
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ static SHADER_DEF_ATTRIBUTE_NAME: &'static str = "shader_def";
 | 
			
		||||
 | 
			
		||||
pub fn derive_shader_defs(input: TokenStream) -> TokenStream {
 | 
			
		||||
    let ast = parse_macro_input!(input as DeriveInput);
 | 
			
		||||
    let modules = get_modules();
 | 
			
		||||
    let modules = get_modules(&ast.attrs);
 | 
			
		||||
    let bevy_render_path: Path = get_path(&modules.bevy_render);
 | 
			
		||||
 | 
			
		||||
    let fields = match &ast.data {
 | 
			
		||||
 | 
			
		||||
@ -1,10 +1,9 @@
 | 
			
		||||
use crate::pipeline::AsVertexBufferDescriptor;
 | 
			
		||||
use bevy_core::bytes::Byteable;
 | 
			
		||||
 | 
			
		||||
use crate as bevy_render;
 | 
			
		||||
 | 
			
		||||
#[repr(C)]
 | 
			
		||||
#[derive(Clone, Copy, AsVertexBufferDescriptor)]
 | 
			
		||||
#[as_crate(bevy_render)]
 | 
			
		||||
pub struct Vertex {
 | 
			
		||||
    pub position: [f32; 3],
 | 
			
		||||
    pub normal: [f32; 3],
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user