Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bevy_reflect: Default attribute paths #9323

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use crate::REFLECT_ATTRIBUTE_NAME;
use syn::meta::ParseNestedMeta;
use syn::{Attribute, LitStr, Token};
use syn::{Attribute, Token};

pub(crate) static IGNORE_SERIALIZATION_ATTR: &str = "skip_serializing";
pub(crate) static IGNORE_ALL_ATTR: &str = "ignore";
Expand Down Expand Up @@ -98,14 +98,13 @@ fn parse_meta(args: &mut ReflectFieldAttr, meta: ParseNestedMeta) -> Result<(),
if meta.path.is_ident(DEFAULT_ATTR) {
// Allow:
// - `#[reflect(default)]`
// - `#[reflect(default = "path::to::func")]`
// - `#[reflect(default = path::to::func)]`
if !matches!(args.default, DefaultBehavior::Required) {
return Err(meta.error(format!("only one of [{:?}] is allowed", [DEFAULT_ATTR])));
}

if meta.input.peek(Token![=]) {
let lit = meta.value()?.parse::<LitStr>()?;
args.default = DefaultBehavior::Func(lit.parse()?);
args.default = DefaultBehavior::Func(meta.value()?.parse()?);
} else {
args.default = DefaultBehavior::Default;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub fn derive_reflect(input: TokenStream) -> TokenStream {
///
/// By default, this attribute denotes that the field's type implements [`Default`].
/// However, it can also take in a path string to a user-defined function that will return the default value.
/// This takes the form: `#[reflect(default = "path::to::my_function")]` where `my_function` is a parameterless
/// This takes the form: `#[reflect(default = path::to::my_function)]` where `my_function` is a parameterless
/// function that must return some default value for the type.
///
/// Specifying a custom default can be used to give different fields their own specialized defaults,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/src/from_reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub trait FromReflect: Reflect + Sized {
/// ```
/// # use bevy_reflect::{DynamicTupleStruct, FromReflect, Reflect};
/// #[derive(Reflect, PartialEq, Eq, Debug)]
/// struct Foo(#[reflect(default = "default_value")] usize);
/// struct Foo(#[reflect(default = default_value)] usize);
///
/// fn default_value() -> usize { 123 }
///
Expand All @@ -77,7 +77,7 @@ pub trait FromReflect: Reflect + Sized {
/// ```
/// # use bevy_reflect::{DynamicTupleStruct, Reflect, ReflectFromReflect, Typed, TypeRegistry};
/// # #[derive(Reflect, PartialEq, Eq, Debug)]
/// # struct Foo(#[reflect(default = "default_value")] usize);
/// # struct Foo(#[reflect(default = default_value)] usize);
/// # fn default_value() -> usize { 123 }
/// # let mut registry = TypeRegistry::new();
/// # registry.register::<Foo>();
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
//!
//! Fields can be given default values for when a field is missing in the passed value or even ignored.
//! Ignored fields must either implement [`Default`] or have a default function specified
//! using `#[reflect(default = "path::to::function")]`.
//! using `#[reflect(default = path::to::function)]`.
//!
//! See the [derive macro documentation](derive@crate::FromReflect) for details.
//!
Expand Down Expand Up @@ -774,11 +774,11 @@ mod tests {

// Use `get_bar_default()`
#[reflect(ignore)]
#[reflect(default = "get_bar_default")]
#[reflect(default = get_bar_default)]
bar: NotReflect,

// Ensure attributes can be combined
#[reflect(ignore, default = "get_bar_default")]
#[reflect(ignore, default = get_bar_default)]
baz: NotReflect,
}

Expand Down Expand Up @@ -807,7 +807,7 @@ mod tests {
enum MyEnum {
Foo(#[reflect(default)] String),
Bar {
#[reflect(default = "get_baz_default")]
#[reflect(default = get_baz_default)]
#[reflect(ignore)]
baz: usize,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/reflection/reflection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
/// reflection by using the `#[reflect(ignore)]` attribute.
/// If you choose to ignore a field, you need to let the automatically-derived `FromReflect` implementation
/// how to handle the field.
/// To do this, you can either define a `#[reflect(default = "...")]` attribute on the ignored field, or
/// To do this, you can either define a `#[reflect(default = ...)]` attribute on the ignored field, or
/// opt-out of `FromReflect`'s auto-derive using the `#[reflect(from_reflect = false)]` attribute.
#[derive(Reflect)]
#[reflect(from_reflect = false)]
Expand Down