diff --git a/crates/bevy_reflect/Cargo.toml b/crates/bevy_reflect/Cargo.toml index a0f092b1d92761..adaa62dd9baad4 100644 --- a/crates/bevy_reflect/Cargo.toml +++ b/crates/bevy_reflect/Cargo.toml @@ -31,4 +31,5 @@ glam = { version = "0.21", features = ["serde"], optional = true } [dev-dependencies] ron = "0.8.0" -rmp-serde = "1.1" \ No newline at end of file +rmp-serde = "1.1" +postcard = {version = "1.0", features = ["alloc"] } \ No newline at end of file diff --git a/crates/bevy_reflect/src/serde/de.rs b/crates/bevy_reflect/src/serde/de.rs index c41225232b64d9..4fd1d001ad0103 100644 --- a/crates/bevy_reflect/src/serde/de.rs +++ b/crates/bevy_reflect/src/serde/de.rs @@ -1325,7 +1325,7 @@ mod tests { } #[test] - fn should_deserialize_binary() { + fn should_deserialize_non_self_describing_binary() { let mut map = HashMap::new(); map.insert(64, 32); @@ -1351,6 +1351,57 @@ mod tests { }, }; + let registry = get_registry(); + + let input = vec![ + 1, 40, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115, 101, 114, + 100, 101, 58, 58, 100, 101, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77, 121, 83, 116, + 114, 117, 99, 116, 123, 1, 12, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, + 1, 246, 1, 219, 15, 73, 64, 185, 10, 5, 3, 1, 0, 2, 4, 3, 1, 0, 2, 4, 1, 64, 32, 254, + 167, 214, 185, 7, 12, 84, 117, 112, 108, 101, 32, 83, 116, 114, 117, 99, 116, 0, 1, + 123, 2, 164, 112, 157, 63, 164, 112, 77, 64, 3, 20, 83, 116, 114, 117, 99, 116, 32, + 118, 97, 114, 105, 97, 110, 116, 32, 118, 97, 108, 117, 101, 100, 202, 1, + ]; + + let deserializer = UntypedReflectDeserializer::new(®istry); + + let dynamic_output = deserializer + .deserialize(&mut postcard::Deserializer::from_bytes(&input)) + .unwrap(); + + let output = ::from_reflect(dynamic_output.as_ref()).unwrap(); + assert_eq!(expected, output); + } + + #[test] + fn should_deserialize_self_describing_binary() { + let mut map = HashMap::new(); + map.insert(64, 32); + + let expected = 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_deserialize: CustomDeserialize { + value: 100, + inner_struct: SomeDeserializableStruct { foo: 101 }, + }, + }; + + let registry = get_registry(); + let input = vec![ 129, 217, 40, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115, 101, 114, 100, 101, 58, 58, 100, 101, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77, 121, @@ -1364,7 +1415,6 @@ mod tests { 101, 146, 100, 145, 101, ]; - let registry = get_registry(); let deserializer = UntypedReflectDeserializer::new(®istry); let dynamic_output = deserializer diff --git a/crates/bevy_reflect/src/serde/ser.rs b/crates/bevy_reflect/src/serde/ser.rs index 76f968b7491a0b..6e3c1519498fe4 100644 --- a/crates/bevy_reflect/src/serde/ser.rs +++ b/crates/bevy_reflect/src/serde/ser.rs @@ -542,59 +542,6 @@ mod tests { registry } - #[test] - fn should_serialize_binary() { - 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 mut registry = get_registry(); - registry.register::(); - registry.register::>(); - registry.register::<(f32, usize)>(); - registry.register::>(); - registry.register::<[i32; 5]>(); - registry.register::>(); - - let serializer = ReflectSerializer::new(&input, ®istry); - let bytes: Vec = rmp_serde::to_vec(&serializer).unwrap(); - - let expected: Vec = vec![ - 129, 217, 41, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115, - 101, 114, 100, 101, 58, 58, 115, 101, 114, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77, - 121, 83, 116, 114, 117, 99, 116, 158, 123, 172, 72, 101, 108, 108, 111, 32, 119, 111, - 114, 108, 100, 33, 145, 123, 146, 202, 64, 73, 15, 219, 205, 5, 57, 149, 254, 255, 0, - 1, 2, 149, 254, 255, 0, 1, 2, 129, 64, 32, 145, 206, 59, 154, 201, 255, 145, 172, 84, - 117, 112, 108, 101, 32, 83, 116, 114, 117, 99, 116, 164, 85, 110, 105, 116, 129, 167, - 78, 101, 119, 84, 121, 112, 101, 123, 129, 165, 84, 117, 112, 108, 101, 146, 202, 63, - 157, 112, 164, 202, 64, 77, 112, 164, 129, 166, 83, 116, 114, 117, 99, 116, 145, 180, - 83, 116, 114, 117, 99, 116, 32, 118, 97, 114, 105, 97, 110, 116, 32, 118, 97, 108, 117, - 101, 146, 100, 145, 101, - ]; - assert_eq!(expected, bytes); - } - #[test] fn should_serialize() { let mut map = HashMap::new(); @@ -782,4 +729,97 @@ mod tests { }"#; assert_eq!(expected, output); } + + #[test] + fn should_serialize_non_self_describing_binary() { + 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, ®istry); + let bytes = postcard::to_allocvec(&serializer).unwrap(); + + let expected: Vec = vec![ + 1, 41, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115, 101, 114, + 100, 101, 58, 58, 115, 101, 114, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77, 121, 83, + 116, 114, 117, 99, 116, 123, 1, 12, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, + 100, 33, 1, 246, 1, 219, 15, 73, 64, 185, 10, 5, 3, 1, 0, 2, 4, 3, 1, 0, 2, 4, 1, 64, + 32, 254, 167, 214, 185, 7, 12, 84, 117, 112, 108, 101, 32, 83, 116, 114, 117, 99, 116, + 0, 1, 123, 2, 164, 112, 157, 63, 164, 112, 77, 64, 3, 20, 83, 116, 114, 117, 99, 116, + 32, 118, 97, 114, 105, 97, 110, 116, 32, 118, 97, 108, 117, 101, 100, 202, 1, + ]; + + assert_eq!(expected, bytes); + } + + #[test] + fn should_serialize_self_describing_binary() { + 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, ®istry); + let bytes: Vec = rmp_serde::to_vec(&serializer).unwrap(); + + let expected: Vec = vec![ + 129, 217, 41, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115, + 101, 114, 100, 101, 58, 58, 115, 101, 114, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77, + 121, 83, 116, 114, 117, 99, 116, 158, 123, 172, 72, 101, 108, 108, 111, 32, 119, 111, + 114, 108, 100, 33, 145, 123, 146, 202, 64, 73, 15, 219, 205, 5, 57, 149, 254, 255, 0, + 1, 2, 149, 254, 255, 0, 1, 2, 129, 64, 32, 145, 206, 59, 154, 201, 255, 145, 172, 84, + 117, 112, 108, 101, 32, 83, 116, 114, 117, 99, 116, 164, 85, 110, 105, 116, 129, 167, + 78, 101, 119, 84, 121, 112, 101, 123, 129, 165, 84, 117, 112, 108, 101, 146, 202, 63, + 157, 112, 164, 202, 64, 77, 112, 164, 129, 166, 83, 116, 114, 117, 99, 116, 145, 180, + 83, 116, 114, 117, 99, 116, 32, 118, 97, 114, 105, 97, 110, 116, 32, 118, 97, 108, 117, + 101, 146, 100, 145, 101, + ]; + + assert_eq!(expected, bytes); + } }