Skip to content

Commit 95de068

Browse files
committed
ReborrowQueryData
1 parent e8520c1 commit 95de068

File tree

8 files changed

+310
-0
lines changed

8 files changed

+310
-0
lines changed

crates/bevy_ecs/macros/src/query_data.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ pub fn derive_query_data_impl(input: TokenStream) -> TokenStream {
247247
}
248248
}
249249

250+
251+
250252
fn provide_extra_access(
251253
state: &mut Self::State,
252254
access: &mut #path::query::Access,
@@ -269,6 +271,22 @@ pub fn derive_query_data_impl(input: TokenStream) -> TokenStream {
269271
}
270272
}
271273

274+
impl #user_impl_generics #path::query::ReborrowQueryData
275+
for #read_only_struct_name #user_ty_generics #user_where_clauses
276+
// Make these HRTBs with an unused lifetime parameter to allow trivial constraints
277+
// See https://github.com/rust-lang/rust/issues/48214
278+
where #(for<'__a> #field_types: #path::query::ReborrowQueryData,)* {
279+
fn reborrow<'wlong: 'short, 'slong: 'short, 'short>(
280+
item: &'short mut Self::Item<'wlong, 'slong>,
281+
) -> Self::Item<'short, 'short> {
282+
#read_only_item_struct_name {
283+
#(
284+
#field_members: <#read_only_field_types>::reborrow(&mut item.#field_members),
285+
)*
286+
}
287+
}
288+
}
289+
272290
impl #user_impl_generics #path::query::ReleaseStateQueryData
273291
for #read_only_struct_name #user_ty_generics #user_where_clauses
274292
// Make these HRTBs with an unused lifetime parameter to allow trivial constraints
@@ -334,6 +352,22 @@ pub fn derive_query_data_impl(input: TokenStream) -> TokenStream {
334352
}
335353
}
336354

355+
impl #user_impl_generics #path::query::ReborrowQueryData
356+
for #struct_name #user_ty_generics #user_where_clauses
357+
// Make these HRTBs with an unused lifetime parameter to allow trivial constraints
358+
// See https://github.com/rust-lang/rust/issues/48214
359+
where #(for<'__a> #field_types: #path::query::ReborrowQueryData,)* {
360+
fn reborrow<'wlong: 'short, 'slong: 'short, 'short>(
361+
item: &'short mut Self::Item<'wlong, 'slong>,
362+
) -> Self::Item<'short, 'short> {
363+
#item_struct_name {
364+
#(
365+
#field_members: <#field_types>::reborrow(&mut item.#field_members),
366+
)*
367+
}
368+
}
369+
}
370+
337371
impl #user_impl_generics #path::query::ReleaseStateQueryData
338372
for #struct_name #user_ty_generics #user_where_clauses
339373
// Make these HRTBs with an unused lifetime parameter to allow trivial constraints

crates/bevy_ecs/src/change_detection/params.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,21 @@ impl<'w, T: ?Sized> Ref<'w, T> {
360360
self.ticks.last_run = last_run;
361361
self.ticks.this_run = this_run;
362362
}
363+
364+
/// Returns a `Mut<>` with a smaller lifetime.
365+
/// This is useful if you have `&Ref<T>`, but you need a `Ref<T>`.
366+
pub fn reborrow(&self) -> Ref<'_, T> {
367+
Ref {
368+
value: self.value,
369+
ticks: ComponentTicksRef {
370+
added: self.ticks.added,
371+
changed: self.ticks.changed,
372+
changed_by: self.ticks.changed_by,
373+
last_run: self.ticks.last_run,
374+
this_run: self.ticks.this_run,
375+
},
376+
}
377+
}
363378
}
364379

365380
impl<'w, 'a, T> IntoIterator for &'a Ref<'w, T>

0 commit comments

Comments
 (0)