bevy/crates
Charles Bournhonesque cbb4c26cad Enable deriving Reflect on structs with generic types (#7364)
# Objective

I recently had an issue, where I have a struct:
```
struct Property {
   inner: T
}
```
that I use as a wrapper for internal purposes.
I don't want to update my struct definition to 
```
struct Property<T: Reflect>{
   inner: T
}
```
because I still want to be able to build `Property<T>` for types `T` that are not `Reflect`. (and also because I don't want to update my whole code base with `<T: Reflect>` bounds)

I still wanted to have reflection on it (for `bevy_inspector_egui`), but adding `derive(Reflect)` fails with the error:
`T cannot be sent between threads safely. T needs to implement Sync.`

I believe that `bevy_reflect` should adopt the model of other derives in the case of generics, which is to add the `Reflect` implementation only if the generics also implement `Reflect`. (That is the behaviour of other macros such as `derive(Clone)` or `derive(Debug)`.

It's also the current behavior of `derive(FromReflect)`.

Basically doing something like:
```
impl<T> Reflect for Foo<T>
where T: Reflect
```


## Solution

- I updated the derive macros for `Structs` and `TupleStructs` to add extra `where` bounds.
   -  Every type that is reflected will need a `T: Reflect` bound
   -  Ignored types will need a `T: 'static + Send + Sync` bound. Here's the reason. For cases like this:
```
#[derive(Reflect)]
struct Foo<T, U>{
   a: T
   #[reflect(ignore)]
   b: U
}
```
I had to add the bound `'static + Send + Sync` to ignored generics like `U`.
The reason is that we want `Foo<T, U>` to be `Reflect: 'static + Send + Sync`, so `Foo<T, U>` must be able to implement those auto-traits. `Foo<T, U>` will only implement those auto-traits if every generic type implements them, including ignored types.
This means that the previously compile-fail case now compiles:
```
#[derive(Reflect)]
struct Foo<'a> {
    #[reflect(ignore)]
    value: &'a str,
}
```
But `Foo<'a>` will only be useable in the cases where `'a: 'static` and panic if we don't have `'a: 'static`, which is what we want (nice bonus from this PR ;) )



---

## Changelog

> This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section.

### Added
Possibility to add `derive(Reflect)` to structs and enums that contain generic types, like so:
```
#[derive(Reflect)]
struct Foo<T>{
   a: T
}
```
Reflection will only be available if the generic type T also implements `Reflect`.
(previously, this would just return a compiler error)
2023-01-28 00:12:06 +00:00
..
bevy_animation Fix minor typos in code and docs (#7378) 2023-01-27 12:12:53 +00:00
bevy_app Remove App::add_sub_app (#7290) 2023-01-24 21:24:25 +00:00
bevy_asset add UnsafeWorldCell abstraction (#6404) 2023-01-27 00:12:13 +00:00
bevy_audio AudioOutput is actually a normal resource now, not a non-send resource (#7262) 2023-01-18 17:20:26 +00:00
bevy_core Fix formatting in Name docs (#7384) 2023-01-27 17:49:11 +00:00
bevy_core_pipeline Cascaded shadow maps. (#7064) 2023-01-25 12:35:39 +00:00
bevy_derive Fix ndk-macro link (#7027) 2022-12-25 05:06:03 +00:00
bevy_diagnostic Rename dynamic feature (#7340) 2023-01-23 14:28:00 +00:00
bevy_dylib Rename dynamic feature (#7340) 2023-01-23 14:28:00 +00:00
bevy_dynamic_plugin Adapt path type of dynamically_load_plugin (#6734) 2022-12-05 23:39:43 +00:00
bevy_ecs Allow returning a value from EntityMut::world_scope (#7385) 2023-01-27 19:42:10 +00:00
bevy_ecs_compile_fail_tests Fix clippy lints and failed test with Rust 1.66 (#6945) 2022-12-15 18:05:15 +00:00
bevy_encase_derive add helper for macro to get either bevy::x or bevy_x depending on how it was imported (#7164) 2023-01-11 21:12:02 +00:00
bevy_gilrs Gamepad events refactor (#6965) 2023-01-09 19:24:52 +00:00
bevy_gltf enum Visibility component (#6320) 2022-12-25 00:39:29 +00:00
bevy_hierarchy Add ReplaceChildren and ClearChildren EntityCommands (#6035) 2023-01-16 21:24:15 +00:00
bevy_input Fix minor typos in code and docs (#7378) 2023-01-27 12:12:53 +00:00
bevy_internal Docs: DefaultPlugins vs. MinimalPlugins and ScheduleRunnerPlugin (#7226) 2023-01-24 05:25:03 +00:00
bevy_log Fix suppression of all console logs when trace_tracy is enabled (#6955) 2022-12-20 23:45:43 +00:00
bevy_macro_utils Update toml_edit to 0.18 (#7370) 2023-01-26 18:09:43 +00:00
bevy_math Improve code/comments for Ray::intersect_plane and its tests (#6823) 2022-12-05 22:49:06 +00:00
bevy_mikktspace Release 0.9.0 (#6568) 2022-11-12 20:01:29 +00:00
bevy_pbr Fix minor typos in code and docs (#7378) 2023-01-27 12:12:53 +00:00
bevy_ptr Fix minor typos in code and docs (#7378) 2023-01-27 12:12:53 +00:00
bevy_reflect Enable deriving Reflect on structs with generic types (#7364) 2023-01-28 00:12:06 +00:00
bevy_reflect_compile_fail_tests Enable deriving Reflect on structs with generic types (#7364) 2023-01-28 00:12:06 +00:00
bevy_render Add main_texture_other (#7343) 2023-01-27 18:41:01 +00:00
bevy_scene Fix beta clippy lints (#7154) 2023-01-11 09:51:22 +00:00
bevy_sprite Changed Msaa to Enum (#7292) 2023-01-20 14:25:21 +00:00
bevy_tasks Fix minor typos in code and docs (#7378) 2023-01-27 12:12:53 +00:00
bevy_text Allow users of Text/TextBundle to choose from glyph_brush_layout's BuiltInLineBreaker options. (#7283) 2023-01-21 00:17:11 +00:00
bevy_time Add bevy_ecs::schedule_v3 module (#6587) 2023-01-17 01:39:17 +00:00
bevy_transform Basic adaptive batching for parallel query iteration (#4777) 2023-01-20 08:47:20 +00:00
bevy_ui Cascaded shadow maps. (#7064) 2023-01-25 12:35:39 +00:00
bevy_utils Fix minor typos in code and docs (#7378) 2023-01-27 12:12:53 +00:00
bevy_window Fix minor typos in code and docs (#7378) 2023-01-27 12:12:53 +00:00
bevy_winit Allow not preventing default event behaviors on wasm (#7304) 2023-01-22 23:35:32 +00:00