Allow `DynamicEnum` to not have a set variant
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 161
Description
## What problem does this solve or what need does it fill?
When using dynamic reflect types like `DynamicStruct`, `DynamicEnum` etc. for patching, it can be useful to have "no-op" patches. This is possible for most of the dynamic types, for example a `DynamicStruct::default()` will result in a no-op when applied (patched) onto a `Reflect` instance.
For `DynamicEnum` there is no such "empty state". `DynamicEnum::default()` results in an enum with its variant set to an empty string and index 0. This leads to an `UnknownVariant` error when applying it to a `Reflect` enum.
I ran into this while experimenting with a [BSN prototype](https://github.com/bevyengine/bevy_editor_prototypes/blob/358ab7713d49785b8b67d734d7f9227af6a4b64f/crates/bevy_proto_bsn/src/bsn_reflect.rs#L530) in `bevy_editor_prototypes`. Currently, its not possible to say that an entity should have a `Visibility` component without also specifying a variant.
## What solution would you like?
- `DynamicEnum` should be able to represent a non-populated variant (presumably using `Option` internally).
- When a `DynamicEnum` without a variant is patched onto a `Reflect`/`Enum` instance, it should be a "no-op" and not result in any error.
## What alternative(s) have you considered?
I've considered wrapping each value in an `Option`, but that would mean a bunch of custom code to traverse and conditionally apply each value, as opposed to just passing a `dyn PartialReflect` to `apply`/`try_apply`.
Contributor guide
Assessment
This issue has not been assessed yet.