From 4c194084b48808f2a564bbdb6eda44cccbb8c3fe Mon Sep 17 00:00:00 2001 From: MrGVSV Date: Mon, 2 May 2022 18:04:48 +0000 Subject: [PATCH] bevy_reflect: Add `GetTypeRegistration` impl for reflected tuples (#4226) # Objective Reflected tuples do not implement `GetTypeRegistration`, preventing us from registering our tuples, like: ```rust app.register_type::<(i32, i32)>(); ``` This is especially important for things like using #4042 to improve the scene format or implementing #4154 to recursively register fields. ## Solution Added an implementation to the tuple macro: ```rust impl<$($name: Reflect + for<'de> Deserialize<'de>),*> GetTypeRegistration for ($($name,)*) { fn get_type_registration() -> TypeRegistration { let mut registration = TypeRegistration::of::<($($name,)*)>(); registration.insert::(FromType::<($($name,)*)>::from_type()); registration } } ``` This requires that the tuple's types implement `Deserialize`. This is exactly how `Vec` and `HashMap` handle it: ```rust impl Deserialize<'de>> GetTypeRegistration for Vec { fn get_type_registration() -> TypeRegistration { let mut registration = TypeRegistration::of::>(); registration.insert::(FromType::>::from_type()); registration } } ``` --- crates/bevy_reflect/src/tuple.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/bevy_reflect/src/tuple.rs b/crates/bevy_reflect/src/tuple.rs index 05d8322f54784..e62fc2bc8c8ac 100644 --- a/crates/bevy_reflect/src/tuple.rs +++ b/crates/bevy_reflect/src/tuple.rs @@ -1,7 +1,10 @@ +use crate::{ + serde::Serializable, FromReflect, FromType, GetTypeRegistration, Reflect, ReflectDeserialize, + ReflectMut, ReflectRef, TypeRegistration, +}; +use serde::Deserialize; use std::any::Any; -use crate::{serde::Serializable, FromReflect, Reflect, ReflectMut, ReflectRef}; - /// A reflected Rust tuple. /// /// This trait is automatically implemented for arbitrary tuples of up to 12 @@ -418,6 +421,14 @@ macro_rules! impl_reflect_tuple { } } + impl<$($name: Reflect + for<'de> Deserialize<'de>),*> GetTypeRegistration for ($($name,)*) { + fn get_type_registration() -> TypeRegistration { + let mut registration = TypeRegistration::of::<($($name,)*)>(); + registration.insert::(FromType::<($($name,)*)>::from_type()); + registration + } + } + impl<$($name: FromReflect),*> FromReflect for ($($name,)*) { fn from_reflect(reflect: &dyn Reflect) -> Option {