bevy/crates
Gino Valente 97f7a1a99c bevy_reflect: Binary formats (#6140)
# Objective

Closes #5934

Currently it is not possible to de/serialize data to non-self-describing formats using reflection.

## Solution

Add support for non-self-describing de/serialization using reflection.

This allows us to use binary formatters, like [`postcard`](https://crates.io/crates/postcard):

```rust
#[derive(Reflect, FromReflect, Debug, PartialEq)]
struct Foo {
  data: String
}

let mut registry = TypeRegistry::new();
registry.register::<Foo>();

let input = Foo {
  data: "Hello world!".to_string()
};

// === Serialize! === //
let serializer = ReflectSerializer::new(&input, &registry);
let bytes: Vec<u8> = postcard::to_allocvec(&serializer).unwrap();

println!("{:?}", bytes); // Output: [129, 217, 61, 98, ...]

// === Deserialize! === //
let deserializer = UntypedReflectDeserializer::new(&registry);

let dynamic_output = deserializer
  .deserialize(&mut postcard::Deserializer::from_bytes(&bytes))
  .unwrap();

let output = <Foo as FromReflect>::from_reflect(dynamic_output.as_ref()).unwrap();

assert_eq!(expected, output); // OK!
```

#### Crates Tested

- ~~[`rmp-serde`](https://crates.io/crates/rmp-serde)~~ Apparently, this _is_ self-describing
- ~~[`bincode` v2.0.0-rc.1](https://crates.io/crates/bincode/2.0.0-rc.1) (using [this PR](https://github.com/bincode-org/bincode/pull/586))~~ This actually works for the latest release (v1.3.3) of [`bincode`](https://crates.io/crates/bincode) as well. You just need to be sure to use fixed-int encoding.
- [`postcard`](https://crates.io/crates/postcard)

## Future Work

Ideally, we would refactor the `serde` module, but I don't think I'll do that in this PR so as to keep the diff relatively small (and to avoid any painful rebases). This should probably be done once this is merged, though.

Some areas we could improve with a refactor:

* Split deserialization logic across multiple files
* Consolidate helper functions/structs
* Make the logic more DRY

---

## Changelog

- Add support for non-self-describing de/serialization using reflection.


Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
2022-11-04 02:22:54 +00:00
..
bevy_animation Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_app Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_asset Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_audio Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_core Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_core_pipeline Bloom (#6397) 2022-11-04 01:34:12 +00:00
bevy_derive Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_diagnostic Add Exponential Moving Average into diagnostics (#4992) 2022-10-24 13:46:37 +00:00
bevy_dylib Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_dynamic_plugin bevy_dynamic_plugin: make it possible to handle loading errors (#6437) 2022-11-01 11:35:44 +00:00
bevy_ecs Fix spawning empty bundles (#6425) 2022-11-03 22:50:41 +00:00
bevy_ecs_compile_fail_tests Fix trybuild tests broken by rust 1.65 (#6457) 2022-11-03 15:09:27 +00:00
bevy_encase_derive Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_gilrs feat: add GamepadInfo, expose gamepad names (#6342) 2022-10-24 14:33:50 +00:00
bevy_gltf fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_hierarchy Replace WorldQueryGats trait with actual gats (#6319) 2022-11-03 16:33:05 +00:00
bevy_input Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_internal Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_log Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_macro_utils fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_math Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_mikktspace Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_pbr Bloom (#6397) 2022-11-04 01:34:12 +00:00
bevy_ptr Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_reflect bevy_reflect: Binary formats (#6140) 2022-11-04 02:22:54 +00:00
bevy_render Replace WorldQueryGats trait with actual gats (#6319) 2022-11-03 16:33:05 +00:00
bevy_scene bevy_reflect: Binary formats (#6140) 2022-11-04 02:22:54 +00:00
bevy_sprite bevy_pbr: Fix incorrect and unnecessary normal-mapping code (#5766) 2022-11-03 20:37:32 +00:00
bevy_tasks TaskPool Panic Handling (#6443) 2022-11-02 23:40:08 +00:00
bevy_text Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_time TaskPool Panic Handling (#6443) 2022-11-02 23:40:08 +00:00
bevy_transform TaskPool Panic Handling (#6443) 2022-11-02 23:40:08 +00:00
bevy_ui Specialize UI pipeline on "hdr-ness" (#6459) 2022-11-03 21:14:03 +00:00
bevy_utils Remove Sync bound from Local (#5483) 2022-09-12 04:15:55 +00:00
bevy_window Revert "Show prelude re-exports in docs (#6448)" (#6449) 2022-11-02 20:40:45 +00:00
bevy_winit do not set cursor grab on window creation if not asked for (#6381) 2022-10-31 16:12:18 +00:00