Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic concept for customizing Component reference type #7499

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

chrisjuchem
Copy link
Contributor

Looking for general feedback on this idea, and help solving a lifetime error:

error: lifetime may not live long enough
   --> crates/bevy_ecs/src/query/fetch.rs:535:9
    |
534 |     fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
    |               ------           ------- lifetime `'wshort` defined here
    |               |
    |               lifetime `'wlong` defined here
535 |         item
    |         ^^^^ associated function was supposed to return data with lifetime `'wlong` but it is returning data with lifetime `'wshort`
    |
    = help: consider adding the following bound: `'wshort: 'wlong`

Objective

Allow users to specify which reference types they want their components to be wrapped in when used in a Query. This would allow:

  1. Disabling change detection, by specifying &T and &mut T instead of &T (or Ref<T>) and Mut<T>.
  2. Enabling https://github.com/chrisjuchem/bevy_mod_index to update when components are changes by providing new Indexed reference types.

Solution

  • Add an associated type to components through which the desired reference types can be accessed and used when creating WorldQuery::Items
  • (todo) Allow setting this type with a new attribute in the component derive macro

Changelog

TODO

Migration Guide

TODO

@chrisjuchem chrisjuchem changed the title Basic concept Basic concept for customizing Component reference type Feb 4, 2023
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events X-Controversial There is active debate or serious implications around merging this PR labels Feb 4, 2023
@alice-i-cecile
Copy link
Member

I quite like the basic idea: I can definitely see how this would be useful for #4513. This could also actually be used to make "read-only" component types, by only ever returning a &/Ref.

@Guvante
Copy link
Contributor

Guvante commented Feb 4, 2023

I would take a look at #7344 and #6659 for some other work around allowing mutating the types here.

I believe the solve that #6659 did that I stole was to move the shrink function into a trait.

@chrisjuchem
Copy link
Contributor Author

Thanks @Guvante, that was the trick I needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants