bevy_reflect: Improve serialization format even more (#5723)

> Note: This is rebased off #4561 and can be viewed as a competitor to that PR. See `Comparison with #4561` section for details.

# Objective

The current serialization format used by `bevy_reflect` is both verbose and error-prone. Taking the following structs[^1] for example:

```rust
// -- src/inventory.rs

#[derive(Reflect)]
struct Inventory {
  id: String,
  max_storage: usize,
  items: Vec<Item>
}

#[derive(Reflect)]
struct Item {
  name: String
}
```

Given an inventory of a single item, this would serialize to something like:

```rust
// -- assets/inventory.ron

{
  "type": "my_game::inventory::Inventory",
  "struct": {
    "id": {
      "type": "alloc::string::String",
      "value": "inv001",
    },
    "max_storage": {
      "type": "usize",
      "value": 10
    },
    "items": {
      "type": "alloc::vec::Vec<alloc::string::String>",
      "list": [
        {
          "type": "my_game::inventory::Item",
          "struct": {
            "name": {
              "type": "alloc::string::String",
              "value": "Pickaxe"
            },
          },
        },
      ],
    },
  },
}
```

Aside from being really long and difficult to read, it also has a few "gotchas" that users need to be aware of if they want to edit the file manually. A major one is the requirement that you use the proper keys for a given type. For structs, you need `"struct"`. For lists, `"list"`. For tuple structs, `"tuple_struct"`. And so on.

It also ***requires*** that the `"type"` entry come before the actual data. Despite being a map— which in programming is almost always orderless by default— the entries need to be in a particular order. Failure to follow the ordering convention results in a failure to deserialize the data.

This makes it very prone to errors and annoyances.


## Solution

Using #4042, we can remove a lot of the boilerplate and metadata needed by this older system. Since we now have static access to type information, we can simplify our serialized data to look like:

```rust
// -- assets/inventory.ron

{
  "my_game::inventory::Inventory": (
    id: "inv001",
    max_storage: 10,
    items: [
      (
        name: "Pickaxe"
      ),
    ],
  ),
}
```

This is much more digestible and a lot less error-prone (no more key requirements and no more extra type names).

Additionally, it is a lot more familiar to users as it follows conventional serde mechanics. For example, the struct is represented with `(...)` when serialized to RON.

#### Custom Serialization

Additionally, this PR adds the opt-in ability to specify a custom serde implementation to be used rather than the one created via reflection. For example[^1]:

```rust
// -- src/inventory.rs

#[derive(Reflect, Serialize)]
#[reflect(Serialize)]
struct Item {
  #[serde(alias = "id")]
  name: String
}
```

```rust
// -- assets/inventory.ron

{
  "my_game::inventory::Inventory": (
    id: "inv001",
    max_storage: 10,
    items: [
      (
        id: "Pickaxe"
      ),
    ],
  ),
},
```

By allowing users to define their own serialization methods, we do two things:

1. We give more control over how data is serialized/deserialized to the end user
2. We avoid having to re-define serde's attributes and forcing users to apply both (e.g. we don't need a `#[reflect(alias)]` attribute).

### Improved Formats

One of the improvements this PR provides is the ability to represent data in ways that are more conventional and/or familiar to users. Many users are familiar with RON so here are some of the ways we can now represent data in RON:

###### Structs

```js
{
  "my_crate::Foo": (
    bar: 123
  )
}
// OR
{
  "my_crate::Foo": Foo(
    bar: 123
  )
}
```

<details>
<summary>Old Format</summary>

```js
{
  "type": "my_crate::Foo",
  "struct": {
    "bar": {
      "type": "usize",
      "value": 123
    }
  }
}
```

</details>

###### Tuples

```js
{
  "(f32, f32)": (1.0, 2.0)
}
```

<details>
<summary>Old Format</summary>

```js
{
  "type": "(f32, f32)",
  "tuple": [
    {
      "type": "f32",
      "value": 1.0
    },
    {
      "type": "f32",
      "value": 2.0
    }
  ]
}
```

</details>

###### Tuple Structs

```js
{
  "my_crate::Bar": ("Hello World!")
}
// OR
{
  "my_crate::Bar": Bar("Hello World!")
}
```

<details>
<summary>Old Format</summary>

```js
{
  "type": "my_crate::Bar",
  "tuple_struct": [
    {
      "type": "alloc::string::String",
      "value": "Hello World!"
    }
  ]
}
```

</details>

###### Arrays

It may be a bit surprising to some, but arrays now also use the tuple format. This is because they essentially _are_ tuples (a sequence of values with a fixed size), but only allow for homogenous types. Additionally, this is how RON handles them and is probably a result of the 32-capacity limit imposed on them (both by [serde](https://docs.rs/serde/latest/serde/trait.Serialize.html#impl-Serialize-for-%5BT%3B%2032%5D) and by [bevy_reflect](https://docs.rs/bevy/latest/bevy/reflect/trait.GetTypeRegistration.html#impl-GetTypeRegistration-for-%5BT%3B%2032%5D)).

```js
{
  "[i32; 3]": (1, 2, 3)
}
```

<details>
<summary>Old Format</summary>

```js
{
  "type": "[i32; 3]",
  "array": [
    {
      "type": "i32",
      "value": 1
    },
    {
      "type": "i32",
      "value": 2
    },
    {
      "type": "i32",
      "value": 3
    }
  ]
}
```

</details>

###### Enums

To make things simple, I'll just put a struct variant here, but the style applies to all variant types:

```js
{
  "my_crate::ItemType": Consumable(
    name: "Healing potion"
  )
}
```

<details>
<summary>Old Format</summary>

```js
{
  "type": "my_crate::ItemType",
  "enum": {
    "variant": "Consumable",
    "struct": {
      "name": {
        "type": "alloc::string::String",
        "value": "Healing potion"
      }
    }
  }
}
```

</details>

### Comparison with #4561

This PR is a rebased version of #4561. The reason for the split between the two is because this PR creates a _very_ different scene format. You may notice that the PR descriptions for either PR are pretty similar. This was done to better convey the changes depending on which (if any) gets merged first. If #4561 makes it in first, I will update this PR description accordingly.

---

## Changelog

* Re-worked serialization/deserialization for reflected types
* Added `TypedReflectDeserializer` for deserializing data with known `TypeInfo`
* Renamed `ReflectDeserializer` to `UntypedReflectDeserializer` 
* ~~Replaced usages of `deserialize_any` with `deserialize_map` for non-self-describing formats~~ Reverted this change since there are still some issues that need to be sorted out (in a separate PR). By reverting this, crates like `bincode` can throw an error when attempting to deserialize non-self-describing formats (`bincode` results in `DeserializeAnyNotSupported`)
* Structs, tuples, tuple structs, arrays, and enums are now all de/serialized using conventional serde methods

## Migration Guide

* This PR reduces the verbosity of the scene format. Scenes will need to be updated accordingly:

```js
// Old format
{
  "type": "my_game::item::Item",
  "struct": {
    "id": {
      "type": "alloc::string::String",
      "value": "bevycraft:stone",
    },
    "tags": {
      "type": "alloc::vec::Vec<alloc::string::String>",
      "list": [
        {
          "type": "alloc::string::String",
          "value": "material"
        },
      ],
    },
}

// New format
{
  "my_game::item::Item": (
    id: "bevycraft:stone",
    tags: ["material"]
  )
}
```

[^1]: Some derives omitted for brevity.
This commit is contained in:
Gino Valente 2022-09-20 19:38:18 +00:00
parent bc863cec4d
commit d30d3e752a
22 changed files with 1652 additions and 1010 deletions

View file

@ -3,43 +3,30 @@
entity: 0, entity: 0,
components: [ components: [
{ {
"type": "bevy_transform::components::transform::Transform", "bevy_transform::components::transform::Transform": (
"struct": { translation: (
"translation": { x: 0.0,
"type": "glam::f32::vec3::Vec3", y: 0.0,
"value": (0.0, 0.0, 0.0), z: 0.0
}, ),
"rotation": { rotation: (0.0, 0.0, 0.0, 1.0),
"type": "glam::f32::sse2::quat::Quat", scale: (
"value": (0.0, 0.0, 0.0, 1.0), x: 1.0,
}, y: 1.0,
"scale": { z: 1.0
"type": "glam::f32::vec3::Vec3", ),
"value": (1.0, 1.0, 1.0), ),
},
},
}, },
{ {
"type": "scene::ComponentB", "scene::ComponentB": (
"struct": { value: "hello",
"value": { ),
"type": "alloc::string::String",
"value": "hello",
},
},
}, },
{ {
"type": "scene::ComponentA", "scene::ComponentA": (
"struct": { x: 1.0,
"x": { y: 2.0,
"type": "f32", ),
"value": 1.0,
},
"y": {
"type": "f32",
"value": 2.0,
},
},
}, },
], ],
), ),
@ -47,17 +34,10 @@
entity: 1, entity: 1,
components: [ components: [
{ {
"type": "scene::ComponentA", "scene::ComponentA": (
"struct": { x: 3.0,
"x": { y: 4.0,
"type": "f32", ),
"value": 3.0,
},
"y": {
"type": "f32",
"value": 4.0,
},
},
}, },
], ],
), ),

View file

