Commit 41f59f1
authored
TypeData dependencies (#22016)
# Objective
The proposed ["Resources as
entities"](#20934 ) PR makes
Resource implement Component. This creates a situation where we need to
do:
```rust
#[derive(Resource, Reflect)]
#[reflect(Resource, Component)]
struct Thing;
```
I'm of the mind that we should add the ability for TypeData to have
"dependencies". Ex `reflect(Resource)` implies `reflect(Component)`. For
"subtrait" types, I think supporting this is logical / users would
appreciate it.
## Solution
1. Add a new `FromType<T>::insert_dependencies` function with a default
impl (which makes this a non-breaking change). This does kind of
overload the `FromType` trait (ex: a name like `GetTypeData` might be
better with this new context), but this is a pretty niche trait / piece
of functionality, and I like the idea of not breaking people.
2. Add a new `TypeRegistration::register_type_data<T, V>` function,
which initializes the TypeData `T` for a given type `V` , inserts that
type data, and also inserts any dependent type data using
`insert_dependencies`.
3. Adjust the `Reflect` macro to use `register_type_data` instead of
`insert(FromType::<Self>::from_type())`
This makes it possible to do the following:
```rust
impl<R: Resource + FromReflect + TypePath> FromType<R> for ReflectResource {
fn from_type() -> Self {
ReflectResource
}
fn insert_dependencies(type_registration: &mut TypeRegistration) {
type_registration.register_type_data::<ReflectComponent, R>();
}
}
```
Which then allows dropping `reflect(Component)`:
```rust
#[derive(Resource, Reflect)]
#[reflect(Resource)]
struct Thing;
```
## Testing
I added a unit test 😜1 parent aa14ada commit 41f59f1
File tree
3 files changed
+42
-1
lines changed- crates/bevy_reflect
- derive/src
- src
3 files changed
+42
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
| 52 | + | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3606 | 3606 | | |
3607 | 3607 | | |
3608 | 3608 | | |
| 3609 | + | |
| 3610 | + | |
| 3611 | + | |
| 3612 | + | |
| 3613 | + | |
| 3614 | + | |
| 3615 | + | |
| 3616 | + | |
| 3617 | + | |
| 3618 | + | |
| 3619 | + | |
| 3620 | + | |
| 3621 | + | |
| 3622 | + | |
| 3623 | + | |
| 3624 | + | |
| 3625 | + | |
| 3626 | + | |
| 3627 | + | |
| 3628 | + | |
| 3629 | + | |
| 3630 | + | |
| 3631 | + | |
| 3632 | + | |
| 3633 | + | |
| 3634 | + | |
| 3635 | + | |
| 3636 | + | |
| 3637 | + | |
| 3638 | + | |
3609 | 3639 | | |
3610 | 3640 | | |
3611 | 3641 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
588 | 588 | | |
589 | 589 | | |
590 | 590 | | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
591 | 598 | | |
592 | 599 | | |
593 | 600 | | |
| |||
747 | 754 | | |
748 | 755 | | |
749 | 756 | | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
750 | 761 | | |
751 | 762 | | |
752 | 763 | | |
| |||
0 commit comments