 d30d3e752a
			
		
	
	
		d30d3e752a
		
	
	
	
	
		
			
			> 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.
		
			
				
	
	
		
			420 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			420 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use crate::utility::NonGenericTypeInfoCell;
 | |
| use crate::{DynamicInfo, Reflect, ReflectMut, ReflectRef, TypeInfo, Typed, UnnamedField};
 | |
| use std::any::{Any, TypeId};
 | |
| use std::fmt::{Debug, Formatter};
 | |
| use std::slice::Iter;
 | |
| 
 | |
| /// A reflected Rust tuple struct.
 | |
| ///
 | |
| /// Implementors of this trait allow their tuple fields to be addressed by
 | |
| /// index.
 | |
| ///
 | |
| /// This trait is automatically implemented for tuple struct types when using
 | |
| /// `#[derive(Reflect)]`.
 | |
| ///
 | |
| /// ```
 | |
| /// use bevy_reflect::{Reflect, TupleStruct};
 | |
| ///
 | |
| /// #[derive(Reflect)]
 | |
| /// struct Foo(String);
 | |
| ///
 | |
| /// # fn main() {
 | |
| /// let foo = Foo("Hello, world!".to_string());
 | |
| ///
 | |
| /// assert_eq!(foo.field_len(), 1);
 | |
| ///
 | |
| /// let first = foo.field(0).unwrap();
 | |
| /// assert_eq!(first.downcast_ref::<String>(), Some(&"Hello, world!".to_string()));
 | |
| /// # }
 | |
| /// ```
 | |
| pub trait TupleStruct: Reflect {
 | |
|     /// Returns a reference to the value of the field with index `index` as a
 | |
|     /// `&dyn Reflect`.
 | |
|     fn field(&self, index: usize) -> Option<&dyn Reflect>;
 | |
| 
 | |
|     /// Returns a mutable reference to the value of the field with index `index`
 | |
|     /// as a `&mut dyn Reflect`.
 | |
|     fn field_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>;
 | |
| 
 | |
|     /// Returns the number of fields in the tuple struct.
 | |
|     fn field_len(&self) -> usize;
 | |
| 
 | |
|     /// Returns an iterator over the values of the tuple struct's fields.
 | |
|     fn iter_fields(&self) -> TupleStructFieldIter;
 | |
| 
 | |
|     /// Clones the struct into a [`DynamicTupleStruct`].
 | |
|     fn clone_dynamic(&self) -> DynamicTupleStruct;
 | |
| }
 | |
| 
 | |
| /// A container for compile-time tuple struct info.
 | |
| #[derive(Clone, Debug)]
 | |
| pub struct TupleStructInfo {
 | |
|     name: &'static str,
 | |
|     type_name: &'static str,
 | |
|     type_id: TypeId,
 | |
|     fields: Box<[UnnamedField]>,
 | |
| }
 | |
| 
 | |
