-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add subtrait for reborrowing QueryData types #22025
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
base: main
Are you sure you want to change the base?
Conversation
6becb1e to
6f0ab87
Compare
6f0ab87 to
2538b79
Compare
NicoZweifel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty neat! This makes total sense to me in terms of utility functions and the like. I suggested a test that tests exactly this use case.
Other than that there are only a few minor things:
-
For the
EntityReftypes, we can dereference and use theCopyimplementations instead of manually creating the struct again, which is a bit more maintainable. -
The implemention of
Ref<>can also be slightly simplified sinceComponentTicksRefimplementsClone. -
I noticed a few missing square brackets in the doc comments.
|
|
||
| fn assert_is_reborrow_query_data<Q: ReborrowQueryData>() {} | ||
| assert_is_reborrow_query_data::<ReborrowData>(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| } | |
| } | |
| #[test] | |
| fn test_reborrow_allows_reuse() { | |
| #[derive(Component)] | |
| struct A(f32); | |
| fn increment(mut component: Mut<A>) { | |
| component.0 += 1.; | |
| } | |
| let mut world = World::new(); | |
| world.spawn(A(0.)); | |
| let mut query = world.query::<&mut A>(); | |
| let mut component = query.single_mut(&mut world).unwrap(); | |
| increment(component.reborrow()); | |
| component.0 += 1.; | |
| assert_eq!(component.0, 2.0); | |
| } |
| /// Returns an `EntityRef<>` with a smaller lifetime. | ||
| /// This is useful if you have `&EntityRef`, but you need an `EntityRef`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Returns an `EntityRef<>` with a smaller lifetime. | |
| /// This is useful if you have `&EntityRef`, but you need an `EntityRef`. | |
| /// Returns an [`EntityRef<>`] with a smaller lifetime. | |
| /// This is useful if you have &[`EntityRef`], but you need an [`EntityRef`]. |
| /// Returns an `EntityRefExcept<>` with a smaller lifetime. | ||
| /// This is useful if you have `&EntityRefExcept`, but you need an `EntityRefExcept`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Returns an `EntityRefExcept<>` with a smaller lifetime. | |
| /// This is useful if you have `&EntityRefExcept`, but you need an `EntityRefExcept`. | |
| /// Returns an [`EntityRefExcept<>`] with a smaller lifetime. | |
| /// This is useful if you have &[`EntityRefExcept`], but you need an [`EntityRefExcept`]. |
| /// Returns an `FilteredEntityRef<>` with a smaller lifetime. | ||
| /// This is useful if you have `&FilteredEntityRef`, but you need an `FilteredEntityRef`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Returns an `FilteredEntityRef<>` with a smaller lifetime. | |
| /// This is useful if you have `&FilteredEntityRef`, but you need an `FilteredEntityRef`. | |
| /// Returns an [`FilteredEntityRef<>`] with a smaller lifetime. | |
| /// This is useful if you have &[`FilteredEntityRef`], but you need an [`FilteredEntityRef`]. |
| /// Returns a `Mut<>` with a smaller lifetime. | ||
| /// This is useful if you have `&Ref<T>`, but you need a `Ref<T>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Returns a `Mut<>` with a smaller lifetime. | |
| /// This is useful if you have `&Ref<T>`, but you need a `Ref<T>`. | |
| /// Returns a [`Mut<>`] with a smaller lifetime. | |
| /// This is useful if you have &[`Ref<T>`], but you need a [`Ref<T>`]. |
| /// A [`QueryData`] that's able to be reborrowed, converting a reference into | ||
| /// an owned struct with a shorter lifetime. | ||
| pub trait ReborrowQueryData: QueryData { | ||
| /// Returns a `QueryData` item with a smaller lifetime. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Returns a `QueryData` item with a smaller lifetime. | |
| /// Returns a [`QueryData`] item with a smaller lifetime. |
| EntityRefExcept { | ||
| entity: self.entity, | ||
| access: self.access, | ||
| phantom: PhantomData, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| EntityRefExcept { | |
| entity: self.entity, | |
| access: self.access, | |
| phantom: PhantomData, | |
| } | |
| *self |
| /// Returns an `EntityRef<>` with a smaller lifetime. | ||
| /// This is useful if you have `&EntityRef`, but you need an `EntityRef`. | ||
| pub fn reborrow(&self) -> EntityRef<'_> { | ||
| EntityRef { cell: self.cell } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| EntityRef { cell: self.cell } | |
| *self |
| FilteredEntityRef { | ||
| entity: self.entity, | ||
| access: self.access, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| FilteredEntityRef { | |
| entity: self.entity, | |
| access: self.access, | |
| } | |
| *self |
| Ref { | ||
| value: self.value, | ||
| ticks: ComponentTicksRef { | ||
| added: self.ticks.added, | ||
| changed: self.ticks.changed, | ||
| changed_by: self.ticks.changed_by, | ||
| last_run: self.ticks.last_run, | ||
| this_run: self.ticks.this_run, | ||
| }, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Ref { | |
| value: self.value, | |
| ticks: ComponentTicksRef { | |
| added: self.ticks.added, | |
| changed: self.ticks.changed, | |
| changed_by: self.ticks.changed_by, | |
| last_run: self.ticks.last_run, | |
| this_run: self.ticks.this_run, | |
| }, | |
| } | |
| Ref { | |
| ticks: self.ticks.clone(), | |
| ..*self | |
| } |
Objective
Allow reborrowing for QueryData types. The end goal here is to work towards a fast
System::run_iterapi, avoiding the overhead ofget_paramwith each iteration. Funnily enough the only system param that actually needs this isSingle, but it seems worthwhile to have anywaysNext steps:
ReborrowSystemParamSystemIterSolution
ReborrowQueryData, an optional subtrait (though almost all QueryData types should implement this in practice)