Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Reflect for PhantomData #15313

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/bevy_reflect/src/impls/std.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can we add a serialization test for PhantomData in the serde module? Whether or not we're allowing this type to be serialized, we should ensure any type containing it can be deserialized and FromReflect-ed.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
TypeRegistration, TypeRegistry, Typed, ValueInfo,
};
use bevy_reflect_derive::{impl_reflect, impl_reflect_value};

use std::fmt;
use std::{
any::Any,
Expand Down Expand Up @@ -99,6 +100,7 @@ impl_reflect_value!(::std::path::PathBuf(
Deserialize,
Default
));
impl_reflect_value!(::core::marker::PhantomData<T: ?Sized>);
impl_reflect_value!(::std::any::TypeId(Debug, Hash, PartialEq,));
impl_reflect_value!(::std::collections::BTreeSet<T: Ord + Eq + Clone + Send + Sync>());
impl_reflect_value!(::core::ops::Range<T: Clone + Send + Sync>());
Expand Down
82 changes: 82 additions & 0 deletions crates/bevy_reflect/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod tests {
type_registry::TypeRegistry,
DynamicStruct, FromReflect, Reflect,
};
use bevy_reflect_derive::TypePath;
use serde::de::DeserializeSeed;

#[test]
Expand Down Expand Up @@ -183,4 +184,85 @@ mod tests {
.reflect_partial_eq(result.as_partial_reflect())
.unwrap());
}

#[test]
fn should_roundtrip_ref_phantom_data() {
use core::marker::PhantomData;

#[derive(Reflect, ::serde::Serialize, ::serde::Deserialize)]
struct TestStruct<'a, T> {
a: i32,
_marker: PhantomData<&'a T>,
}

#[derive(Reflect)]
struct MarkerType;

let mut registry = TypeRegistry::default();
registry.register::<TestStruct<'_, MarkerType>>();

let value: DynamicStruct = TestStruct::<'static, MarkerType> {
a: 123,
_marker: PhantomData,
}
.clone_dynamic();

let serializer = ReflectSerializer::new(&value, &registry);

let expected = r#"{"bevy_reflect::serde::tests::TestStruct":(a:123)}"#;
let result = ron::ser::to_string(&serializer).unwrap();
assert_eq!(expected, result);

let mut deserializer = ron::de::Deserializer::from_str(&result).unwrap();
let reflect_deserializer = ReflectDeserializer::new(&registry);

let expected = value.clone_value();
let result = reflect_deserializer.deserialize(&mut deserializer).unwrap();

assert!(expected
.reflect_partial_eq(result.as_partial_reflect())
.unwrap());
}

#[test]
fn should_roundtrip_phantom_data() {
use core::marker::PhantomData;

#[derive(Reflect, ::serde::Serialize, ::serde::Deserialize)]
struct TestStruct<T> {
a: i32,
_marker: PhantomData<T>,
}

#[derive(Reflect)]
struct MarkerType;

let mut registry = TypeRegistry::default();
registry.register::<TestStruct<MarkerType>>();
registry.register::<PhantomData<MarkerType>>();
registry.register_type_data::<PhantomData<MarkerType>, crate::ReflectDeserialize>();
registry.register_type_data::<PhantomData<MarkerType>, crate::ReflectDeserialize>();

let value: DynamicStruct = TestStruct::<MarkerType> {
a: 123,
_marker: PhantomData,
}
.clone_dynamic();

let serializer = ReflectSerializer::new(&value, &registry);

let expected = r#"{"bevy_reflect::serde::tests::TestStruct":(a:123)}"#;
let result = ron::ser::to_string(&serializer).unwrap();
assert_eq!(expected, result);

let mut deserializer = ron::de::Deserializer::from_str(&result).unwrap();
let reflect_deserializer = ReflectDeserializer::new(&registry);

let expected = value.clone_value();
let result = reflect_deserializer.deserialize(&mut deserializer).unwrap();

assert!(expected
.reflect_partial_eq(result.as_partial_reflect())
.unwrap());
}
}
Loading