@ -21,6 +21,7 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
enum_name_at, enum_name_at,
enum_field_len, enum_field_len,
enum_variant_name, enum_variant_name,
enum_variant_index,
enum_variant_type, enum_variant_type,
} = generate_impls(reflect_enum, &ref_index, &ref_name); } = generate_impls(reflect_enum, &ref_index, &ref_name);
@ -53,12 +54,13 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
} }
}); });
let string_name = enum_name.to_string();
let typed_impl = impl_typed( let typed_impl = impl_typed(
enum_name, enum_name,
reflect_enum.meta().generics(), reflect_enum.meta().generics(),
quote! { quote! {
let variants = [#(#variant_info),*]; let variants = [#(#variant_info),*];
let info = #bevy_reflect_path::EnumInfo::new::<Self>(&variants); let info = #bevy_reflect_path::EnumInfo::new::<Self>(#string_name, &variants);
#bevy_reflect_path::TypeInfo::Enum(info) #bevy_reflect_path::TypeInfo::Enum(info)
}, },
bevy_reflect_path, bevy_reflect_path,
@ -136,6 +138,14 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
} }
} }
#[inline]
fn variant_index(&self) -> usize {
match self {
#(#enum_variant_index,)*
_ => unreachable!(),
}
}
#[inline] #[inline]
fn variant_type(&self) -> #bevy_reflect_path::VariantType { fn variant_type(&self) -> #bevy_reflect_path::VariantType {
match self { match self {
@ -254,6 +264,7 @@ struct EnumImpls {
enum_name_at: Vec<proc_macro2::TokenStream>, enum_name_at: Vec<proc_macro2::TokenStream>,
enum_field_len: Vec<proc_macro2::TokenStream>, enum_field_len: Vec<proc_macro2::TokenStream>,
enum_variant_name: Vec<proc_macro2::TokenStream>, enum_variant_name: Vec<proc_macro2::TokenStream>,
enum_variant_index: Vec<proc_macro2::TokenStream>,
enum_variant_type: Vec<proc_macro2::TokenStream>, enum_variant_type: Vec<proc_macro2::TokenStream>,
} }
@ -267,13 +278,21 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
let mut enum_name_at = Vec::new(); let mut enum_name_at = Vec::new();
let mut enum_field_len = Vec::new(); let mut enum_field_len = Vec::new();
let mut enum_variant_name = Vec::new(); let mut enum_variant_name = Vec::new();
let mut enum_variant_index = Vec::new();
let mut enum_variant_type = Vec::new(); let mut enum_variant_type = Vec::new();
for variant in reflect_enum.variants() { for (variant_index, variant) in reflect_enum.variants().iter().enumerate() {
let ident = &variant.data.ident; let ident = &variant.data.ident;
let name = ident.to_string(); let name = ident.to_string();
let unit = reflect_enum.get_unit(ident); let unit = reflect_enum.get_unit(ident);
enum_variant_name.push(quote! {
#unit{..} => #name
});
enum_variant_index.push(quote! {
#unit{..} => #variant_index
});
fn for_fields( fn for_fields(
fields: &[StructField], fields: &[StructField],
mut generate_for_field: impl FnMut(usize, usize, &StructField) -> proc_macro2::TokenStream, mut generate_for_field: impl FnMut(usize, usize, &StructField) -> proc_macro2::TokenStream,
@ -301,9 +320,6 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
enum_field_len.push(quote! { enum_field_len.push(quote! {
#unit{..} => #field_len #unit{..} => #field_len
}); });
enum_variant_name.push(quote! {
#unit{..} => #name
});
enum_variant_type.push(quote! { enum_variant_type.push(quote! {
#unit{..} => #bevy_reflect_path::VariantType::#variant #unit{..} => #bevy_reflect_path::VariantType::#variant
}); });
@ -342,7 +358,7 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
}); });
let field_ty = &field.data.ty; let field_ty = &field.data.ty;
quote! { #bevy_reflect_path::NamedField::new::<#field_ty, _>(#field_name) } quote! { #bevy_reflect_path::NamedField::new::<#field_ty>(#field_name) }
}); });
let arguments = quote!(#name, &[ #(#argument),* ]); let arguments = quote!(#name, &[ #(#argument),* ]);
add_fields_branch("Struct", "StructVariantInfo", arguments, field_len); add_fields_branch("Struct", "StructVariantInfo", arguments, field_len);
@ -358,6 +374,7 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
enum_name_at, enum_name_at,
enum_field_len, enum_field_len,
enum_variant_name, enum_variant_name,
enum_variant_index,
enum_variant_type, enum_variant_type,
} }
} }

View file

@ -51,14 +51,15 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
} }
}); });
let string_name = struct_name.to_string();
let typed_impl = impl_typed( let typed_impl = impl_typed(
struct_name, struct_name,
reflect_struct.meta().generics(), reflect_struct.meta().generics(),
quote! { quote! {
let fields = [ let fields = [
#(#bevy_reflect_path::NamedField::new::<#field_types, _>(#field_names),)* #(#bevy_reflect_path::NamedField::new::<#field_types>(#field_names),)*
]; ];
let info = #bevy_reflect_path::StructInfo::new::<Self>(&fields); let info = #bevy_reflect_path::StructInfo::new::<Self>(#string_name, &fields);
#bevy_reflect_path::TypeInfo::Struct(info) #bevy_reflect_path::TypeInfo::Struct(info)
}, },
bevy_reflect_path, bevy_reflect_path,

View file

@ -35,6 +35,7 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
} }
}); });
let string_name = struct_name.to_string();
let typed_impl = impl_typed( let typed_impl = impl_typed(
struct_name, struct_name,
reflect_struct.meta().generics(), reflect_struct.meta().generics(),
@ -42,7 +43,7 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
let fields = [ let fields = [
#(#bevy_reflect_path::UnnamedField::new::<#field_types>(#field_idents),)* #(#bevy_reflect_path::UnnamedField::new::<#field_types>(#field_idents),)*
]; ];
let info = #bevy_reflect_path::TupleStructInfo::new::<Self>(&fields); let info = #bevy_reflect_path::TupleStructInfo::new::<Self>(#string_name, &fields);
#bevy_reflect_path::TypeInfo::TupleStruct(info) #bevy_reflect_path::TypeInfo::TupleStruct(info)
}, },
bevy_reflect_path, bevy_reflect_path,

View file

@ -124,10 +124,7 @@ where
let mut bitset = BitSet::default(); let mut bitset = BitSet::default();
member_iter.fold(0, |next_idx, member| match member { member_iter.fold(0, |next_idx, member| match member {
ReflectIgnoreBehavior::IgnoreAlways => { ReflectIgnoreBehavior::IgnoreAlways => next_idx,
bitset.insert(next_idx);
next_idx
}
ReflectIgnoreBehavior::IgnoreSerialization => { ReflectIgnoreBehavior::IgnoreSerialization => {
bitset.insert(next_idx); bitset.insert(next_idx);
next_idx + 1 next_idx + 1

View file

@ -77,6 +77,7 @@ impl From<()> for DynamicVariant {
pub struct DynamicEnum { pub struct DynamicEnum {
name: String, name: String,
variant_name: String, variant_name: String,
variant_index: usize,
variant: DynamicVariant, variant: DynamicVariant,
} }
@ -96,6 +97,30 @@ impl DynamicEnum {
) -> Self { ) -> Self {
Self { Self {
name: name.into(), name: name.into(),
variant_index: 0,
variant_name: variant_name.into(),
variant: variant.into(),
}
}
/// Create a new [`DynamicEnum`] with a variant index to represent an enum at runtime.
///
/// # Arguments
///
/// * `name`: The type name of the enum
/// * `variant_index`: The index of the variant to set
/// * `variant_name`: The name of the variant to set
/// * `variant`: The variant data
///
pub fn new_with_index<I: Into<String>, V: Into<DynamicVariant>>(
name: I,
variant_index: usize,
variant_name: I,
variant: V,
) -> Self {
Self {
name: name.into(),
variant_index,
variant_name: variant_name.into(), variant_name: variant_name.into(),
variant: variant.into(), variant: variant.into(),
} }
@ -117,6 +142,18 @@ impl DynamicEnum {
self.variant = variant.into(); self.variant = variant.into();
} }
/// Set the current enum variant represented by this struct along with its variant index.
pub fn set_variant_with_index<I: Into<String>, V: Into<DynamicVariant>>(
&mut self,
variant_index: usize,
name: I,
variant: V,
) {
self.variant_index = variant_index;
self.variant_name = name.into();
self.variant = variant.into();
}
/// Create a [`DynamicEnum`] from an existing one. /// Create a [`DynamicEnum`] from an existing one.
/// ///
/// This is functionally the same as [`DynamicEnum::from_ref`] except it takes an owned value. /// This is functionally the same as [`DynamicEnum::from_ref`] except it takes an owned value.
@ -129,8 +166,9 @@ impl DynamicEnum {
/// This is functionally the same as [`DynamicEnum::from`] except it takes a reference. /// This is functionally the same as [`DynamicEnum::from`] except it takes a reference.
pub fn from_ref<TEnum: Enum>(value: &TEnum) -> Self { pub fn from_ref<TEnum: Enum>(value: &TEnum) -> Self {
match value.variant_type() { match value.variant_type() {
VariantType::Unit => DynamicEnum::new( VariantType::Unit => DynamicEnum::new_with_index(
value.type_name(), value.type_name(),
value.variant_index(),
value.variant_name(), value.variant_name(),
DynamicVariant::Unit, DynamicVariant::Unit,
), ),
@ -139,8 +177,9 @@ impl DynamicEnum {
for field in value.iter_fields() { for field in value.iter_fields() {
data.insert_boxed(field.value().clone_value()); data.insert_boxed(field.value().clone_value());
} }
DynamicEnum::new( DynamicEnum::new_with_index(
value.type_name(), value.type_name(),
value.variant_index(),
value.variant_name(), value.variant_name(),
DynamicVariant::Tuple(data), DynamicVariant::Tuple(data),
) )
@ -151,8 +190,9 @@ impl DynamicEnum {
let name = field.name().unwrap(); let name = field.name().unwrap();
data.insert_boxed(name, field.value().clone_value()); data.insert_boxed(name, field.value().clone_value());
} }
DynamicEnum::new( DynamicEnum::new_with_index(
value.type_name(), value.type_name(),
value.variant_index(),
value.variant_name(), value.variant_name(),
DynamicVariant::Struct(data), DynamicVariant::Struct(data),
) )
@ -226,6 +266,10 @@ impl Enum for DynamicEnum {
&self.variant_name &self.variant_name
} }
fn variant_index(&self) -> usize {
self.variant_index
}
fn variant_type(&self) -> VariantType { fn variant_type(&self) -> VariantType {
match &self.variant { match &self.variant {
DynamicVariant::Unit => VariantType::Unit, DynamicVariant::Unit => VariantType::Unit,
@ -237,6 +281,7 @@ impl Enum for DynamicEnum {
fn clone_dynamic(&self) -> DynamicEnum { fn clone_dynamic(&self) -> DynamicEnum {
Self { Self {
name: self.name.clone(), name: self.name.clone(),
variant_index: self.variant_index,
variant_name: self.variant_name.clone(), variant_name: self.variant_name.clone(),
variant: self.variant.clone(), variant: self.variant.clone(),
} }

View file

@ -1,7 +1,6 @@
use crate::{DynamicEnum, Reflect, VariantInfo, VariantType}; use crate::{DynamicEnum, Reflect, VariantInfo, VariantType};
use bevy_utils::HashMap; use bevy_utils::HashMap;
use std::any::{Any, TypeId}; use std::any::{Any, TypeId};
use std::borrow::Cow;
use std::slice::Iter; use std::slice::Iter;
/// A trait representing a [reflected] enum. /// A trait representing a [reflected] enum.
@ -114,6 +113,8 @@ pub trait Enum: Reflect {
fn field_len(&self) -> usize; fn field_len(&self) -> usize;
/// The name of the current variant. /// The name of the current variant.
fn variant_name(&self) -> &str; fn variant_name(&self) -> &str;
/// The index of the current variant.
fn variant_index(&self) -> usize;
/// The type of the current variant. /// The type of the current variant.
fn variant_type(&self) -> VariantType; fn variant_type(&self) -> VariantType;
// Clones the enum into a [`DynamicEnum`]. // Clones the enum into a [`DynamicEnum`].
@ -131,10 +132,11 @@ pub trait Enum: Reflect {
/// A container for compile-time enum info, used by [`TypeInfo`](crate::TypeInfo). /// A container for compile-time enum info, used by [`TypeInfo`](crate::TypeInfo).
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct EnumInfo { pub struct EnumInfo {
name: &'static str,
type_name: &'static str, type_name: &'static str,
type_id: TypeId, type_id: TypeId,
variants: Box<[VariantInfo]>, variants: Box<[VariantInfo]>,
variant_indices: HashMap<Cow<'static, str>, usize>, variant_indices: HashMap<&'static str, usize>,
} }
impl EnumInfo { impl EnumInfo {
@ -142,19 +144,18 @@ impl EnumInfo {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `name`: The name of this enum (_without_ generics or lifetimes)
/// * `variants`: The variants of this enum in the order they are defined /// * `variants`: The variants of this enum in the order they are defined
/// ///
pub fn new<TEnum: Enum>(variants: &[VariantInfo]) -> Self { pub fn new<TEnum: Enum>(name: &'static str, variants: &[VariantInfo]) -> Self {
let variant_indices = variants let variant_indices = variants
.iter() .iter()
.enumerate() .enumerate()
.map(|(index, variant)| { .map(|(index, variant)| (variant.name(), index))
let name = variant.name().clone();
(name, index)
})
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
Self { Self {
name,
type_name: std::any::type_name::<TEnum>(), type_name: std::any::type_name::<TEnum>(),
type_id: TypeId::of::<TEnum>(), type_id: TypeId::of::<TEnum>(),
variants: variants.to_vec().into_boxed_slice(), variants: variants.to_vec().into_boxed_slice(),
@ -201,6 +202,15 @@ impl EnumInfo {
self.variants.len() self.variants.len()
} }
/// The name of the enum.
///
/// This does _not_ include any generics or lifetimes.
///
/// For example, `foo::bar::Baz<'a, T>` would simply be `Baz`.
pub fn name(&self) -> &'static str {
self.name
}
/// The [type name] of the enum. /// The [type name] of the enum.
/// ///
/// [type name]: std::any::type_name /// [type name]: std::any::type_name

View file

@ -1,6 +1,5 @@
use crate::{NamedField, UnnamedField}; use crate::{NamedField, UnnamedField};
use bevy_utils::HashMap; use bevy_utils::HashMap;
use std::borrow::Cow;
use std::slice::Iter; use std::slice::Iter;
/// Describes the form of an enum variant. /// Describes the form of an enum variant.
@ -66,7 +65,7 @@ pub enum VariantInfo {
} }
impl VariantInfo { impl VariantInfo {
pub fn name(&self) -> &Cow<'static, str> { pub fn name(&self) -> &'static str {
match self { match self {
Self::Struct(info) => info.name(), Self::Struct(info) => info.name(),
Self::Tuple(info) => info.name(), Self::Tuple(info) => info.name(),
@ -78,39 +77,25 @@ impl VariantInfo {
/// Type info for struct variants. /// Type info for struct variants.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct StructVariantInfo { pub struct StructVariantInfo {
name: Cow<'static, str>, name: &'static str,
fields: Box<[NamedField]>, fields: Box<[NamedField]>,
field_indices: HashMap<Cow<'static, str>, usize>, field_indices: HashMap<&'static str, usize>,
} }
impl StructVariantInfo { impl StructVariantInfo {
/// Create a new [`StructVariantInfo`]. /// Create a new [`StructVariantInfo`].
pub fn new(name: &str, fields: &[NamedField]) -> Self { pub fn new(name: &'static str, fields: &[NamedField]) -> Self {
let field_indices = Self::collect_field_indices(fields);
Self {
name: Cow::Owned(name.into()),
fields: fields.to_vec().into_boxed_slice(),
field_indices,
}
}
/// Create a new [`StructVariantInfo`] using a static string.
///
/// This helps save an allocation when the string has a static lifetime, such
/// as when using defined sa a literal.
pub fn new_static(name: &'static str, fields: &[NamedField]) -> Self {
let field_indices = Self::collect_field_indices(fields); let field_indices = Self::collect_field_indices(fields);
Self { Self {
name: Cow::Borrowed(name), name,
fields: fields.to_vec().into_boxed_slice(), fields: fields.to_vec().into_boxed_slice(),
field_indices, field_indices,
} }
} }
/// The name of this variant. /// The name of this variant.
pub fn name(&self) -> &Cow<'static, str> { pub fn name(&self) -> &'static str {
&self.name self.name
} }
/// Get the field with the given name. /// Get the field with the given name.
@ -140,14 +125,11 @@ impl StructVariantInfo {
self.fields.len() self.fields.len()
} }
fn collect_field_indices(fields: &[NamedField]) -> HashMap<Cow<'static, str>, usize> { fn collect_field_indices(fields: &[NamedField]) -> HashMap<&'static str, usize> {
fields fields
.iter() .iter()
.enumerate() .enumerate()
.map(|(index, field)| { .map(|(index, field)| (field.name(), index))
let name = field.name().clone();
(name, index)
})
.collect() .collect()
} }
} }
@ -155,33 +137,22 @@ impl StructVariantInfo {
/// Type info for tuple variants. /// Type info for tuple variants.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct TupleVariantInfo { pub struct TupleVariantInfo {
name: Cow<'static, str>, name: &'static str,
fields: Box<[UnnamedField]>, fields: Box<[UnnamedField]>,
} }
impl TupleVariantInfo { impl TupleVariantInfo {
/// Create a new [`TupleVariantInfo`]. /// Create a new [`TupleVariantInfo`].
pub fn new(name: &str, fields: &[UnnamedField]) -> Self { pub fn new(name: &'static str, fields: &[UnnamedField]) -> Self {
Self { Self {
name: Cow::Owned(name.into()), name,
fields: fields.to_vec().into_boxed_slice(),
}
}
/// Create a new [`TupleVariantInfo`] using a static string.
///
/// This helps save an allocation when the string has a static lifetime, such
/// as when using defined sa a literal.
pub fn new_static(name: &'static str, fields: &[UnnamedField]) -> Self {
Self {
name: Cow::Borrowed(name),
fields: fields.to_vec().into_boxed_slice(), fields: fields.to_vec().into_boxed_slice(),
} }
} }
/// The name of this variant. /// The name of this variant.
pub fn name(&self) -> &Cow<'static, str> { pub fn name(&self) -> &'static str {
&self.name self.name
} }
/// Get the field at the given index. /// Get the field at the given index.
@ -203,29 +174,17 @@ impl TupleVariantInfo {
/// Type info for unit variants. /// Type info for unit variants.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct UnitVariantInfo { pub struct UnitVariantInfo {
name: Cow<'static, str>, name: &'static str,
} }
impl UnitVariantInfo { impl UnitVariantInfo {
/// Create a new [`UnitVariantInfo`]. /// Create a new [`UnitVariantInfo`].
pub fn new(name: &str) -> Self { pub fn new(name: &'static str) -> Self {
Self { Self { name }
name: Cow::Owned(name.into()),
}
}
/// Create a new [`UnitVariantInfo`] using a static string.
///
/// This helps save an allocation when the string has a static lifetime, such
/// as when using defined sa a literal.
pub fn new_static(name: &'static str) -> Self {
Self {
name: Cow::Borrowed(name),
}
} }
/// The name of this variant. /// The name of this variant.
pub fn name(&self) -> &Cow<'static, str> { pub fn name(&self) -> &'static str {
&self.name self.name
} }
} }

View file

@ -1,28 +1,27 @@
use crate::Reflect; use crate::Reflect;
use std::any::{Any, TypeId}; use std::any::{Any, TypeId};
use std::borrow::Cow;
/// The named field of a reflected struct. /// The named field of a reflected struct.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct NamedField { pub struct NamedField {
name: Cow<'static, str>, name: &'static str,
type_name: &'static str, type_name: &'static str,
type_id: TypeId, type_id: TypeId,
} }
impl NamedField { impl NamedField {
/// Create a new [`NamedField`]. /// Create a new [`NamedField`].
pub fn new<T: Reflect, TName: Into<Cow<'static, str>>>(name: TName) -> Self { pub fn new<T: Reflect>(name: &'static str) -> Self {
Self { Self {
name: name.into(), name,
type_name: std::any::type_name::<T>(), type_name: std::any::type_name::<T>(),
type_id: TypeId::of::<T>(), type_id: TypeId::of::<T>(),
} }
} }
/// The name of the field. /// The name of the field.
pub fn name(&self) -> &Cow<'static, str> { pub fn name(&self) -> &'static str {
&self.name self.name
} }
/// The [type name] of the field. /// The [type name] of the field.

View file

@ -6,14 +6,14 @@ use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_struct, impl_ref
use glam::*; use glam::*;
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct IVec2 { struct IVec2 {
x: i32, x: i32,
y: i32, y: i32,
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct IVec3 { struct IVec3 {
x: i32, x: i32,
y: i32, y: i32,
@ -21,7 +21,7 @@ impl_reflect_struct!(
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct IVec4 { struct IVec4 {
x: i32, x: i32,
y: i32, y: i32,
@ -31,14 +31,14 @@ impl_reflect_struct!(
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct UVec2 { struct UVec2 {
x: u32, x: u32,
y: u32, y: u32,
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct UVec3 { struct UVec3 {
x: u32, x: u32,
y: u32, y: u32,
@ -46,7 +46,7 @@ impl_reflect_struct!(
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct UVec4 { struct UVec4 {
x: u32, x: u32,
y: u32, y: u32,
@ -56,14 +56,14 @@ impl_reflect_struct!(
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct Vec2 { struct Vec2 {
x: f32, x: f32,
y: f32, y: f32,
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct Vec3 { struct Vec3 {
x: f32, x: f32,
y: f32, y: f32,
@ -71,7 +71,7 @@ impl_reflect_struct!(
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct Vec3A { struct Vec3A {
x: f32, x: f32,
y: f32, y: f32,
@ -79,7 +79,7 @@ impl_reflect_struct!(
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct Vec4 { struct Vec4 {
x: f32, x: f32,
y: f32, y: f32,
@ -114,14 +114,14 @@ impl_reflect_struct!(
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct DVec2 { struct DVec2 {
x: f64, x: f64,
y: f64, y: f64,
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct DVec3 { struct DVec3 {
x: f64, x: f64,
y: f64, y: f64,
@ -129,7 +129,7 @@ impl_reflect_struct!(
} }
); );
impl_reflect_struct!( impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] #[reflect(Debug, PartialEq, Default)]
struct DVec4 { struct DVec4 {
x: f64, x: f64,
y: f64, y: f64,

View file

@ -683,6 +683,13 @@ impl<T: FromReflect> Enum for Option<T> {
} }
} }
fn variant_index(&self) -> usize {
match self {
None => 0,
Some(..) => 1,
}
}
#[inline] #[inline]
fn variant_type(&self) -> VariantType { fn variant_type(&self) -> VariantType {
match self { match self {
@ -845,12 +852,13 @@ impl<T: FromReflect> Typed for Option<T> {
fn type_info() -> &'static TypeInfo { fn type_info() -> &'static TypeInfo {
static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new(); static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
CELL.get_or_insert::<Self, _>(|| { CELL.get_or_insert::<Self, _>(|| {
let none_variant = VariantInfo::Unit(UnitVariantInfo::new_static("None")); let none_variant = VariantInfo::Unit(UnitVariantInfo::new("None"));
let some_variant = VariantInfo::Tuple(TupleVariantInfo::new_static( let some_variant =
"Some", VariantInfo::Tuple(TupleVariantInfo::new("Some", &[UnnamedField::new::<T>(0)]));
&[UnnamedField::new::<T>(0)], TypeInfo::Enum(EnumInfo::new::<Self>(
)); "Option",
TypeInfo::Enum(EnumInfo::new::<Self>(&[none_variant, some_variant])) &[none_variant, some_variant],
))
}) })
} }
} }

View file

@ -97,7 +97,7 @@ pub mod __macro_exports {
mod tests { mod tests {
#[cfg(feature = "glam")] #[cfg(feature = "glam")]
use ::glam::{vec3, Vec3}; use ::glam::{vec3, Vec3};
use ::serde::de::DeserializeSeed; use ::serde::{de::DeserializeSeed, Deserialize, Serialize};
use bevy_utils::HashMap; use bevy_utils::HashMap;
use ron::{ use ron::{
ser::{to_string_pretty, PrettyConfig}, ser::{to_string_pretty, PrettyConfig},
@ -108,7 +108,7 @@ mod tests {
use super::prelude::*; use super::prelude::*;
use super::*; use super::*;
use crate as bevy_reflect; use crate as bevy_reflect;
use crate::serde::{ReflectDeserializer, ReflectSerializer}; use crate::serde::{ReflectSerializer, UntypedReflectDeserializer};
#[test] #[test]
fn reflect_struct() { fn reflect_struct() {
@ -455,7 +455,8 @@ mod tests {
h: [u32; 2], h: [u32; 2],
} }
#[derive(Reflect)] #[derive(Reflect, Serialize, Deserialize)]
#[reflect(Serialize, Deserialize)]
struct Bar { struct Bar {
x: u32, x: u32,
} }
@ -476,18 +477,23 @@ mod tests {
let mut registry = TypeRegistry::default(); let mut registry = TypeRegistry::default();
registry.register::<u32>(); registry.register::<u32>();
registry.register::<isize>();
registry.register::<usize>();
registry.register::<Bar>();
registry.register::<String>();
registry.register::<i8>(); registry.register::<i8>();
registry.register::<i32>(); registry.register::<i32>();
registry.register::<usize>();
registry.register::<isize>();
registry.register::<Foo>();
registry.register::<Bar>();
registry.register::<String>();
registry.register::<Vec<isize>>();
registry.register::<HashMap<usize, i8>>();
registry.register::<(i32, Vec<isize>, Bar)>();
registry.register::<[u32; 2]>();
let serializer = ReflectSerializer::new(&foo, &registry); let serializer = ReflectSerializer::new(&foo, &registry);
let serialized = to_string_pretty(&serializer, PrettyConfig::default()).unwrap(); let serialized = to_string_pretty(&serializer, PrettyConfig::default()).unwrap();
let mut deserializer = Deserializer::from_str(&serialized).unwrap(); let mut deserializer = Deserializer::from_str(&serialized).unwrap();
let reflect_deserializer = ReflectDeserializer::new(&registry); let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let value = reflect_deserializer.deserialize(&mut deserializer).unwrap(); let value = reflect_deserializer.deserialize(&mut deserializer).unwrap();
let dynamic_struct = value.take::<DynamicStruct>().unwrap(); let dynamic_struct = value.take::<DynamicStruct>().unwrap();
@ -956,23 +962,38 @@ bevy_reflect::tests::should_reflect_debug::Test {
let ser = ReflectSerializer::new(&v, &registry); let ser = ReflectSerializer::new(&v, &registry);
let result = ron::to_string(&ser).expect("Failed to serialize to string"); let config = PrettyConfig::default()
.new_line(String::from("\n"))
.indentor(String::from(" "));
let output = to_string_pretty(&ser, config).unwrap();
let expected = r#"
{
"glam::f32::vec3::Vec3": (
x: 12.0,
y: 3.0,
z: -6.9,
),
}"#;
assert_eq!( assert_eq!(expected, format!("\n{}", output));
result,
r#"{"type":"glam::f32::vec3::Vec3","struct":{"x":{"type":"f32","value":12.0},"y":{"type":"f32","value":3.0},"z":{"type":"f32","value":-6.9}}}"#
);
} }
#[test] #[test]
fn vec3_deserialization() { fn vec3_deserialization() {
let data = r#"{"type":"glam::vec3::Vec3","struct":{"x":{"type":"f32","value":12},"y":{"type":"f32","value":3},"z":{"type":"f32","value":-6.9}}}"#; let data = r#"
{
"glam::f32::vec3::Vec3": (
x: 12.0,
y: 3.0,
z: -6.9,
),
}"#;
let mut registry = TypeRegistry::default(); let mut registry = TypeRegistry::default();
registry.add_registration(Vec3::get_type_registration()); registry.add_registration(Vec3::get_type_registration());
registry.add_registration(f32::get_type_registration()); registry.add_registration(f32::get_type_registration());
let de = ReflectDeserializer::new(&registry); let de = UntypedReflectDeserializer::new(&registry);
let mut deserializer = let mut deserializer =
ron::de::Deserializer::from_str(data).expect("Failed to acquire deserializer"); ron::de::Deserializer::from_str(data).expect("Failed to acquire deserializer");

File diff suppressed because it is too large Load diff

View file

@ -6,24 +6,11 @@ pub use de::*;
pub use ser::*; pub use ser::*;
pub use type_data::*; pub use type_data::*;
pub(crate) mod type_fields {
pub const TYPE: &str = "type";
pub const MAP: &str = "map";
pub const STRUCT: &str = "struct";
pub const TUPLE_STRUCT: &str = "tuple_struct";
pub const ENUM: &str = "enum";
pub const VARIANT: &str = "variant";
pub const TUPLE: &str = "tuple";
pub const LIST: &str = "list";
pub const ARRAY: &str = "array";
pub const VALUE: &str = "value";
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{self as bevy_reflect, DynamicTupleStruct}; use crate::{self as bevy_reflect, DynamicTupleStruct};
use crate::{ use crate::{
serde::{ReflectDeserializer, ReflectSerializer}, serde::{ReflectSerializer, UntypedReflectDeserializer},
type_registry::TypeRegistry, type_registry::TypeRegistry,
DynamicStruct, Reflect, DynamicStruct, Reflect,
}; };
@ -61,7 +48,7 @@ mod tests {
expected.insert("d", 6); expected.insert("d", 6);
let mut deserializer = ron::de::Deserializer::from_str(&serialized).unwrap(); let mut deserializer = ron::de::Deserializer::from_str(&serialized).unwrap();
let reflect_deserializer = ReflectDeserializer::new(&registry); let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let value = reflect_deserializer.deserialize(&mut deserializer).unwrap(); let value = reflect_deserializer.deserialize(&mut deserializer).unwrap();
let deserialized = value.take::<DynamicStruct>().unwrap(); let deserialized = value.take::<DynamicStruct>().unwrap();
@ -96,7 +83,7 @@ mod tests {
expected.insert(6); expected.insert(6);
let mut deserializer = ron::de::Deserializer::from_str(&serialized).unwrap(); let mut deserializer = ron::de::Deserializer::from_str(&serialized).unwrap();
let reflect_deserializer = ReflectDeserializer::new(&registry); let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let value = reflect_deserializer.deserialize(&mut deserializer).unwrap(); let value = reflect_deserializer.deserialize(&mut deserializer).unwrap();
let deserialized = value.take::<DynamicTupleStruct>().unwrap(); let deserialized = value.take::<DynamicTupleStruct>().unwrap();

View file

@ -1,11 +1,14 @@
use crate::{ use crate::{
serde::type_fields, Array, Enum, List, Map, Reflect, ReflectRef, ReflectSerialize, Struct, Array, Enum, List, Map, Reflect, ReflectRef, ReflectSerialize, Struct, Tuple, TupleStruct,
Tuple, TupleStruct, TypeRegistry, VariantType, TypeInfo, TypeRegistry, VariantInfo, VariantType,
};
use serde::ser::{
Error, SerializeStruct, SerializeStructVariant, SerializeTuple, SerializeTupleStruct,
SerializeTupleVariant,
}; };
use serde::ser::Error;
use serde::{ use serde::{
ser::{SerializeMap, SerializeSeq}, ser::{SerializeMap, SerializeSeq},
Serialize, Serializer, Serialize,
}; };
use super::SerializationData; use super::SerializationData;
@ -40,6 +43,34 @@ fn get_serializable<'a, E: serde::ser::Error>(
Ok(reflect_serialize.get_serializable(reflect_value)) Ok(reflect_serialize.get_serializable(reflect_value))
} }
/// Get the underlying [`TypeInfo`] of a given type.
///
/// If the given type is a [`TypeInfo::Dynamic`] then we need to try and look
/// up the actual type in the registry.
fn get_type_info<E: Error>(
type_info: &'static TypeInfo,
type_name: &str,
registry: &TypeRegistry,
) -> Result<&'static TypeInfo, E> {
match type_info {
TypeInfo::Dynamic(..) => match registry.get_with_name(type_name) {
Some(registration) => Ok(registration.type_info()),
None => Err(Error::custom(format_args!(
"no registration found for dynamic type with name {}",
type_name
))),
},
info => Ok(info),
}
}
/// A general purpose serializer for reflected types.
///
/// The serialized data will take the form of a map containing the following entries:
/// 1. `type`: The _full_ [type name]
/// 2. `value`: The serialized value of the reflected type
///
/// [type name]: std::any::type_name
pub struct ReflectSerializer<'a> { pub struct ReflectSerializer<'a> {
pub value: &'a dyn Reflect, pub value: &'a dyn Reflect,
pub registry: &'a TypeRegistry, pub registry: &'a TypeRegistry,
@ -56,6 +87,39 @@ impl<'a> Serialize for ReflectSerializer<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let mut state = serializer.serialize_map(Some(1))?;
state.serialize_entry(
self.value.type_name(),
&TypedReflectSerializer::new(self.value, self.registry),
)?;
state.end()
}
}
/// A serializer for reflected types whose type is known and does not require
/// serialization to include other metadata about it.
pub struct TypedReflectSerializer<'a> {
pub value: &'a dyn Reflect,
pub registry: &'a TypeRegistry,
}
impl<'a> TypedReflectSerializer<'a> {
pub fn new(value: &'a dyn Reflect, registry: &'a TypeRegistry) -> Self {
TypedReflectSerializer { value, registry }
}
}
impl<'a> Serialize for TypedReflectSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
// Handle both Value case and types that have a custom `Serialize`
let serializable = get_serializable::<S::Error>(self.value, self.registry);
if let Ok(serializable) = serializable {
return serializable.borrow().serialize(serializer);
}
match self.value.reflect_ref() { match self.value.reflect_ref() {
ReflectRef::Struct(value) => StructSerializer { ReflectRef::Struct(value) => StructSerializer {
struct_value: value, struct_value: value,
@ -92,11 +156,7 @@ impl<'a> Serialize for ReflectSerializer<'a> {
registry: self.registry, registry: self.registry,
} }
.serialize(serializer), .serialize(serializer),
ReflectRef::Value(value) => ReflectValueSerializer { ReflectRef::Value(_) => Err(serializable.err().unwrap()),
registry: self.registry,
value,
}
.serialize(serializer),
} }
} }
} }
@ -111,13 +171,9 @@ impl<'a> Serialize for ReflectValueSerializer<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let mut state = serializer.serialize_map(Some(2))?; get_serializable::<S::Error>(self.value, self.registry)?
state.serialize_entry(type_fields::TYPE, self.value.type_name())?; .borrow()
state.serialize_entry( .serialize(serializer)
type_fields::VALUE,
get_serializable::<S::Error>(self.value, self.registry)?.borrow(),
)?;
state.end()
} }
} }
@ -131,35 +187,31 @@ impl<'a> Serialize for StructSerializer<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let mut state = serializer.serialize_map(Some(2))?; let type_info = get_type_info(
self.struct_value.get_type_info(),
state.serialize_entry(type_fields::TYPE, self.struct_value.type_name())?; self.struct_value.type_name(),
state.serialize_entry( self.registry,
type_fields::STRUCT,
&StructValueSerializer {
struct_value: self.struct_value,
registry: self.registry,
},
)?; )?;
state.end()
}
}
pub struct StructValueSerializer<'a> { let struct_info = match type_info {
pub struct_value: &'a dyn Struct, TypeInfo::Struct(struct_info) => struct_info,
pub registry: &'a TypeRegistry, info => {
} return Err(Error::custom(format_args!(
"expected struct type but received {:?}",
info
)));
}
};
impl<'a> Serialize for StructValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_map(Some(self.struct_value.field_len()))?;
let serialization_data = self let serialization_data = self
.registry .registry
.get_with_name(self.struct_value.type_name()) .get(self.struct_value.type_id())
.and_then(|registration| registration.data::<SerializationData>()); .and_then(|registration| registration.data::<SerializationData>());
let ignored_len = serialization_data.map(|data| data.len()).unwrap_or(0);
let mut state = serializer.serialize_struct(
struct_info.name(),
self.struct_value.field_len() - ignored_len,
)?;
for (index, value) in self.struct_value.iter_fields().enumerate() { for (index, value) in self.struct_value.iter_fields().enumerate() {
if serialization_data if serialization_data
@ -168,8 +220,8 @@ impl<'a> Serialize for StructValueSerializer<'a> {
{ {
continue; continue;
} }
let key = self.struct_value.name_at(index).unwrap(); let key = struct_info.field_at(index).unwrap().name();
state.serialize_entry(key, &ReflectSerializer::new(value, self.registry))?; state.serialize_field(key, &TypedReflectSerializer::new(value, self.registry))?;
} }
state.end() state.end()
} }
@ -185,35 +237,31 @@ impl<'a> Serialize for TupleStructSerializer<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let mut state = serializer.serialize_map(Some(2))?; let type_info = get_type_info(
self.tuple_struct.get_type_info(),
state.serialize_entry(type_fields::TYPE, self.tuple_struct.type_name())?; self.tuple_struct.type_name(),
state.serialize_entry( self.registry,
type_fields::TUPLE_STRUCT,
&TupleStructValueSerializer {
tuple_struct: self.tuple_struct,
registry: self.registry,
},
)?; )?;
state.end()
}
}
pub struct TupleStructValueSerializer<'a> { let tuple_struct_info = match type_info {
pub tuple_struct: &'a dyn TupleStruct, TypeInfo::TupleStruct(tuple_struct_info) => tuple_struct_info,
pub registry: &'a TypeRegistry, info => {
} return Err(Error::custom(format_args!(
"expected tuple struct type but received {:?}",
info
)));
}
};
impl<'a> Serialize for TupleStructValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_seq(Some(self.tuple_struct.field_len()))?;
let serialization_data = self let serialization_data = self
.registry .registry
.get_with_name(self.tuple_struct.type_name()) .get(self.tuple_struct.type_id())
.and_then(|registration| registration.data::<SerializationData>()); .and_then(|registration| registration.data::<SerializationData>());
let ignored_len = serialization_data.map(|data| data.len()).unwrap_or(0);
let mut state = serializer.serialize_tuple_struct(
tuple_struct_info.name(),
self.tuple_struct.field_len() - ignored_len,
)?;
for (index, value) in self.tuple_struct.iter_fields().enumerate() { for (index, value) in self.tuple_struct.iter_fields().enumerate() {
if serialization_data if serialization_data
@ -222,7 +270,7 @@ impl<'a> Serialize for TupleStructValueSerializer<'a> {
{ {
continue; continue;
} }
state.serialize_element(&ReflectSerializer::new(value, self.registry))?; state.serialize_field(&TypedReflectSerializer::new(value, self.registry))?;
} }
state.end() state.end()
} }
@ -238,104 +286,107 @@ impl<'a> Serialize for EnumSerializer<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let mut state = serializer.serialize_map(Some(2))?; let type_info = get_type_info(
self.enum_value.get_type_info(),
state.serialize_entry(type_fields::TYPE, self.enum_value.type_name())?; self.enum_value.type_name(),
state.serialize_entry( self.registry,
type_fields::ENUM,
&EnumValueSerializer {
enum_value: self.enum_value,
registry: self.registry,
},
)?; )?;
state.end()
}
}
pub struct EnumValueSerializer<'a> { let enum_info = match type_info {
pub enum_value: &'a dyn Enum, TypeInfo::Enum(enum_info) => enum_info,
pub registry: &'a TypeRegistry, info => {
} return Err(Error::custom(format_args!(
"expected enum type but received {:?}",
impl<'a> Serialize for EnumValueSerializer<'a> { info
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> )));
where }
S: serde::Serializer,
{
let variant_type = self.enum_value.variant_type();
let variant_name = self.enum_value.variant_name();
let mut state = if matches!(variant_type, VariantType::Unit) {
serializer.serialize_map(Some(1))?
} else {
serializer.serialize_map(Some(2))?
}; };
state.serialize_entry(type_fields::VARIANT, variant_name)?; let enum_name = enum_info.name();
let variant_index = self.enum_value.variant_index() as u32;
match self.enum_value.variant_type() { let variant_info = enum_info
VariantType::Struct => { .variant_at(variant_index as usize)
state.serialize_key(type_fields::STRUCT)?; .ok_or_else(|| {
state.serialize_value(&StructVariantSerializer { Error::custom(format_args!(
enum_value: self.enum_value, "variant at index `{}` does not exist",
registry: self.registry, variant_index
})?;
}
VariantType::Tuple => {
state.serialize_key(type_fields::TUPLE)?;
state.serialize_value(&TupleVariantSerializer {
enum_value: self.enum_value,
registry: self.registry,
})?;
}
_ => {}
}
state.end()
}
}
pub struct TupleVariantSerializer<'a> {
pub enum_value: &'a dyn Enum,
pub registry: &'a TypeRegistry,
}
impl<'a> Serialize for TupleVariantSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let field_len = self.enum_value.field_len();
let mut state = serializer.serialize_seq(Some(field_len))?;
for field in self.enum_value.iter_fields() {
state.serialize_element(&ReflectSerializer::new(field.value(), self.registry))?;
}
state.end()
}
}
pub struct StructVariantSerializer<'a> {
pub enum_value: &'a dyn Enum,
pub registry: &'a TypeRegistry,
}
impl<'a> Serialize for StructVariantSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let field_len = self.enum_value.field_len();
let mut state = serializer.serialize_map(Some(field_len))?;
for (index, field) in self.enum_value.iter_fields().enumerate() {
let name = field.name().ok_or_else(|| {
S::Error::custom(format_args!(
"struct variant missing name for field at index {}",
index
)) ))
})?; })?;
state.serialize_entry(name, &ReflectSerializer::new(field.value(), self.registry))?; let variant_name = variant_info.name();
let variant_type = self.enum_value.variant_type();
let field_len = self.enum_value.field_len();
match variant_type {
VariantType::Unit => {
if self
.enum_value
.type_name()
.starts_with("core::option::Option")
{
serializer.serialize_none()
} else {
serializer.serialize_unit_variant(enum_name, variant_index, variant_name)
}
}
VariantType::Struct => {
let struct_info = match variant_info {
VariantInfo::Struct(struct_info) => struct_info,
info => {
return Err(Error::custom(format_args!(
"expected struct variant type but received {:?}",
info
)));
}
};
let mut state = serializer.serialize_struct_variant(
enum_name,
variant_index,
variant_name,
field_len,
)?;
for (index, field) in self.enum_value.iter_fields().enumerate() {
let field_info = struct_info.field_at(index).unwrap();
state.serialize_field(
field_info.name(),
&TypedReflectSerializer::new(field.value(), self.registry),
)?;
}
state.end()
}
VariantType::Tuple if field_len == 1 => {
let field = self.enum_value.field_at(0).unwrap();
if self
.enum_value
.type_name()
.starts_with("core::option::Option")
{
serializer.serialize_some(&TypedReflectSerializer::new(field, self.registry))
} else {
serializer.serialize_newtype_variant(
enum_name,
variant_index,
variant_name,
&TypedReflectSerializer::new(field, self.registry),
)
}
}
VariantType::Tuple => {
let mut state = serializer.serialize_tuple_variant(
enum_name,
variant_index,
variant_name,
field_len,
)?;
for field in self.enum_value.iter_fields() {
state.serialize_field(&TypedReflectSerializer::new(
field.value(),
self.registry,
))?;
}
state.end()
}
} }
state.end()
} }
} }
@ -349,34 +400,10 @@ impl<'a> Serialize for TupleSerializer<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let mut state = serializer.serialize_map(Some(2))?; let mut state = serializer.serialize_tuple(self.tuple.field_len())?;
state.serialize_entry(type_fields::TYPE, self.tuple.type_name())?;
state.serialize_entry(
type_fields::TUPLE,
&TupleValueSerializer {
tuple: self.tuple,
registry: self.registry,
},
)?;
state.end()
}
}
pub struct TupleValueSerializer<'a> {
pub tuple: &'a dyn Tuple,
pub registry: &'a TypeRegistry,
}
impl<'a> Serialize for TupleValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_seq(Some(self.tuple.field_len()))?;
for value in self.tuple.iter_fields() { for value in self.tuple.iter_fields() {
state.serialize_element(&ReflectSerializer::new(value, self.registry))?; state.serialize_element(&TypedReflectSerializer::new(value, self.registry))?;
} }
state.end() state.end()
} }
@ -388,30 +415,6 @@ pub struct MapSerializer<'a> {
} }
impl<'a> Serialize for MapSerializer<'a> { impl<'a> Serialize for MapSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_map(Some(2))?;
state.serialize_entry(type_fields::TYPE, self.map.type_name())?;
state.serialize_entry(
type_fields::MAP,
&MapValueSerializer {
map: self.map,
registry: self.registry,
},
)?;
state.end()
}
}
pub struct MapValueSerializer<'a> {
pub map: &'a dyn Map,
pub registry: &'a TypeRegistry,
}
impl<'a> Serialize for MapValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: serde::Serializer, S: serde::Serializer,
@ -419,8 +422,8 @@ impl<'a> Serialize for MapValueSerializer<'a> {
let mut state = serializer.serialize_map(Some(self.map.len()))?; let mut state = serializer.serialize_map(Some(self.map.len()))?;
for (key, value) in self.map.iter() { for (key, value) in self.map.iter() {
state.serialize_entry( state.serialize_entry(
&ReflectSerializer::new(key, self.registry), &TypedReflectSerializer::new(key, self.registry),
&ReflectSerializer::new(value, self.registry), &TypedReflectSerializer::new(value, self.registry),
)?; )?;
} }
state.end() state.end()
@ -433,36 +436,13 @@ pub struct ListSerializer<'a> {
} }
impl<'a> Serialize for ListSerializer<'a> { impl<'a> Serialize for ListSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_map(Some(2))?;
state.serialize_entry(type_fields::TYPE, self.list.type_name())?;
state.serialize_entry(
type_fields::LIST,
&ListValueSerializer {
list: self.list,
registry: self.registry,
},
)?;
state.end()
}
}
pub struct ListValueSerializer<'a> {
pub list: &'a dyn List,
pub registry: &'a TypeRegistry,
}
impl<'a> Serialize for ListValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let mut state = serializer.serialize_seq(Some(self.list.len()))?; let mut state = serializer.serialize_seq(Some(self.list.len()))?;
for value in self.list.iter() { for value in self.list.iter() {
state.serialize_element(&ReflectSerializer::new(value, self.registry))?; state.serialize_element(&TypedReflectSerializer::new(value, self.registry))?;
} }
state.end() state.end()
} }
@ -478,32 +458,9 @@ impl<'a> Serialize for ArraySerializer<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let mut state = serializer.serialize_map(Some(2))?; let mut state = serializer.serialize_tuple(self.array.len())?;
state.serialize_entry(type_fields::TYPE, self.array.type_name())?;
state.serialize_entry(
type_fields::ARRAY,
&ArrayValueSerializer {
array: self.array,
registry: self.registry,
},
)?;
state.end()
}
}
pub struct ArrayValueSerializer<'a> {
pub array: &'a dyn Array,
pub registry: &'a TypeRegistry,
}
impl<'a> Serialize for ArrayValueSerializer<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_seq(Some(self.array.len()))?;
for value in self.array.iter() { for value in self.array.iter() {
state.serialize_element(&ReflectSerializer::new(value, self.registry))?; state.serialize_element(&TypedReflectSerializer::new(value, self.registry))?;
} }
state.end() state.end()
} }
@ -511,21 +468,211 @@ impl<'a> Serialize for ArrayValueSerializer<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::ReflectSerializer;
use crate as bevy_reflect; use crate as bevy_reflect;
use crate::prelude::*; use crate::serde::ReflectSerializer;
use crate::TypeRegistry; use crate::{FromReflect, Reflect, ReflectSerialize, TypeRegistry};
use bevy_utils::HashMap;
use ron::extensions::Extensions;
use ron::ser::PrettyConfig; use ron::ser::PrettyConfig;
use serde::Serialize;
use std::f32::consts::PI;
#[derive(Reflect, Debug, PartialEq)]
struct MyStruct {
primitive_value: i8,
option_value: Option<String>,
option_value_complex: Option<SomeStruct>,
tuple_value: (f32, usize),
list_value: Vec<i32>,
array_value: [i32; 5],
map_value: HashMap<u8, usize>,
struct_value: SomeStruct,
tuple_struct_value: SomeTupleStruct,
unit_enum: SomeEnum,
newtype_enum: SomeEnum,
tuple_enum: SomeEnum,
struct_enum: SomeEnum,
custom_serialize: CustomSerialize,
}
#[derive(Reflect, FromReflect, Debug, PartialEq)]
struct SomeStruct {
foo: i64,
}
#[derive(Reflect, Debug, PartialEq)]
struct SomeTupleStruct(String);
#[derive(Reflect, Debug, PartialEq)]
enum SomeEnum {
Unit,
NewType(usize),
Tuple(f32, f32),
Struct { foo: String },
}
#[derive(Reflect, Debug, PartialEq, Serialize)]
struct SomeSerializableStruct {
foo: i64,
}
/// Implements a custom serialize using `#[reflect(Serialize)]`.
///
/// For testing purposes, this just uses the generated one from deriving Serialize.
#[derive(Reflect, Debug, PartialEq, Serialize)]
#[reflect(Serialize)]
struct CustomSerialize {
value: usize,
#[serde(rename = "renamed")]
inner_struct: SomeSerializableStruct,
}
fn get_registry() -> TypeRegistry { fn get_registry() -> TypeRegistry {
let mut registry = TypeRegistry::default(); let mut registry = TypeRegistry::default();
registry.register::<usize>(); registry.register::<MyStruct>();
registry.register::<f32>(); registry.register::<SomeStruct>();
registry.register::<SomeTupleStruct>();
registry.register::<CustomSerialize>();
registry.register::<SomeSerializableStruct>();
registry.register_type_data::<SomeSerializableStruct, ReflectSerialize>();
registry.register::<String>(); registry.register::<String>();
registry.register::<(f32, f32)>(); registry.register::<Option<String>>();
registry.register_type_data::<Option<String>, ReflectSerialize>();
registry registry
} }
#[test]
fn should_serialize() {
let mut map = HashMap::new();
map.insert(64, 32);
let input = MyStruct {
primitive_value: 123,
option_value: Some(String::from("Hello world!")),
option_value_complex: Some(SomeStruct { foo: 123 }),
tuple_value: (PI, 1337),
list_value: vec![-2, -1, 0, 1, 2],
array_value: [-2, -1, 0, 1, 2],
map_value: map,
struct_value: SomeStruct { foo: 999999999 },
tuple_struct_value: SomeTupleStruct(String::from("Tuple Struct")),
unit_enum: SomeEnum::Unit,
newtype_enum: SomeEnum::NewType(123),
tuple_enum: SomeEnum::Tuple(1.23, 3.21),
struct_enum: SomeEnum::Struct {
foo: String::from("Struct variant value"),
},
custom_serialize: CustomSerialize {
value: 100,
inner_struct: SomeSerializableStruct { foo: 101 },
},
};
let registry = get_registry();
let serializer = ReflectSerializer::new(&input, &registry);
let config = PrettyConfig::default()
.new_line(String::from("\n"))
.indentor(String::from(" "));
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
let expected = r#"{
"bevy_reflect::serde::ser::tests::MyStruct": (
primitive_value: 123,
option_value: Some("Hello world!"),
option_value_complex: Some((
foo: 123,
)),
tuple_value: (3.1415927, 1337),
list_value: [
-2,
-1,
0,
1,
2,
],
array_value: (-2, -1, 0, 1, 2),
map_value: {
64: 32,
},
struct_value: (
foo: 999999999,
),
tuple_struct_value: ("Tuple Struct"),
unit_enum: Unit,
newtype_enum: NewType(123),
tuple_enum: Tuple(1.23, 3.21),
struct_enum: Struct(
foo: "Struct variant value",
),
custom_serialize: (
value: 100,
renamed: (
foo: 101,
),
),
),
}"#;
assert_eq!(expected, output);
}
#[test]
fn should_serialize_option() {
#[derive(Reflect, FromReflect, Debug, PartialEq)]
struct OptionTest {
none: Option<()>,
simple: Option<String>,
complex: Option<SomeStruct>,
}
let value = OptionTest {
none: None,
simple: Some(String::from("Hello world!")),
complex: Some(SomeStruct { foo: 123 }),
};
let registry = get_registry();
let serializer = ReflectSerializer::new(&value, &registry);
// === Normal === //
let config = PrettyConfig::default()
.new_line(String::from("\n"))
.indentor(String::from(" "));
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
let expected = r#"{
"bevy_reflect::serde::ser::tests::should_serialize_option::OptionTest": (
none: None,
simple: Some("Hello world!"),
complex: Some((
foo: 123,
)),
),
}"#;
assert_eq!(expected, output);
// === Implicit Some === //
let config = PrettyConfig::default()
.new_line(String::from("\n"))
.extensions(Extensions::IMPLICIT_SOME)
.indentor(String::from(" "));
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
let expected = r#"#![enable(implicit_some)]
{
"bevy_reflect::serde::ser::tests::should_serialize_option::OptionTest": (
none: None,
simple: "Hello world!",
complex: (
foo: 123,
),
),
}"#;
assert_eq!(expected, output);
}
#[test] #[test]
fn enum_should_serialize() { fn enum_should_serialize() {
#[derive(Reflect)] #[derive(Reflect)]
@ -546,10 +693,7 @@ mod tests {
let serializer = ReflectSerializer::new(&value, &registry); let serializer = ReflectSerializer::new(&value, &registry);
let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap(); let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap();
let expected = r#"{ let expected = r#"{
"type": "bevy_reflect::serde::ser::tests::enum_should_serialize::MyEnum", "bevy_reflect::serde::ser::tests::enum_should_serialize::MyEnum": Unit,
"enum": {
"variant": "Unit",
},
}"#; }"#;
assert_eq!(expected, output); assert_eq!(expected, output);
@ -558,16 +702,7 @@ mod tests {
let serializer = ReflectSerializer::new(&value, &registry); let serializer = ReflectSerializer::new(&value, &registry);
let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap(); let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap();
let expected = r#"{ let expected = r#"{
"type": "bevy_reflect::serde::ser::tests::enum_should_serialize::MyEnum", "bevy_reflect::serde::ser::tests::enum_should_serialize::MyEnum": NewType(123),
"enum": {
"variant": "NewType",
"tuple": [
{
"type": "usize",
"value": 123,
},
],
},
}"#; }"#;
assert_eq!(expected, output); assert_eq!(expected, output);
@ -576,20 +711,7 @@ mod tests {
let serializer = ReflectSerializer::new(&value, &registry); let serializer = ReflectSerializer::new(&value, &registry);
let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap(); let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap();
let expected = r#"{ let expected = r#"{
"type": "bevy_reflect::serde::ser::tests::enum_should_serialize::MyEnum", "bevy_reflect::serde::ser::tests::enum_should_serialize::MyEnum": Tuple(1.23, 3.21),
"enum": {
"variant": "Tuple",
"tuple": [
{
"type": "f32",
"value": 1.23,
},
{
"type": "f32",
"value": 3.21,
},
],
},
}"#; }"#;
assert_eq!(expected, output); assert_eq!(expected, output);
@ -600,17 +722,10 @@ mod tests {
let serializer = ReflectSerializer::new(&value, &registry); let serializer = ReflectSerializer::new(&value, &registry);
let output = ron::ser::to_string_pretty(&serializer, config).unwrap(); let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
let expected = r#"{ let expected = r#"{
"type": "bevy_reflect::serde::ser::tests::enum_should_serialize::MyEnum", "bevy_reflect::serde::ser::tests::enum_should_serialize::MyEnum": Struct(
"enum": { value: "I <3 Enums",
"variant": "Struct", ),
"struct": {
"value": {
"type": "alloc::string::String",
"value": "I <3 Enums",
},
},
},
}"#; }"#;
assert_eq!(expected, output.replace('\r', "")); assert_eq!(expected, output);
} }
} }