| impl TupleStructInfo {
 | |
|     /// Create a new [`TupleStructInfo`].
 | |
|     ///
 | |
|     /// # Arguments
 | |
|     ///
 | |
|     /// * `name`: The name of this struct (_without_ generics or lifetimes)
 | |
|     /// * `fields`: The fields of this struct in the order they are defined
 | |
|     ///
 | |
|     pub fn new<T: Reflect>(name: &'static str, fields: &[UnnamedField]) -> Self {
 | |
|         Self {
 | |
|             name,
 | |
|             type_name: std::any::type_name::<T>(),
 | |
|             type_id: TypeId::of::<T>(),
 | |
|             fields: fields.to_vec().into_boxed_slice(),
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /// Get the field at the given index.
 | |
|     pub fn field_at(&self, index: usize) -> Option<&UnnamedField> {
 | |
|         self.fields.get(index)
 | |
|     }
 | |
| 
 | |
|     /// Iterate over the fields of this struct.
 | |
|     pub fn iter(&self) -> Iter<'_, UnnamedField> {
 | |
|         self.fields.iter()
 | |
|     }
 | |
| 
 | |
|     /// The total number of fields in this struct.
 | |
|     pub fn field_len(&self) -> usize {
 | |
|         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.
 | |
|     ///
 | |
|     /// [type name]: std::any::type_name
 | |
|     pub fn type_name(&self) -> &'static str {
 | |
|         self.type_name
 | |
|     }
 | |
| 
 | |
|     /// The [`TypeId`] of the tuple struct.
 | |
|     pub fn type_id(&self) -> TypeId {
 | |
|         self.type_id
 | |
|     }
 | |
| 
 | |
|     /// Check if the given type matches the tuple struct type.
 | |
|     pub fn is<T: Any>(&self) -> bool {
 | |
|         TypeId::of::<T>() == self.type_id
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// An iterator over the field values of a tuple struct.
 | |
| pub struct TupleStructFieldIter<'a> {
 | |
|     pub(crate) tuple_struct: &'a dyn TupleStruct,
 | |
|     pub(crate) index: usize,
 | |
| }
 | |
| 
 | |
| impl<'a> TupleStructFieldIter<'a> {
 | |
|     pub fn new(value: &'a dyn TupleStruct) -> Self {
 | |
|         TupleStructFieldIter {
 | |
|             tuple_struct: value,
 | |
|             index: 0,
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl<'a> Iterator for TupleStructFieldIter<'a> {
 | |
|     type Item = &'a dyn Reflect;
 | |
| 
 | |
|     fn next(&mut self) -> Option<Self::Item> {
 | |
|         let value = self.tuple_struct.field(self.index);
 | |
|         self.index += 1;
 | |
|         value
 | |
|     }
 | |
| 
 | |
|     fn size_hint(&self) -> (usize, Option<usize>) {
 | |
|         let size = self.tuple_struct.field_len();
 | |
|         (size, Some(size))
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl<'a> ExactSizeIterator for TupleStructFieldIter<'a> {}
 | |
| 
 | |
| /// A convenience trait which combines fetching and downcasting of tuple
 | |
| /// struct fields.
 | |
| ///
 | |
| /// # Example
 | |
| ///
 | |
| /// ```
 | |
| /// use bevy_reflect::{GetTupleStructField, Reflect};
 | |
| ///
 | |
| /// #[derive(Reflect)]
 | |
| /// struct Foo(String);
 | |
| ///
 | |
| /// # fn main() {
 | |
| /// let mut foo = Foo("Hello, world!".to_string());
 | |
| ///
 | |
| /// foo.get_field_mut::<String>(0).unwrap().truncate(5);
 | |
| /// assert_eq!(foo.get_field::<String>(0), Some(&"Hello".to_string()));
 | |
| /// # }
 | |
| /// ```
 | |
| pub trait GetTupleStructField {
 | |
|     /// Returns a reference to the value of the field with index `index`,
 | |
|     /// downcast to `T`.
 | |
|     fn get_field<T: Reflect>(&self, index: usize) -> Option<&T>;
 | |
| 
 | |
|     /// Returns a mutable reference to the value of the field with index
 | |
|     /// `index`, downcast to `T`.
 | |
|     fn get_field_mut<T: Reflect>(&mut self, index: usize) -> Option<&mut T>;
 | |
| }
 | |
| 
 | |
| impl<S: TupleStruct> GetTupleStructField for S {
 | |
|     fn get_field<T: Reflect>(&self, index: usize) -> Option<&T> {
 | |
|         self.field(index)
 | |
|             .and_then(|value| value.downcast_ref::<T>())
 | |
|     }
 | |
| 
 | |
|     fn get_field_mut<T: Reflect>(&mut self, index: usize) -> Option<&mut T> {
 | |
|         self.field_mut(index)
 | |
|             .and_then(|value| value.downcast_mut::<T>())
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl GetTupleStructField for dyn TupleStruct {
 | |
|     fn get_field<T: Reflect>(&self, index: usize) -> Option<&T> {
 | |
|         self.field(index)
 | |
|             .and_then(|value| value.downcast_ref::<T>())
 | |
|     }
 | |
| 
 | |
|     fn get_field_mut<T: Reflect>(&mut self, index: usize) -> Option<&mut T> {
 | |
|         self.field_mut(index)
 | |
|             .and_then(|value| value.downcast_mut::<T>())
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// A tuple struct which allows fields to be added at runtime.
 | |
| #[derive(Default)]
 | |
| pub struct DynamicTupleStruct {
 | |
|     name: String,
 | |
|     fields: Vec<Box<dyn Reflect>>,
 | |
| }
 | |
| 
 | |
| impl DynamicTupleStruct {
 | |
|     /// Returns the type name of the tuple struct.
 | |
|     pub fn name(&self) -> &str {
 | |
|         &self.name
 | |
|     }
 | |
| 
 | |
|     /// Sets the type name of the tuple struct.
 | |
|     pub fn set_name(&mut self, name: String) {
 | |
|         self.name = name;
 | |
|     }
 | |
| 
 | |
|     /// Appends an element with value `value` to the tuple struct.
 | |
|     pub fn insert_boxed(&mut self, value: Box<dyn Reflect>) {
 | |
|         self.fields.push(value);
 | |
|     }
 | |
| 
 | |
|     /// Appends a typed element with value `value` to the tuple struct.
 | |
|     pub fn insert<T: Reflect>(&mut self, value: T) {
 | |
|         self.insert_boxed(Box::new(value));
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl TupleStruct for DynamicTupleStruct {
 | |
|     #[inline]
 | |
|     fn field(&self, index: usize) -> Option<&dyn Reflect> {
 | |
|         self.fields.get(index).map(|field| &**field)
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn field_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> {
 | |
|         self.fields.get_mut(index).map(|field| &mut **field)
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn field_len(&self) -> usize {
 | |
|         self.fields.len()
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn iter_fields(&self) -> TupleStructFieldIter {
 | |
|         TupleStructFieldIter {
 | |
|             tuple_struct: self,
 | |
|             index: 0,
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     fn clone_dynamic(&self) -> DynamicTupleStruct {
 | |
|         DynamicTupleStruct {
 | |
|             name: self.name.clone(),
 | |
|             fields: self
 | |
|                 .fields
 | |
|                 .iter()
 | |
|                 .map(|value| value.clone_value())
 | |
|                 .collect(),
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl Reflect for DynamicTupleStruct {
 | |
|     #[inline]
 | |
|     fn type_name(&self) -> &str {
 | |
|         self.name.as_str()
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn get_type_info(&self) -> &'static TypeInfo {
 | |
|         <Self as Typed>::type_info()
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn into_any(self: Box<Self>) -> Box<dyn Any> {
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn as_any(&self) -> &dyn Any {
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn as_any_mut(&mut self) -> &mut dyn Any {
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn as_reflect(&self) -> &dyn Reflect {
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
 | |
|         self
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn clone_value(&self) -> Box<dyn Reflect> {
 | |
|         Box::new(self.clone_dynamic())
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn reflect_ref(&self) -> ReflectRef {
 | |
|         ReflectRef::TupleStruct(self)
 | |
|     }
 | |
| 
 | |
|     #[inline]
 | |
|     fn reflect_mut(&mut self) -> ReflectMut {
 | |
|         ReflectMut::TupleStruct(self)
 | |
|     }
 | |
| 
 | |
|     fn apply(&mut self, value: &dyn Reflect) {
 | |
|         if let ReflectRef::TupleStruct(tuple_struct) = value.reflect_ref() {
 | |
|             for (i, value) in tuple_struct.iter_fields().enumerate() {
 | |
|                 if let Some(v) = self.field_mut(i) {
 | |
|                     v.apply(value);
 | |
|                 }
 | |
|             }
 | |
|         } else {
 | |
|             panic!("Attempted to apply non-TupleStruct type to TupleStruct type.");
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
 | |
|         *self = value.take()?;
 | |
|         Ok(())
 | |
|     }
 | |
| 
 | |
|     fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
 | |
|         tuple_struct_partial_eq(self, value)
 | |
|     }
 | |
| 
 | |
|     fn debug(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 | |
|         write!(f, "DynamicTupleStruct(")?;
 | |
|         tuple_struct_debug(self, f)?;
 | |
|         write!(f, ")")
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl Debug for DynamicTupleStruct {
 | |
|     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 | |
|         self.debug(f)
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl Typed for DynamicTupleStruct {
 | |
|     fn type_info() -> &'static TypeInfo {
 | |
|         static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
 | |
|         CELL.get_or_set(|| TypeInfo::Dynamic(DynamicInfo::new::<Self>()))
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// Compares a [`TupleStruct`] with a [`Reflect`] value.
 | |
| ///
 | |
| /// Returns true if and only if all of the following are true:
 | |
| /// - `b` is a tuple struct;
 | |
| /// - `b` has the same number of fields as `a`;
 | |
| /// - [`Reflect::reflect_partial_eq`] returns `Some(true)` for pairwise fields of `a` and `b`.
 | |
| ///
 | |
| /// Returns [`None`] if the comparison couldn't even be performed.
 | |
| #[inline]
 | |
| pub fn tuple_struct_partial_eq<S: TupleStruct>(a: &S, b: &dyn Reflect) -> Option<bool> {
 | |
|     let tuple_struct = if let ReflectRef::TupleStruct(tuple_struct) = b.reflect_ref() {
 | |
|         tuple_struct
 | |
|     } else {
 | |
|         return Some(false);
 | |
|     };
 | |
| 
 | |
|     if a.field_len() != tuple_struct.field_len() {
 | |
|         return Some(false);
 | |
|     }
 | |
| 
 | |
|     for (i, value) in tuple_struct.iter_fields().enumerate() {
 | |
|         if let Some(field_value) = a.field(i) {
 | |
|             let eq_result = field_value.reflect_partial_eq(value);
 | |
|             if let failed @ (Some(false) | None) = eq_result {
 | |
|                 return failed;
 | |
|             }
 | |
|         } else {
 | |
|             return Some(false);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     Some(true)
 | |
| }
 | |
| 
 | |
| /// The default debug formatter for [`TupleStruct`] types.
 | |
| ///
 | |
| /// # Example
 | |
| /// ```
 | |
| /// use bevy_reflect::Reflect;
 | |
| /// #[derive(Reflect)]
 | |
| /// struct MyTupleStruct(usize);
 | |
| ///
 | |
| /// let my_tuple_struct: &dyn Reflect = &MyTupleStruct(123);
 | |
| /// println!("{:#?}", my_tuple_struct);
 | |
| ///
 | |
| /// // Output:
 | |
| ///
 | |
| /// // MyTupleStruct (
 | |
| /// //   123,
 | |
| /// // )
 | |
| /// ```
 | |
| #[inline]
 | |
| pub fn tuple_struct_debug(
 | |
|     dyn_tuple_struct: &dyn TupleStruct,
 | |
|     f: &mut std::fmt::Formatter<'_>,
 | |
| ) -> std::fmt::Result {
 | |
|     let mut debug = f.debug_tuple(dyn_tuple_struct.type_name());
 | |
|     for field in dyn_tuple_struct.iter_fields() {
 | |
|         debug.field(&field as &dyn Debug);
 | |
|     }
 | |
|     debug.finish()
 | |
| }
 |