Skip to content

Commit 6dd8fa0

Browse files
committed
Update documentation on FromReflect derive
1 parent f5a5ba1 commit 6dd8fa0

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

+25-4
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,32 @@ pub fn derive_reflect(input: TokenStream) -> TokenStream {
142142

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

0 commit comments

Comments
 (0)