Skip to content

Commit 2c45c3c

Browse files
committed
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::<ReflectDeserialize>(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<T: FromReflect + for<'de> Deserialize<'de>> GetTypeRegistration for Vec<T> { fn get_type_registration() -> TypeRegistration { let mut registration = TypeRegistration::of::<Vec<T>>(); registration.insert::<ReflectDeserialize>(FromType::<Vec<T>>::from_type()); registration } } ```
1 parent 04c671a commit 2c45c3c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

crates/bevy_reflect/src/tuple.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use crate::{
2+
serde::Serializable, FromReflect, FromType, GetTypeRegistration, Reflect, ReflectDeserialize,
3+
ReflectMut, ReflectRef, TypeRegistration,
4+
};
5+
use serde::Deserialize;
16
use std::any::Any;
27

3-
use crate::{serde::Serializable, FromReflect, Reflect, ReflectMut, ReflectRef};
4-
58
/// A reflected Rust tuple.
69
///
710
/// This trait is automatically implemented for arbitrary tuples of up to 12
@@ -418,6 +421,14 @@ macro_rules! impl_reflect_tuple {
418421
}
419422
}
420423

424+
impl<$($name: Reflect + for<'de> Deserialize<'de>),*> GetTypeRegistration for ($($name,)*) {
425+
fn get_type_registration() -> TypeRegistration {
426+
let mut registration = TypeRegistration::of::<($($name,)*)>();
427+
registration.insert::<ReflectDeserialize>(FromType::<($($name,)*)>::from_type());
428+
registration
429+
}
430+
}
431+
421432
impl<$($name: FromReflect),*> FromReflect for ($($name,)*)
422433
{
423434
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {

0 commit comments

Comments
 (0)