
# Objective Currently, `FromReflect` makes a couple assumptions: * Ignored fields must implement `Default` * Active fields must implement `FromReflect` * The reflected must be fully populated for active fields (can't use an empty `DynamicStruct`) However, one or both of these requirements might be unachievable, such as for external types. In these cases, it might be nice to tell `FromReflect` to use a custom default. ## Solution Added the `#[reflect(default)]` derive helper attribute. This attribute can be applied to any field (ignored or not) and will allow a default value to be specified in place of the regular `from_reflect()` call. It takes two forms: `#[reflect(default)]` and `#[reflect(default = "some_func")]`. The former specifies that `Default::default()` should be used while the latter specifies that `some_func()` should be used. This is pretty much [how serde does it](https://serde.rs/field-attrs.html#default). ### Example ```rust #[derive(Reflect, FromReflect)] struct MyStruct { // Use `Default::default()` #[reflect(default)] foo: String, // Use `get_bar_default()` #[reflect(default = "get_bar_default")] #[reflect(ignore)] bar: usize, } fn get_bar_default() -> usize { 123 } ``` ### Active Fields As an added benefit, this also allows active fields to be completely missing from their dynamic object. This is because the attribute tells `FromReflect` how to handle missing active fields (it still tries to use `from_reflect` first so the `FromReflect` trait is still required). ```rust let dyn_struct = DynamicStruct::default(); // We can do this without actually including the active fields since they have `#[reflect(default)]` let my_struct = <MyStruct as FromReflect>::from_reflect(&dyn_struct); ``` ### Container Defaults Also, with the addition of #3733, people will likely start adding `#[reflect(Default)]` to their types now. Just like with the fields, we can use this to mark the entire container as "defaultable". This grants us the ability to completely remove the field markers altogether if our type implements `Default` (and we're okay with fields using that instead of their own `Default` impls): ```rust #[derive(Reflect, FromReflect)] #[reflect(Default)] struct MyStruct { foo: String, #[reflect(ignore)] bar: usize, } impl Default for MyStruct { fn default() -> Self { Self { foo: String::from("Hello"), bar: 123, } } } // Again, we can now construct this from nothing pretty much let dyn_struct = DynamicStruct::default(); let my_struct = <MyStruct as FromReflect>::from_reflect(&dyn_struct); ``` Now if _any_ field is missing when using `FromReflect`, we simply fallback onto the container's `Default` implementation. This behavior can be completely overridden on a per-field basis, of course, by simply defining those same field attributes like before. ### Related * #3733 * #1395 * #2377 --- ## Changelog * Added `#[reflect(default)]` field attribute for `FromReflect` * Allows missing fields to be given a default value when using `FromReflect` * `#[reflect(default)]` - Use the field's `Default` implementation * `#[reflect(default = "some_fn")]` - Use a custom function to get the default value * Allow `#[reflect(Default)]` to have a secondary usage as a container attribute * Allows missing fields to be given a default value based on the container's `Default` impl when using `FromReflect` Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
175 lines
6.3 KiB
Rust
175 lines
6.3 KiB
Rust
//! This crate contains macros used by Bevy's `Reflect` API.
|
|
//!
|
|
//! The main export of this crate is the derive macro for [`Reflect`]. This allows
|
|
//! types to easily implement `Reflect` along with other `bevy_reflect` traits,
|
|
//! such as `Struct`, `GetTypeRegistration`, and more— all with a single derive!
|
|
//!
|
|
//! Some other noteworthy exports include the derive macros for [`FromReflect`] and
|
|
//! [`TypeUuid`], as well as the [`reflect_trait`] attribute macro.
|
|
//!
|
|
//! [`Reflect`]: crate::derive_reflect
|
|
//! [`FromReflect`]: crate::derive_from_reflect
|
|
//! [`TypeUuid`]: crate::derive_type_uuid
|
|
//! [`reflect_trait`]: macro@reflect_trait
|
|
|
|
extern crate proc_macro;
|
|
|
|
mod container_attributes;
|
|
mod derive_data;
|
|
mod field_attributes;
|
|
mod from_reflect;
|
|
mod impls;
|
|
mod reflect_value;
|
|
mod registration;
|
|
mod trait_reflection;
|
|
mod type_uuid;
|
|
mod utility;
|
|
|
|
use crate::derive_data::ReflectDeriveData;
|
|
use derive_data::DeriveType;
|
|
use proc_macro::TokenStream;
|
|
use quote::quote;
|
|
use reflect_value::ReflectValueDef;
|
|
use syn::{parse_macro_input, DeriveInput};
|
|
|
|
pub(crate) static REFLECT_ATTRIBUTE_NAME: &str = "reflect";
|
|
pub(crate) static REFLECT_VALUE_ATTRIBUTE_NAME: &str = "reflect_value";
|
|
|
|
#[proc_macro_derive(Reflect, attributes(reflect, reflect_value, module))]
|
|
pub fn derive_reflect(input: TokenStream) -> TokenStream {
|
|
let ast = parse_macro_input!(input as DeriveInput);
|
|
|
|
let derive_data = match ReflectDeriveData::from_input(&ast) {
|
|
Ok(data) => data,
|
|
Err(err) => return err.into_compile_error().into(),
|
|
};
|
|
|
|
match derive_data.derive_type() {
|
|
DeriveType::Struct | DeriveType::UnitStruct => impls::impl_struct(&derive_data),
|
|
DeriveType::TupleStruct => impls::impl_tuple_struct(&derive_data),
|
|
DeriveType::Value => impls::impl_value(
|
|
derive_data.type_name(),
|
|
derive_data.generics(),
|
|
derive_data.get_type_registration(),
|
|
derive_data.bevy_reflect_path(),
|
|
derive_data.traits(),
|
|
),
|
|
}
|
|
}
|
|
|
|
/// Derives the `FromReflect` trait.
|
|
///
|
|
/// This macro supports the following field attributes:
|
|
/// * `#[reflect(ignore)]`: Ignores the field. This requires the field to implement [`Default`].
|
|
/// * `#[reflect(default)]`: If the field's value cannot be read, uses its [`Default`] implementation.
|
|
/// * `#[reflect(default = "some_func")]`: If the field's value cannot be read, uses the function with the given name.
|
|
///
|
|
#[proc_macro_derive(FromReflect, attributes(reflect))]
|
|
pub fn derive_from_reflect(input: TokenStream) -> TokenStream {
|
|
let ast = parse_macro_input!(input as DeriveInput);
|
|
|
|
let derive_data = match ReflectDeriveData::from_input(&ast) {
|
|
Ok(data) => data,
|
|
Err(err) => return err.into_compile_error().into(),
|
|
};
|
|
|
|
match derive_data.derive_type() {
|
|
DeriveType::Struct | DeriveType::UnitStruct => from_reflect::impl_struct(&derive_data),
|
|
DeriveType::TupleStruct => from_reflect::impl_tuple_struct(&derive_data),
|
|
DeriveType::Value => from_reflect::impl_value(
|
|
derive_data.type_name(),
|
|
&ast.generics,
|
|
derive_data.bevy_reflect_path(),
|
|
),
|
|
}
|
|
}
|
|
|
|
// From https://github.com/randomPoison/type-uuid
|
|
#[proc_macro_derive(TypeUuid, attributes(uuid))]
|
|
pub fn derive_type_uuid(input: TokenStream) -> TokenStream {
|
|
type_uuid::type_uuid_derive(input)
|
|
}
|
|
|
|
#[proc_macro_attribute]
|
|
pub fn reflect_trait(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
trait_reflection::reflect_trait(&args, input)
|
|
}
|
|
|
|
#[proc_macro]
|
|
pub fn impl_reflect_value(input: TokenStream) -> TokenStream {
|
|
let reflect_value_def = parse_macro_input!(input as ReflectValueDef);
|
|
|
|
let bevy_reflect_path = utility::get_bevy_reflect_path();
|
|
let ty = &reflect_value_def.type_name;
|
|
let reflect_traits = reflect_value_def.traits.unwrap_or_default();
|
|
let registration_data = &reflect_traits.idents();
|
|
let get_type_registration_impl = registration::impl_get_type_registration(
|
|
ty,
|
|
&bevy_reflect_path,
|
|
registration_data,
|
|
&reflect_value_def.generics,
|
|
);
|
|
impls::impl_value(
|
|
ty,
|
|
&reflect_value_def.generics,
|
|
get_type_registration_impl,
|
|
&bevy_reflect_path,
|
|
&reflect_traits,
|
|
)
|
|
}
|
|
|
|
/// A replacement for `#[derive(Reflect)]` to be used with foreign types which
|
|
/// the definitions of cannot be altered.
|
|
///
|
|
/// This macro is an alternative to [`impl_reflect_value!`] and [`impl_from_reflect_value!`]
|
|
/// which implement foreign types as Value types. Note that there is no `impl_from_reflect_struct`,
|
|
/// as this macro will do the job of both. This macro implements them as `Struct` types,
|
|
/// which have greater functionality. The type being reflected must be in scope, as you cannot
|
|
/// qualify it in the macro as e.g. `bevy::prelude::Vec3`.
|
|
///
|
|
/// It may be necessary to add `#[reflect(Default)]` for some types, specifically non-constructible
|
|
/// foreign types. Without `Default` reflected for such types, you will usually get an arcane
|
|
/// error message and fail to compile. If the type does not implement `Default`, it may not
|
|
/// be possible to reflect without extending the macro.
|
|
///
|
|
/// # Example
|
|
/// Implementing `Reflect` for `bevy::prelude::Vec3` as a struct type:
|
|
/// ```ignore
|
|
/// use bevy::prelude::Vec3;
|
|
///
|
|
/// impl_reflect_struct!(
|
|
/// #[reflect(PartialEq, Serialize, Deserialize, Default)]
|
|
/// struct Vec3 {
|
|
/// x: f32,
|
|
/// y: f32,
|
|
/// z: f32
|
|
/// }
|
|
/// );
|
|
/// ```
|
|
#[proc_macro]
|
|
pub fn impl_reflect_struct(input: TokenStream) -> TokenStream {
|
|
let ast = parse_macro_input!(input as DeriveInput);
|
|
let derive_data = match ReflectDeriveData::from_input(&ast) {
|
|
Ok(data) => data,
|
|
Err(err) => return err.into_compile_error().into(),
|
|
};
|
|
|
|
let impl_struct: proc_macro2::TokenStream = impls::impl_struct(&derive_data).into();
|
|
let impl_from_struct: proc_macro2::TokenStream = from_reflect::impl_struct(&derive_data).into();
|
|
|
|
TokenStream::from(quote! {
|
|
#impl_struct
|
|
|
|
#impl_from_struct
|
|
})
|
|
}
|
|
|
|
#[proc_macro]
|
|
pub fn impl_from_reflect_value(input: TokenStream) -> TokenStream {
|
|
let reflect_value_def = parse_macro_input!(input as ReflectValueDef);
|
|
|
|
let bevy_reflect_path = utility::get_bevy_reflect_path();
|
|
let ty = &reflect_value_def.type_name;
|
|
from_reflect::impl_value(ty, &reflect_value_def.generics, &bevy_reflect_path)
|
|
}
|