Skip to content

Conversation

@cart
Copy link
Member

@cart cart commented Dec 3, 2025

Objective

The proposed "Resources as entities" PR makes Resource implement Component. This creates a situation where we need to do:

#[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:

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):

#[derive(Resource, Reflect)]
#[reflect(Resource)]
struct Thing;

Testing

I added a unit test 😜

@cart cart added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Reflection Runtime information about types labels Dec 3, 2025
@alice-i-cecile
Copy link
Member

This makes a lot of sense to me: it mirrors the notion of subtraits.

@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Uncontroversial This work is generally agreed upon D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes labels Dec 3, 2025
@cart cart added this pull request to the merge queue Dec 3, 2025
Merged via the queue into bevyengine:main with commit 41f59f1 Dec 3, 2025
36 checks passed
@cart cart added this to the 0.18 milestone Dec 3, 2025
github-merge-queue bot pushed a commit that referenced this pull request Dec 5, 2025
# Objective

My [TypeData dependencies](#22016 ) PR regressed native binary size by
~8mb.

(thank you [twitcher](https://github.com/bevyengine/twitcher/))

<img width="858" height="306" alt="image"
src="https://github.com/user-attachments/assets/e6d86c67-33f6-4280-bee4-49771014b678"
/>

## Solution

- Inline `register_type_data`, which restores the original binary size:
  - `release breakout` before #22016: 127.3mb
  - `release breakout` after #22016: 136.4mb
  - `release breakout` after this PR: 127.4mb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Reflection Runtime information about types C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants