bevy/crates
aecsocket 57931ce42f
bevy_reflect: Add ReflectDeserializerProcessor (#15482)
**NOTE: Also see https://github.com/bevyengine/bevy/pull/15548 for the
serializer equivalent**

# Objective

The current `ReflectDeserializer` and `TypedReflectDeserializer` use the
`TypeRegistration` and/or `ReflectDeserialize` of a given type in order
to determine how to deserialize a value of that type. However, there is
currently no way to statefully override deserialization of a given type
when using these two deserializers - that is, to have some local data in
the same scope as the `ReflectDeserializer`, and make use of that data
when deserializing.

The motivating use case for this came up when working on
[`bevy_animation_graph`](https://github.com/aecsocket/bevy_animation_graph/tree/feat/dynamic-nodes),
when loading an animation graph asset. The `AnimationGraph` stores
`Vec<Box<dyn NodeLike>>`s which we have to load in. Those `Box<dyn
NodeLike>`s may store `Handle`s to e.g. `Handle<AnimationClip>`. I want
to trigger a `load_context.load()` for that handle when it's loaded.
```rs
#[derive(Reflect)]
struct Animation {
    clips: Vec<Handle<AnimationClip>>,
}
```
```rs
(
    clips: [
        "animation_clips/walk.animclip.ron",
        "animation_clips/run.animclip.ron",
        "animation_clips/jump.animclip.ron",
    ],
)
````
Currently, if this were deserialized from an asset loader, this would be
deserialized as a vec of `Handle::default()`s, which isn't useful since
we also need to `load_context.load()` those handles for them to be used.
With this processor field, a processor can detect when `Handle<T>`s are
being loaded, then actually load them in.

## Solution

```rs
trait ReflectDeserializerProcessor {
    fn try_deserialize<'de, D>(
        &mut self,
        registration: &TypeRegistration,
        deserializer: D,
    ) -> Result<Result<Box<dyn PartialReflect>, D>, D::Error>
    where
        D: serde::Deserializer<'de>;
}
```
```diff
- pub struct ReflectDeserializer<'a> {
+ pub struct ReflectDeserializer<'a, P = ()> { // also for ReflectTypedDeserializer
      registry: &'a TypeRegistry,
+     processor: Option<&'a mut P>,
  }
```
```rs
impl<'a, P: ReflectDeserializerProcessor> ReflectDeserializer<'a, P> { // also for ReflectTypedDeserializer
    pub fn with_processor(registry: &'a TypeRegistry, processor: &'a mut P) -> Self {
        Self {
            registry,
            processor: Some(processor),
        }
    }
}
```
This does not touch the existing `fn new`s.
This `processor` field is also added to all internal visitor structs.

When `TypedReflectDeserializer` runs, it will first try to deserialize a
value of this type by passing the `TypeRegistration` and deserializer to
the processor, and fallback to the default logic. This processor runs
the earliest, and takes priority over all other deserialization logic.

## Testing

Added unit tests to `bevy_reflect::serde::de`. Also using almost exactly
the same implementation in [my fork of
`bevy_animation_graph`](https://github.com/aecsocket/bevy_animation_graph/tree/feat/dynamic-nodes).

## Migration Guide

(Since I added `P = ()`, I don't think this is actually a breaking
change anymore, but I'll leave this in)

`bevy_reflect`'s `ReflectDeserializer` and `TypedReflectDeserializer`
now take a `ReflectDeserializerProcessor` as the type parameter `P`,
which allows you to customize deserialization for specific types when
they are found. However, the rest of the API surface (`new`) remains the
same.

<details>
<summary>Original implementation</summary>

Add `ReflectDeserializerProcessor`:
```rs
struct ReflectDeserializerProcessor {
    pub can_deserialize: Box<dyn FnMut(&TypeRegistration) -> bool + 'p>,
    pub deserialize: Box<
        dyn FnMut(
                &TypeRegistration,
                &mut dyn erased_serde::Deserializer,
            ) -> Result<Box<dyn PartialReflect>, erased_serde::Error>
            + 'p,
}
``` 

Along with `ReflectDeserializer::new_with_processor` and
`TypedReflectDeserializer::new_with_processor`. This does not touch the
public API of the existing `new` fns.

This is stored as an `Option<&mut ReflectDeserializerProcessor>` on the
deserializer and any of the private `-Visitor` structs, and when we
attempt to deserialize a value, we first pass it through this processor.

Also added a very comprehensive doc test to
`ReflectDeserializerProcessor`, which is actually a scaled down version
of the code for the `bevy_animation_graph` loader. This should give
users a good motivating example for when and why to use this feature.

### Why `Box<dyn ..>`?

When I originally implemented this, I added a type parameter to
`ReflectDeserializer` to determine the processor used, with `()` being
"no processor". However when using this, I kept running into rustc
errors where it failed to validate certain type bounds and led to
overflows. I then switched to a dynamic dispatch approach.

The dynamic dispatch should not be that expensive, nor should it be a
performance regression, since it's only used if there is `Some`
processor. (Note: I have not benchmarked this, I am just speculating.)
Also, it means that we don't infect the rest of the code with an extra
type parameter, which is nicer to maintain.

### Why the `'p` on `ReflectDeserializerProcessor<'p>`?

Without a lifetime here, the `Box`es would automatically become `Box<dyn
FnMut(..) + 'static>`. This makes them practically useless, since any
local data you would want to pass in must then be `'static`. In the
motivating example, you couldn't pass in that `&mut LoadContext` to the
function.

