From 6aedb2500aedaab449a94e7c972d800c6f5fc56b Mon Sep 17 00:00:00 2001 From: davier Date: Fri, 13 Aug 2021 22:21:34 +0000 Subject: [PATCH] Cleanup FromResources (#2601) ## Objective - Clean up remaining references to the trait `FromResources`, which was replaced in favor of `FromWorld` during the ECS rework. ## Solution - Remove the derive macro for `FromResources` - Change doc references of `FromResources` to `FromWorld` (this is the first item in #2576) --- crates/bevy_app/src/app.rs | 4 +-- crates/bevy_derive/src/lib.rs | 8 ----- crates/bevy_derive/src/modules.rs | 1 - crates/bevy_derive/src/resource.rs | 33 ------------------- .../bevy_transform/src/components/parent.rs | 2 +- examples/ecs/ecs_guide.rs | 2 +- examples/scene/scene.rs | 10 +++--- 7 files changed, 9 insertions(+), 51 deletions(-) delete mode 100644 crates/bevy_derive/src/resource.rs diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index d418220540..4b9a71812f 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -343,7 +343,7 @@ impl App { /// A resource in Bevy represents globally unique data. Resources must be added to Bevy Apps /// before using them. This happens with [`App::insert_resource`]. /// - /// See also `init_resource` for resources that implement `Default` or [`FromResources`]. + /// See also `init_resource` for resources that implement `Default` or [`FromWorld`]. /// /// ## Example /// ``` @@ -390,7 +390,7 @@ impl App { /// Initialize a resource in the current [App], if it does not exist yet /// - /// Adds a resource that implements `Default` or [`FromResources`] trait. + /// Adds a resource that implements `Default` or [`FromWorld`] trait. /// If the resource already exists, `init_resource` does nothing. /// /// ## Example diff --git a/crates/bevy_derive/src/lib.rs b/crates/bevy_derive/src/lib.rs index a73093ed2a..edbbf6f1ab 100644 --- a/crates/bevy_derive/src/lib.rs +++ b/crates/bevy_derive/src/lib.rs @@ -7,18 +7,10 @@ mod enum_variant_meta; mod modules; mod render_resource; mod render_resources; -mod resource; mod shader_defs; use proc_macro::TokenStream; -/// Derives the FromResources trait. Each field must also implement the FromResources trait or this -/// will fail. FromResources is automatically implemented for types that implement Default. -#[proc_macro_derive(FromResources)] -pub fn derive_from_resources(input: TokenStream) -> TokenStream { - resource::derive_from_resources(input) -} - /// Derives the Bytes trait. Each field must also implements Bytes or this will fail. #[proc_macro_derive(Bytes)] pub fn derive_bytes(input: TokenStream) -> TokenStream { diff --git a/crates/bevy_derive/src/modules.rs b/crates/bevy_derive/src/modules.rs index 7e7dcbc98e..0191ff907b 100644 --- a/crates/bevy_derive/src/modules.rs +++ b/crates/bevy_derive/src/modules.rs @@ -1,4 +1,3 @@ -pub const BEVY_APP: &str = "bevy_app"; pub const BEVY_ASSET: &str = "bevy_asset"; pub const BEVY_CORE: &str = "bevy_core"; pub const BEVY_RENDER: &str = "bevy_render"; diff --git a/crates/bevy_derive/src/resource.rs b/crates/bevy_derive/src/resource.rs deleted file mode 100644 index 110b5605ca..0000000000 --- a/crates/bevy_derive/src/resource.rs +++ /dev/null @@ -1,33 +0,0 @@ -use bevy_macro_utils::BevyManifest; -use proc_macro::TokenStream; -use quote::quote; -use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields}; - -pub fn derive_from_resources(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 bevy_app_path = BevyManifest::default().get_path(crate::modules::BEVY_APP); - let field_types = fields.iter().map(|field| &field.ty); - let fields = fields.iter().map(|field| field.ident.as_ref().unwrap()); - 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::FromResources for #struct_name#ty_generics { - fn from_resources(resources: &Resources) -> Self { - use #bevy_app_path::FromResources; - #struct_name { - #(#fields: <#field_types>::from_resources(resources),)* - } - } - } - }) -} diff --git a/crates/bevy_transform/src/components/parent.rs b/crates/bevy_transform/src/components/parent.rs index 8f65d3a81d..646dca02cc 100644 --- a/crates/bevy_transform/src/components/parent.rs +++ b/crates/bevy_transform/src/components/parent.rs @@ -52,7 +52,7 @@ impl MapEntities for PreviousParent { } } -// TODO: Better handle this case see `impl FromResources for Parent` +// TODO: Better handle this case see `impl FromWorld for Parent` impl FromWorld for PreviousParent { fn from_world(_world: &mut World) -> Self { PreviousParent(Entity::new(u32::MAX)) diff --git a/examples/ecs/ecs_guide.rs b/examples/ecs/ecs_guide.rs index f71fd803c7..ae51cb6b3d 100644 --- a/examples/ecs/ecs_guide.rs +++ b/examples/ecs/ecs_guide.rs @@ -272,7 +272,7 @@ fn main() { // that :) The plugin below runs our app's "system schedule" once every 5 seconds // (configured above). .add_plugin(ScheduleRunnerPlugin::default()) - // Resources that implement the Default or FromResources trait can be added like this: + // Resources that implement the Default or FromWorld trait can be added like this: .init_resource::() // Startup systems run exactly once BEFORE all other systems. These are generally used for // app initialization code (ex: adding entities and resources) diff --git a/examples/scene/scene.rs b/examples/scene/scene.rs index 000968e8b6..9cf53e5db7 100644 --- a/examples/scene/scene.rs +++ b/examples/scene/scene.rs @@ -13,12 +13,12 @@ fn main() { .run(); } -// Registered components must implement the `Reflect` and `FromResources` traits. +// Registered components must implement the `Reflect` and `FromWorld` traits. // The `Reflect` trait enables serialization, deserialization, and dynamic property access. // `Reflect` enable a bunch of cool behaviors, so its worth checking out the dedicated `reflect.rs` -// example. The `FromResources` trait determines how your component is constructed when it loads. +// example. The `FromWorld` trait determines how your component is constructed when it loads. // For simple use cases you can just implement the `Default` trait (which automatically implements -// FromResources). The simplest registered component just needs these two derives: +// FromWorld). The simplest registered component just needs these two derives: #[derive(Reflect, Default)] #[reflect(Component)] // this tells the reflect derive to also reflect component behaviors struct ComponentA { @@ -27,8 +27,8 @@ struct ComponentA { } // Some components have fields that cannot (or should not) be written to scene files. These can be -// ignored with the #[reflect(ignore)] attribute. This is also generally where the `FromResources` -// trait comes into play. `FromResources` gives you access to your App's current ECS `Resources` +// ignored with the #[reflect(ignore)] attribute. This is also generally where the `FromWorld` +// trait comes into play. `FromWorld` gives you access to your App's current ECS `Resources` // when you construct your component. #[derive(Reflect)] #[reflect(Component)]