bevy/crates
MrGVSV 3d8d922566 bevy_reflect_derive: Tidying up the code (#4712)
# Objective

The `bevy_reflect_derive` crate is not the cleanest or easiest to follow/maintain. The `lib.rs` file is especially difficult with over 1000 lines of code written in a confusing order. This is just a result of growth within the crate and it would be nice to clean it up for future work.

## Solution

Split `bevy_reflect_derive` into many more submodules. The submodules include:

* `container_attributes` - Code relating to container attributes
* `derive_data` - Code relating to reflection-based derive metadata
* `field_attributes` - Code relating to field attributes
* `impls` - Code containing actual reflection implementations
* `reflect_value` - Code relating to reflection-based value metadata
* `registration` - Code relating to type registration
* `utility` - General-purpose utility functions

This leaves the `lib.rs` file to contain only the public macros, making it much easier to digest (and fewer than 200 lines).

By breaking up the code into smaller modules, we make it easier for future contributors to find the code they're looking for or identify which module best fits their own additions.

### Metadata Structs

This cleanup also adds two big metadata structs: `ReflectFieldAttr` and `ReflectDeriveData`. The former is used to store all attributes for a struct field (if any). The latter is used to store all metadata for struct-based derive inputs.

Both significantly reduce code duplication and make editing these macros much simpler. The tradeoff is that we may collect more metadata than needed. However, this is usually a small thing (such as checking for attributes when they're not really needed or creating a `ReflectFieldAttr` for every field regardless of whether they actually have an attribute).

We could try to remove these tradeoffs and squeeze some more performance out, but doing so might come at the cost of developer experience. Personally, I think it's much nicer to create a `ReflectFieldAttr` for every field since it means I don't have to do two `Option` checks. Others may disagree, though, and so we can discuss changing this either in this PR or in a future one.

### Out of Scope

_Some_ documentation has been added or improved, but ultimately good docs are probably best saved for a dedicated PR.

## 🔍 Focus Points (for reviewers)

I know it's a lot to sift through, so here is a list of **key points for reviewers**:

- The following files contain code that was mostly just relocated:
  - `reflect_value.rs`
  - `registration.rs`
- `container_attributes.rs` was also mostly moved but features some general cleanup (reducing nesting, removing hardcoded strings, etc.) and lots of doc comments
- Most impl logic was moved from `lib.rs` to `impls.rs`, but they have been significantly modified to use the new `ReflectDeriveData` metadata struct in order to reduce duplication.
- `derive_data.rs` and `field_attributes.rs` contain almost entirely new code and should probably be given the most attention.
- Likewise, `from_reflect.rs` saw major changes using `ReflectDeriveData` so it should also be given focus.
- There was no change to the `lib.rs` exports so the end-user API should be the same.

## Prior Work

This task was initially tackled by @NathanSWard in #2377 (which was closed in favor of this PR), so hats off to them for beating me to the punch by nearly a year!

---

## Changelog

* **[INTERNAL]** Split `bevy_reflect_derive` into smaller submodules
* **[INTERNAL]** Add `ReflectFieldAttr`
* **[INTERNAL]** Add `ReflectDeriveData`
* Add `BevyManifest::get_path_direct()` method (`bevy_macro_utils`)


Co-authored-by: MrGVSV <49806985+MrGVSV@users.noreply.github.com>
2022-05-12 19:43:23 +00:00
..
bevy_animation Make AnimationClip::duration return value instead of reference (#4617) 2022-04-27 23:44:06 +00:00
bevy_app exact sized event iterators (#3863) 2022-05-09 13:19:32 +00:00
bevy_asset Make public macros more robust with $crate (#4655) 2022-05-06 19:29:45 +00:00
bevy_audio Add missing audio/ogg file extensions: .oga, .spx (#4703) 2022-05-09 13:37:40 +00:00
bevy_core add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_core_pipeline Do not create nor execute render passes which have no phase items to draw (#4643) 2022-05-02 20:22:30 +00:00
bevy_crevice Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_derive Decouple some dependencies (#3886) 2022-04-27 19:08:11 +00:00
bevy_diagnostic Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_dylib Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_dynamic_plugin Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_ecs Tidy up the code of events (#4713) 2022-05-10 20:18:59 +00:00
bevy_ecs_compile_fail_tests Make derived SystemParam readonly if possible (#4650) 2022-05-09 16:09:33 +00:00
bevy_gilrs Change gamepad.rs tuples to normal structs (#4519) 2022-05-02 13:20:55 +00:00
bevy_gltf fix "unused" warnings when compiling with render feature but without animation (#4714) 2022-05-10 20:01:55 +00:00
bevy_hierarchy Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_input Allow closing windows at runtime (#3575) 2022-05-05 13:35:43 +00:00
bevy_internal bevy_ptr standalone crate (#4653) 2022-05-04 19:16:10 +00:00
bevy_log can specify chrome tracing file (#4618) 2022-05-03 15:35:04 +00:00
bevy_macro_utils bevy_reflect_derive: Tidying up the code (#4712) 2022-05-12 19:43:23 +00:00
bevy_math Document bevy_math (#4591) 2022-04-26 18:23:29 +00:00
bevy_pbr use const Vec2 in lights cluster and bounding box when possible (#4602) 2022-05-06 22:05:45 +00:00
bevy_ptr some cleanup for bevy_ptr (#4668) 2022-05-06 19:15:24 +00:00
bevy_reflect bevy_reflect_derive: Tidying up the code (#4712) 2022-05-12 19:43:23 +00:00
bevy_render fix tracy frame marker placement (after preseting *all* windows) (#4731) 2022-05-12 18:39:20 +00:00
bevy_scene Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_sprite Add support for vertex colors (#4528) 2022-05-05 00:46:32 +00:00
bevy_tasks Remove unused CountdownEvent (#4290) 2022-04-26 21:20:12 +00:00
bevy_text Update layout/style when scale factor changes too (#4689) 2022-05-09 14:18:02 +00:00
bevy_transform add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_ui add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_utils Make public macros more robust with $crate (#4655) 2022-05-06 19:29:45 +00:00
bevy_window Allow closing windows at runtime (#3575) 2022-05-05 13:35:43 +00:00
bevy_winit Allow closing windows at runtime (#3575) 2022-05-05 13:35:43 +00:00