This means that the `'p` infects the rest of the Visitor types, but this
is acceptable IMO. This PR also elides the lifetimes in the `impl<'de>
Visitor<'de> for -Visitor` blocks where possible.

### Future possibilities

I think it's technically possible to turn the processor into a trait,
and make the deserializers generic over that trait. This would also open
the door to an API like:
```rs
type Seed;

fn seed_deserialize(&mut self, r: &TypeRegistration) -> Option<Self::Seed>;

fn deserialize(&mut self, r: &TypeRegistration, d: &mut dyn erased_serde::Deserializer, s: Self::Seed) -> ...;
```

A similar processor system should also be added to the serialization
side, but that's for another PR. Ideally, both PRs will be in the same
release, since one isn't very useful without the other.

## Testing

Added unit tests to `bevy_reflect::serde::de`. Also using almost exactly
the same implementation in [my fork of
`bevy_animation_graph`](https://github.com/aecsocket/bevy_animation_graph/tree/feat/dynamic-nodes).

## Migration Guide

`bevy_reflect`'s `ReflectDeserializer` and `TypedReflectDeserializer`
now take a second lifetime parameter `'p` for storing the
`ReflectDeserializerProcessor` field lifetimes. However, the rest of the
API surface (`new`) remains the same, so if you are not storing these
deserializers or referring to them with lifetimes, you should not have
to make any changes.

</details>
2024-11-11 18:46:23 +00:00
..
bevy_a11y Remove accesskit re-export from bevy_a11y (#16257) 2024-11-08 21:01:16 +00:00
bevy_animation Fix permissions of rust source files (#16310) 2024-11-09 16:34:38 +00:00
bevy_app Improve SubApp documentation example (#16160) 2024-10-30 22:12:25 +00:00
bevy_asset Support creating asset directories (#16220) 2024-11-04 22:06:00 +00:00
bevy_audio AudioPlayer::new() (#16287) 2024-11-08 01:51:50 +00:00
bevy_color Upgrade to wgpu 23 (#15988) 2024-11-05 21:18:48 +00:00
bevy_core chore(deps): remove unused uuid dependency from bevy_core (#16253) 2024-11-05 23:31:58 +00:00
bevy_core_pipeline Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_derive move ANDROID_APP to bevy_window (#15585) 2024-10-02 03:01:06 +00:00
bevy_dev_tools fix bevy_dev_tools build (#16099) 2024-10-25 20:14:39 +00:00
bevy_diagnostic Use en-us locale for typos (#16037) 2024-10-20 18:55:17 +00:00
bevy_dylib Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_ecs Fixed issue with derive_more Display Implementations (#16298) 2024-11-08 20:29:52 +00:00
bevy_encase_derive Update `glam to 0.29, encase` to 0.10. (#15249) 2024-09-23 19:44:02 +00:00
bevy_gilrs Use Name component for gamepad (#16233) 2024-11-05 00:30:48 +00:00
bevy_gizmos Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_gltf Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_hierarchy Fix permissions of rust source files (#16310) 2024-11-09 16:34:38 +00:00
bevy_image Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_input Use Name component for gamepad (#16233) 2024-11-05 00:30:48 +00:00
bevy_internal Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_log Use en-us locale for typos (#16037) 2024-10-20 18:55:17 +00:00
bevy_macro_utils Modify derive_label to support no_std environments (#15465) 2024-09-27 20:23:26 +00:00
bevy_math Rename Rot2::angle_between to Rot2::angle_to (#16327) 2024-11-10 14:36:48 +00:00
bevy_mesh Upgrade to wgpu 23 (#15988) 2024-11-05 21:18:48 +00:00
bevy_mikktspace Use en-us locale for typos (#16037) 2024-10-20 18:55:17 +00:00
bevy_pbr Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_picking Fix typos in bevy_picking module docs (#16265) 2024-11-08 01:15:44 +00:00
bevy_ptr Reduce compile time of bevy_ptr::OwnedPtr::make function (#15644) 2024-10-28 21:15:00 +00:00
bevy_reflect bevy_reflect: Add ReflectDeserializerProcessor (#15482) 2024-11-11 18:46:23 +00:00
bevy_remote BRP System Ordering (#16198) 2024-11-05 21:05:11 +00:00
bevy_render Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_scene fix: add reflect to SceneInstanceReady and other observers/events (#16018) 2024-10-20 13:51:41 +00:00
bevy_sprite Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_state Fix typo in bevy_ecs (#16195) 2024-10-31 19:20:01 +00:00
bevy_tasks Resolve unused_qualifications warnings (#16001) 2024-10-19 16:59:58 +00:00
bevy_text Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_time Use en-us locale for typos (#16037) 2024-10-20 18:55:17 +00:00
bevy_transform Improved the global transform api to access rotation and scale (#16211) 2024-11-04 15:35:16 +00:00
bevy_ui Don't reëxport bevy_image from bevy_render (#16163) 2024-11-10 06:54:38 +00:00
bevy_utils More #[doc(fake_variadic)] goodness (#16108) 2024-10-27 19:01:50 +00:00
bevy_window Support prefers_home_indicator_hidden (#16005) 2024-10-31 16:09:30 +00:00
bevy_winit Handle failed cursor grab mode changes so that the cursor grab mode change can be attempted again (#16293) 2024-11-09 23:30:57 +00:00