View file

@ -31,4 +31,14 @@ impl SerializationData {
pub fn is_ignored_field(&self, index: usize) -> bool { pub fn is_ignored_field(&self, index: usize) -> bool {
self.ignored_field_indices.contains(&index) self.ignored_field_indices.contains(&index)
} }
/// Returns the number of ignored fields.
pub fn len(&self) -> usize {
self.ignored_field_indices.len()
}
/// Returns true if there are no ignored fields.
pub fn is_empty(&self) -> bool {
self.ignored_field_indices.is_empty()
}
} }

View file

@ -69,10 +69,11 @@ pub trait Struct: Reflect {
/// A container for compile-time struct info. /// A container for compile-time struct info.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct StructInfo { pub struct StructInfo {
name: &'static str,
type_name: &'static str, type_name: &'static str,
type_id: TypeId, type_id: TypeId,
fields: Box<[NamedField]>, fields: Box<[NamedField]>,
field_indices: HashMap<Cow<'static, str>, usize>, field_indices: HashMap<&'static str, usize>,
} }
impl StructInfo { impl StructInfo {
@ -80,19 +81,18 @@ impl StructInfo {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `name`: The name of this struct (_without_ generics or lifetimes)
/// * `fields`: The fields of this struct in the order they are defined /// * `fields`: The fields of this struct in the order they are defined
/// ///
pub fn new<T: Reflect>(fields: &[NamedField]) -> Self { pub fn new<T: Reflect>(name: &'static str, fields: &[NamedField]) -> Self {
let field_indices = fields let field_indices = fields
.iter() .iter()
.enumerate() .enumerate()
.map(|(index, field)| { .map(|(index, field)| (field.name(), index))
let name = field.name().clone();
(name, index)
})
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
Self { Self {
name,
type_name: std::any::type_name::<T>(), type_name: std::any::type_name::<T>(),
type_id: TypeId::of::<T>(), type_id: TypeId::of::<T>(),
fields: fields.to_vec().into_boxed_slice(), fields: fields.to_vec().into_boxed_slice(),
@ -127,6 +127,15 @@ impl StructInfo {
self.fields.len() self.fields.len()
} }
/// The name of the struct.
///
/// This does _not_ include any generics or lifetimes.
///
/// For example, `foo::bar::Baz<'a, T>` would simply be `Baz`.
pub fn name(&self) -> &'static str {
self.name
}
/// The [type name] of the struct. /// The [type name] of the struct.
/// ///
/// [type name]: std::any::type_name /// [type name]: std::any::type_name

View file

@ -49,6 +49,7 @@ pub trait TupleStruct: Reflect {
/// A container for compile-time tuple struct info. /// A container for compile-time tuple struct info.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct TupleStructInfo { pub struct TupleStructInfo {
name: &'static str,
type_name: &'static str, type_name: &'static str,
type_id: TypeId, type_id: TypeId,
fields: Box<[UnnamedField]>, fields: Box<[UnnamedField]>,
@ -59,10 +60,12 @@ impl TupleStructInfo {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `name`: The name of this struct (_without_ generics or lifetimes)
/// * `fields`: The fields of this struct in the order they are defined /// * `fields`: The fields of this struct in the order they are defined
/// ///
pub fn new<T: Reflect>(fields: &[UnnamedField]) -> Self { pub fn new<T: Reflect>(name: &'static str, fields: &[UnnamedField]) -> Self {
Self { Self {
name,
type_name: std::any::type_name::<T>(), type_name: std::any::type_name::<T>(),
type_id: TypeId::of::<T>(), type_id: TypeId::of::<T>(),
fields: fields.to_vec().into_boxed_slice(), fields: fields.to_vec().into_boxed_slice(),
@ -84,6 +87,15 @@ impl TupleStructInfo {
self.fields.len() self.fields.len()
} }
/// The name of the struct.
///
/// This does _not_ include any generics or lifetimes.
///
/// For example, `foo::bar::Baz<'a, T>` would simply be `Baz`.
pub fn name(&self) -> &'static str {
self.name
}
/// The [type name] of the tuple struct. /// The [type name] of the tuple struct.
/// ///
/// [type name]: std::any::type_name /// [type name]: std::any::type_name

View file

@ -37,10 +37,10 @@ use std::any::{Any, TypeId};
/// static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new(); /// static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
/// CELL.get_or_set(|| { /// CELL.get_or_set(|| {
/// let fields = [ /// let fields = [
/// NamedField::new::<usize, _>("foo"), /// NamedField::new::<usize >("foo"),
/// NamedField::new::<(f32, f32), _>("bar"), /// NamedField::new::<(f32, f32) >("bar"),
/// ]; /// ];
/// let info = StructInfo::new::<Self>(&fields); /// let info = StructInfo::new::<Self>("MyStruct", &fields);
/// TypeInfo::Struct(info) /// TypeInfo::Struct(info)
/// }) /// })
/// } /// }

View file

@ -27,8 +27,8 @@ use std::any::{Any, TypeId};
/// fn type_info() -> &'static TypeInfo { /// fn type_info() -> &'static TypeInfo {
/// static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new(); /// static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
/// CELL.get_or_set(|| { /// CELL.get_or_set(|| {
/// let fields = [NamedField::new::<i32, _>("bar")]; /// let fields = [NamedField::new::<i32>("bar")];
/// let info = StructInfo::new::<Self>(&fields); /// let info = StructInfo::new::<Self>("Foo", &fields);
/// TypeInfo::Struct(info) /// TypeInfo::Struct(info)
/// }) /// })
/// } /// }
@ -89,7 +89,7 @@ impl NonGenericTypeInfoCell {
/// static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new(); /// static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
/// CELL.get_or_insert::<Self, _>(|| { /// CELL.get_or_insert::<Self, _>(|| {
/// let fields = [UnnamedField::new::<T>(0)]; /// let fields = [UnnamedField::new::<T>(0)];
/// let info = TupleStructInfo::new::<Self>(&fields); /// let info = TupleStructInfo::new::<Self>("Foo", &fields);
/// TypeInfo::TupleStruct(info) /// TypeInfo::TupleStruct(info)
/// }) /// })
/// } /// }

View file

@ -1,7 +1,7 @@
use crate::{DynamicEntity, DynamicScene}; use crate::{DynamicEntity, DynamicScene};
use anyhow::Result; use anyhow::Result;
use bevy_reflect::{ use bevy_reflect::{
serde::{ReflectDeserializer, ReflectSerializer}, serde::{ReflectSerializer, UntypedReflectDeserializer},
Reflect, TypeRegistry, TypeRegistryArc, Reflect, TypeRegistry, TypeRegistryArc,
}; };
use serde::{ use serde::{
@ -242,7 +242,9 @@ impl<'a, 'de> Visitor<'de> for ComponentSeqVisitor<'a> {
A: SeqAccess<'de>, A: SeqAccess<'de>,
{ {
let mut dynamic_properties = Vec::new(); let mut dynamic_properties = Vec::new();
while let Some(entity) = seq.next_element_seed(ReflectDeserializer::new(self.registry))? { while let Some(entity) =
seq.next_element_seed(UntypedReflectDeserializer::new(self.registry))?
{
dynamic_properties.push(entity); dynamic_properties.push(entity);
} }

View file

@ -7,7 +7,7 @@
use bevy::{ use bevy::{
prelude::*, prelude::*,
reflect::{ reflect::{
serde::{ReflectDeserializer, ReflectSerializer}, serde::{ReflectSerializer, UntypedReflectDeserializer},
DynamicStruct, DynamicStruct,
}, },
}; };
@ -81,7 +81,7 @@ fn setup(type_registry: Res<AppTypeRegistry>) {
info!("{}\n", ron_string); info!("{}\n", ron_string);
// Dynamic properties can be deserialized // Dynamic properties can be deserialized
let reflect_deserializer = ReflectDeserializer::new(&type_registry); let reflect_deserializer = UntypedReflectDeserializer::new(&type_registry);
let mut deserializer = ron::de::Deserializer::from_str(&ron_string).unwrap(); let mut deserializer = ron::de::Deserializer::from_str(&ron_string).unwrap();
let reflect_value = reflect_deserializer.deserialize(&mut deserializer).unwrap(); let reflect_value = reflect_deserializer.deserialize(&mut deserializer).unwrap();