2020-11-28 00:39:59 +00:00
|
|
|
[package]
|
|
|
|
name = "bevy_reflect"
|
2023-07-10 21:19:27 +00:00
|
|
|
version = "0.12.0-dev"
|
2021-10-27 00:12:14 +00:00
|
|
|
edition = "2021"
|
2020-11-28 00:39:59 +00:00
|
|
|
description = "Dynamically interact with rust types"
|
|
|
|
homepage = "https://bevyengine.org"
|
|
|
|
repository = "https://github.com/bevyengine/bevy"
|
2021-07-23 21:11:51 +00:00
|
|
|
license = "MIT OR Apache-2.0"
|
2020-11-28 00:39:59 +00:00
|
|
|
keywords = ["bevy"]
|
|
|
|
readme = "README.md"
|
|
|
|
|
|
|
|
[features]
|
2022-10-18 13:49:57 +00:00
|
|
|
default = []
|
2023-06-08 20:33:21 +00:00
|
|
|
# When enabled, provides Bevy-related reflection implementations
|
|
|
|
bevy = ["glam", "smallvec", "bevy_math", "smol_str"]
|
2022-10-18 13:49:57 +00:00
|
|
|
# When enabled, allows documentation comments to be accessed via reflection
|
|
|
|
documentation = ["bevy_reflect_derive/documentation"]
|
2020-11-28 00:39:59 +00:00
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
# bevy
|
2023-07-10 21:19:27 +00:00
|
|
|
bevy_math = { path = "../bevy_math", version = "0.12.0-dev", features = [
|
2023-06-08 20:33:21 +00:00
|
|
|
"serialize",
|
|
|
|
], optional = true }
|
2023-07-10 21:19:27 +00:00
|
|
|
bevy_reflect_derive = { path = "bevy_reflect_derive", version = "0.12.0-dev" }
|
|
|
|
bevy_utils = { path = "../bevy_utils", version = "0.12.0-dev" }
|
|
|
|
bevy_ptr = { path = "../bevy_ptr", version = "0.12.0-dev" }
|
2020-11-28 00:39:59 +00:00
|
|
|
|
|
|
|
# other
|
|
|
|
erased-serde = "0.3"
|
|
|
|
downcast-rs = "1.2"
|
|
|
|
thiserror = "1.0"
|
2023-01-09 21:57:14 +00:00
|
|
|
serde = "1"
|
2023-06-08 20:33:21 +00:00
|
|
|
smallvec = { version = "1.6", features = [
|
|
|
|
"serde",
|
|
|
|
"union",
|
|
|
|
"const_generics",
|
|
|
|
], optional = true }
|
2023-08-31 12:55:17 +00:00
|
|
|
glam = { version = "0.24.1", features = ["serde"], optional = true }
|
2023-06-08 20:33:21 +00:00
|
|
|
smol_str = { version = "0.2.0", optional = true }
|
2020-11-28 00:39:59 +00:00
|
|
|
|
|
|
|
[dev-dependencies]
|
2022-09-02 14:20:49 +00:00
|
|
|
ron = "0.8.0"
|
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, ®istry);
let bytes: Vec<u8> = postcard::to_allocvec(&serializer).unwrap();
println!("{:?}", bytes); // Output: [129, 217, 61, 98, ...]
// === Deserialize! === //
let deserializer = UntypedReflectDeserializer::new(®istry);
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
|
|
|
rmp-serde = "1.1"
|
|
|
|
bincode = "1.3"
|
2022-10-18 13:49:57 +00:00
|
|
|
|
|
|
|
[[example]]
|
|
|
|
name = "reflect_docs"
|
|
|
|
path = "examples/reflect_docs.rs"
|
|
|
|
required-features = ["documentation"]
|