Skip to content

Commit

Permalink
bevy_reflect: Add GetTypeRegistration impl for reflected tuples (be…
Browse files Browse the repository at this point in the history
…vyengine#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 bevyengine#4042 to improve the scene format or implementing bevyengine#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
  }
}
```
  • Loading branch information
MrGVSV authored and ItsDoot committed Feb 1, 2023
1 parent 51f2f95 commit 198f1b1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions crates/bevy_reflect/src/tuple.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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::<ReflectDeserialize>(FromType::<($($name,)*)>::from_type());
registration
}
}

impl<$($name: FromReflect),*> FromReflect for ($($name,)*)
{
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
Expand Down

0 comments on commit 198f1b1

Please sign in to comment.