mirror of
https://github.com/bevyengine/bevy
synced 2024-11-21 20:23:28 +00:00
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)
This commit is contained in:
parent
5eeba1556d
commit
6aedb2500a
7 changed files with 9 additions and 51 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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),)*
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
|
@ -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))
|
||||
|
|
|
@ -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::<GameState>()
|
||||
// Startup systems run exactly once BEFORE all other systems. These are generally used for
|
||||
// app initialization code (ex: adding entities and resources)
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Reference in a new issue