bevy_reflect: Allow #[reflect(default)]
on enum variant fields
#8514
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
When using
FromReflect
, fields can be optionally left out if they are marked with#[reflect(default)]
. This is very handy for working with serialized data as giant structs only need to list a subset of defined fields in order to be constructed.Example
Take the following struct:
Since all the fields are default-able, we can successfully call
FromReflect
on deserialized data like:Unfortunately, this does not work with fields in enum variants. Marking a variant field as
#[reflect(default)]
does nothing when callingFromReflect
.Solution
Allow enum variant fields to define a default value using
#[reflect(default)]
.#[reflect(Default)]
One thing that structs and tuple structs can do is use their
Default
implementation when callingFromReflect
. Adding#[reflect(Default)]
to the struct or tuple struct both registersReflectDefault
and alters theFromReflect
implementation to useDefault
to generate any missing fields.This works well enough for structs and tuple structs, but for enums it's not as simple. Since the
Default
implementation for an enum only covers a single variant, it's not as intuitive as to what the behavior will be. And (imo) it feels weird that we would be able to specify default values in this way for one variant but not the others.Because of this, I chose to not implement that behavior here. However, I'm open to adding it in if anyone feels otherwise.
Changelog
#[reflect(default)]