Skip to content

Commit 94490cb

Browse files
committed
Update documentation on FromReflect derive
1 parent 4a62390 commit 94490cb

File tree

1 file changed

+25
-4
lines changed
  • crates/bevy_reflect/bevy_reflect_derive/src

1 file changed

+25
-4
lines changed

crates/bevy_reflect/bevy_reflect_derive/src/lib.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,32 @@ pub fn derive_reflect(input: TokenStream) -> TokenStream {
140140

141141
/// Derives the `FromReflect` trait.
142142
///
143-
/// This macro supports the following field attributes:
144-
/// * `#[reflect(ignore)]`: Ignores the field. This requires the field to implement [`Default`].
145-
/// * `#[reflect(default)]`: If the field's value cannot be read, uses its [`Default`] implementation.
146-
/// * `#[reflect(default = "some_func")]`: If the field's value cannot be read, uses the function with the given name.
143+
/// # Field Attributes
144+
///
145+
/// ## `#[reflect(ignore)]`
146+
///
147+
/// The `#[reflect(ignore)]` attribute is shared with the [`Reflect` derive macro] and has much of the same
148+
/// functionality in that it marks a field to be ignored by the reflection API.
149+
///
150+
/// The only major difference is that using it with this derive requires that the field implements [`Default`].
151+
/// Without this requirement, there would be no way for `FromReflect` to automatically construct missing fields
152+
/// that have been ignored.
147153
///
154+
/// ## `#[reflect(default)]`
155+
///
156+
/// If a field cannot be read, this attribute specifies a default value to be used in its place.
157+
///
158+
/// By default, this attribute denotes that the field's type implements [`Default`].
159+
/// However, it can also take in a path string to a user-defined function that will return the default value.
160+
/// This takes the form: `#[reflect(default = "path::to::my_function)]` where `my_function` is a parameterless
161+
/// function that must return some default value for the type.
162+
///
163+
/// Specifying a custom default can be used to give different fields their own specialized defaults,
164+
/// or to remove the `Default` requirement on fields marked with `#[reflect(ignore)]`.
165+
/// Additionally, either form of this attribute can be used to fill in fields that are simply missing,
166+
/// such as when converting a partially-constructed dynamic type to a concrete one.
167+
///
168+
/// [`Reflect` derive macro]: Reflect
148169
#[proc_macro_derive(FromReflect, attributes(reflect))]
149170
pub fn derive_from_reflect(input: TokenStream) -> TokenStream {
150171
let ast = parse_macro_input!(input as DeriveInput);

0 commit comments

Comments
 (0)