bevy/crates/bevy_reflect/src
Jakob Hellermann 4b191d968d remove blanket Serialize + Deserialize requirement for Reflect on generic types (#5197)
# Objective

Some generic types like `Option<T>`, `Vec<T>` and `HashMap<K, V>` implement `Reflect` when where their generic types `T`/`K`/`V` implement `Serialize + for<'de> Deserialize<'de>`.
This is so that in their `GetTypeRegistration` impl they can insert the `ReflectSerialize` and `ReflectDeserialize` type data structs.

This has the annoying side effect that if your struct contains a `Option<NonSerdeStruct>` you won't be able to derive reflect (https://github.com/bevyengine/bevy/issues/4054).

## Solution

- remove the `Serialize + Deserialize` bounds on wrapper types
  - this means that `ReflectSerialize` and `ReflectDeserialize` will no longer be inserted even for `.register::<Option<DoesImplSerde>>()`
- add `register_type_data<T, D>` shorthand for `registry.get_mut(T).insert(D::from_type<T>())`
- require users to register their specific generic types **and the serde types** separately like
```rust
        .register_type::<Option<String>>()
        .register_type_data::<Option<String>, ReflectSerialize>()
        .register_type_data::<Option<String>, ReflectDeserialize>()

```
I believe this is the best we can do for extensibility and convenience without specialization.


## Changelog

- `.register_type` for generic types like `Option<T>`, `Vec<T>`, `HashMap<K, V>` will no longer insert `ReflectSerialize` and `ReflectDeserialize` type data. Instead you need to register it separately for concrete generic types like so:
```rust
        .register_type::<Option<String>>()
        .register_type_data::<Option<String>, ReflectSerialize>()
        .register_type_data::<Option<String>, ReflectDeserialize>()
```

TODO: more docs and tweaks to the scene example to demonstrate registering generic types.
2022-07-21 14:57:37 +00:00
..
impls remove blanket Serialize + Deserialize requirement for Reflect on generic types (#5197) 2022-07-21 14:57:37 +00:00
serde bevy_reflect: support map insertion (#5173) 2022-07-04 13:04:19 +00:00
array.rs Make reflect_partial_eq return more accurate results (#5210) 2022-07-05 17:41:54 +00:00
fields.rs bevy_reflect: Add statically available type info for reflected types (#4042) 2022-06-09 21:18:15 +00:00
lib.rs bevy_reflect: remove glam from a test which is active without the glam feature (#5195) 2022-07-04 14:17:46 +00:00
list.rs Make reflect_partial_eq return more accurate results (#5210) 2022-07-05 17:41:54 +00:00
map.rs Make reflect_partial_eq return more accurate results (#5210) 2022-07-05 17:41:54 +00:00
path.rs document more of bevy_reflect (#3655) 2022-01-14 19:09:44 +00:00
reflect.rs bevy_reflect: support map insertion (#5173) 2022-07-04 13:04:19 +00:00
std_traits.rs add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
struct_trait.rs Make reflect_partial_eq return more accurate results (#5210) 2022-07-05 17:41:54 +00:00
tuple.rs remove blanket Serialize + Deserialize requirement for Reflect on generic types (#5197) 2022-07-21 14:57:37 +00:00
tuple_struct.rs Make reflect_partial_eq return more accurate results (#5210) 2022-07-05 17:41:54 +00:00
type_info.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
type_registry.rs remove blanket Serialize + Deserialize requirement for Reflect on generic types (#5197) 2022-07-21 14:57:37 +00:00
type_uuid.rs re-enable #[derive(TypeUuid)] for generics (#4118) 2022-04-26 19:41:25 +00:00
utility.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00