diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 6e94741f638b9..5bcc915ab09e4 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -13,6 +13,7 @@ use std::{ any::Any, borrow::Cow, hash::{Hash, Hasher}, + marker::PhantomData, ops::Range, }; @@ -37,6 +38,7 @@ impl_reflect_value!(Option Deserialize<'de> + Re impl_reflect_value!(HashSet Deserialize<'de> + Send + Sync + 'static>(Serialize, Deserialize)); impl_reflect_value!(Range Deserialize<'de> + Send + Sync + 'static>(Serialize, Deserialize)); impl_reflect_value!(Duration(Debug, Hash, PartialEq, Serialize, Deserialize)); +impl_reflect_value!(PhantomData(Debug, Hash, PartialEq, Serialize, Deserialize)); impl_from_reflect_value!(bool); impl_from_reflect_value!(char); @@ -65,6 +67,7 @@ impl_from_reflect_value!( Range Deserialize<'de> + Send + Sync + 'static> ); impl_from_reflect_value!(Duration); +impl_from_reflect_value!(PhantomData); impl Array for Vec { #[inline] @@ -576,8 +579,13 @@ impl FromReflect for Cow<'static, str> { #[cfg(test)] mod tests { + use super::*; + use crate::serde::{ReflectDeserializer, ReflectSerializer}; + use crate::DynamicStruct; use crate::{Reflect, ReflectSerialize, TypeRegistry}; + use ::serde::de::DeserializeSeed; use bevy_utils::HashMap; + use ron::{ser::to_string, Deserializer}; use std::f32::consts::{PI, TAU}; #[test] @@ -591,6 +599,38 @@ mod tests { let _serializable = reflect_serialize.get_serializable(&std::time::Duration::ZERO); } + #[test] + fn can_serialize_phantomdata() { + #[derive(Reflect, PartialEq, Eq, Debug)] + struct Foo { + a: u32, + _phantomdata: PhantomData, + } + + let foo: Foo = Foo { + a: 1, + _phantomdata: PhantomData, + }; + + let mut registry = TypeRegistry::default(); + registry.register::(); + registry.register::>(); + + let serializer = ReflectSerializer::new(&foo, ®istry); + let serialized = to_string(&serializer).unwrap(); + + let mut deserializer = Deserializer::from_str(&serialized).unwrap(); + let reflect_deserializer = ReflectDeserializer::new(®istry); + let value = reflect_deserializer.deserialize(&mut deserializer).unwrap(); + let dynamic_struct = value.take::().unwrap(); + + assert_eq!( + serialized, + r#"{"type":"bevy_reflect::impls::std::tests::can_serialize_phantomdata::Foo","struct":{"a":{"type":"u32","value":1},"_phantomdata":{"type":"core::marker::PhantomData","value":()}}}"# + ); + assert!(foo.reflect_partial_eq(&dynamic_struct).unwrap()); + } + #[test] fn should_partial_eq_char() { let a: &dyn Reflect = &'x';