Skip to content
Merged
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: 1 addition & 1 deletion crates/bevy_reflect/derive/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub(crate) fn impl_get_type_registration<'a>(
registration.insert::<#bevy_reflect_path::ReflectFromPtr>(#bevy_reflect_path::FromType::<Self>::from_type());
#from_reflect_data
#serialization_data
#(registration.insert::<#registration_data>(#bevy_reflect_path::FromType::<Self>::from_type());)*
#(registration.register_type_data::<#registration_data, Self>();)*
registration
}

Expand Down
30 changes: 30 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3606,6 +3606,36 @@ bevy_reflect::tests::Test {
assert!(registry.contains(TypeId::of::<OpaqueStructReflect>()));
assert!(registry.contains(TypeId::of::<ZSTOpaqueStructReflect>()));
}

#[test]
fn type_data_dependency() {
#[derive(Reflect)]
#[reflect(A)]
struct X;

#[derive(Clone)]
struct ReflectA;

impl<T> FromType<T> for ReflectA {
fn from_type() -> Self {
ReflectA
}

fn insert_dependencies(type_registration: &mut TypeRegistration) {
type_registration.insert(ReflectB);
}
}

#[derive(Clone)]
struct ReflectB;

let mut registry = TypeRegistry::new();
registry.register::<X>();

let registration = registry.get(TypeId::of::<X>()).unwrap();
assert!(registration.data::<ReflectA>().is_some());
assert!(registration.data::<ReflectB>().is_some());
}
}

#[cfg(feature = "glam")]
Expand Down
11 changes: 11 additions & 0 deletions crates/bevy_reflect/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,13 @@ impl TypeRegistration {
self.data.insert(TypeId::of::<T>(), Box::new(data));
}

/// Inserts the [`TypeData`] instance of `T` created for `V`, and inserts any
/// [`TypeData`] dependencies for that combination of `T` and `V`.
pub fn register_type_data<T: TypeData + FromType<V>, V>(&mut self) {
self.insert(T::from_type());
T::insert_dependencies(self);
}

/// Returns a reference to the value of type `T` in this registration's
/// [type data].
///
Expand Down Expand Up @@ -747,6 +754,10 @@ where
pub trait FromType<T> {
/// Creates an instance of `Self` for type `T`.
fn from_type() -> Self;
/// Inserts [`TypeData`] dependencies of this [`TypeData`].
/// This is especially useful for trait [`TypeData`] that has a supertrait (ex: `A: B`).
/// When the [`TypeData`] for `A` is inserted, the `B` [`TypeData`] will also be inserted.
fn insert_dependencies(_type_registration: &mut TypeRegistration) {}
}

/// A struct used to serialize reflected instances of a type.
Expand Down