bevy/crates
Gino Valente fb813a3a64 bevy_reflect: Add #[reflect(default)] attribute for FromReflect (#4140)
# 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>
2022-05-30 19:06:25 +00:00
..
bevy_animation Split time functionality into bevy_time (#4187) 2022-05-26 00:27:18 +00:00
bevy_app Copy TaskPool resoures to subapps (#4792) 2022-05-30 16:59:43 +00:00
bevy_asset Enforce type safe usage of Handle::get (#4794) 2022-05-30 16:59:44 +00:00
bevy_audio Add missing audio/ogg file extensions: .oga, .spx (#4703) 2022-05-09 13:37:40 +00:00
bevy_core Split time functionality into bevy_time (#4187) 2022-05-26 00:27:18 +00:00
bevy_core_pipeline ExtractResourcePlugin (#3745) 2022-05-30 18:36:03 +00:00
bevy_derive Decouple some dependencies (#3886) 2022-04-27 19:08:11 +00:00
bevy_diagnostic Split time functionality into bevy_time (#4187) 2022-05-26 00:27:18 +00:00
bevy_dylib Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_dynamic_plugin Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_ecs Allow higher order systems (#4833) 2022-05-30 17:59:20 +00:00
bevy_ecs_compile_fail_tests Make derived SystemParam readonly if possible (#4650) 2022-05-09 16:09:33 +00:00
bevy_encase_derive Migrate to encase from crevice (#4339) 2022-05-18 21:09:21 +00:00
bevy_gilrs Update gilrs to v0.9 (#4848) 2022-05-30 17:26:23 +00:00
bevy_gltf Fixing confusing near and far fields in Camera (#4457) 2022-05-16 16:37:33 +00:00
bevy_hierarchy Support returning data out of with_children (#4708) 2022-05-17 22:37:51 +00:00
bevy_input Update keyboard.rs docs in bevy_input (#4517) 2022-05-17 04:16:54 +00:00
bevy_internal Split time functionality into bevy_time (#4187) 2022-05-26 00:27:18 +00:00
bevy_log Update tracing-tracy requirement from 0.8.0 to 0.9.0 (#4786) 2022-05-27 11:54:57 +00:00
bevy_macro_utils bevy_reflect_derive: Tidying up the code (#4712) 2022-05-12 19:43:23 +00:00
bevy_math Document bevy_math (#4591) 2022-04-26 18:23:29 +00:00
bevy_pbr ExtractResourcePlugin (#3745) 2022-05-30 18:36:03 +00:00
bevy_ptr bevy_ptr works in no_std environments (#4760) 2022-05-16 17:45:10 +00:00
bevy_reflect bevy_reflect: Add #[reflect(default)] attribute for FromReflect (#4140) 2022-05-30 19:06:25 +00:00
bevy_render ExtractResourcePlugin (#3745) 2022-05-30 18:36:03 +00:00
bevy_scene Update gilrs to v0.9 (#4848) 2022-05-30 17:26:23 +00:00
bevy_sprite ExtractResourcePlugin (#3745) 2022-05-30 18:36:03 +00:00
bevy_tasks Remove unused CountdownEvent (#4290) 2022-04-26 21:20:12 +00:00
bevy_text Enforce type safe usage of Handle::get (#4794) 2022-05-30 16:59:44 +00:00
bevy_time Split time functionality into bevy_time (#4187) 2022-05-26 00:27:18 +00:00
bevy_transform Nightly clippy fixes (#3491) 2022-05-17 04:38:03 +00:00
bevy_ui Enforce type safe usage of Handle::get (#4794) 2022-05-30 16:59:44 +00:00
bevy_utils Update gilrs to v0.9 (#4848) 2022-05-30 17:26:23 +00:00
bevy_window Update gilrs to v0.9 (#4848) 2022-05-30 17:26:23 +00:00
bevy_winit Optionally resize Window canvas element to fit parent element (#4726) 2022-05-20 23:13:48 +00:00