Misc query.rs cleanup (#5591)

# Objective
- `for_each` methods inconsistently used an actual generic param or `impl Trait` change it to use `impl Trait` always, change them to be consistent
- some methods returned `'w 's` or `'_ '_`, change them to return `'_ 's`

## Solution

- Do what i just said

---

## Changelog

- `iter_unsafe` and `get_unchecked` no longer return borrows tied to `'w`

## Migration Guide

transmute the returned borrow from `iter_unsafe` and `get_unchecked` if this broke you (although preferably find a way to write your code that doesnt need to do this...)
This commit is contained in:
Boxy 2022-08-30 21:56:00 +00:00
parent dcdda4cb33
commit ed773dbe30

View File

@ -283,7 +283,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
/// For example, `Query<(&mut A, &B, &mut C), With<D>>` will become `Query<(&A, &B, &C), With<D>>`. /// For example, `Query<(&mut A, &B, &mut C), With<D>>` will become `Query<(&A, &B, &C), With<D>>`.
/// This can be useful when working around the borrow checker, /// This can be useful when working around the borrow checker,
/// or reusing functionality between systems via functions that accept query types. /// or reusing functionality between systems via functions that accept query types.
pub fn to_readonly(&self) -> Query<'_, '_, Q::ReadOnly, F::ReadOnly> { pub fn to_readonly(&self) -> Query<'_, 's, Q::ReadOnly, F::ReadOnly> {
let new_state = self.state.as_readonly(); let new_state = self.state.as_readonly();
// SAFETY: This is memory safe because it turns the query immutable. // SAFETY: This is memory safe because it turns the query immutable.
unsafe { unsafe {
@ -372,7 +372,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
#[inline] #[inline]
pub fn iter_combinations<const K: usize>( pub fn iter_combinations<const K: usize>(
&self, &self,
) -> QueryCombinationIter<'_, '_, Q::ReadOnly, F::ReadOnly, K> { ) -> QueryCombinationIter<'_, 's, Q::ReadOnly, F::ReadOnly, K> {
// SAFETY: system runs without conflicts with other systems. // SAFETY: system runs without conflicts with other systems.
// same-system queries have runtime borrow checks when they conflict // same-system queries have runtime borrow checks when they conflict
unsafe { unsafe {
@ -409,7 +409,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
#[inline] #[inline]
pub fn iter_combinations_mut<const K: usize>( pub fn iter_combinations_mut<const K: usize>(
&mut self, &mut self,
) -> QueryCombinationIter<'_, '_, Q, F, K> { ) -> QueryCombinationIter<'_, 's, Q, F, K> {
// SAFETY: system runs without conflicts with other systems. // SAFETY: system runs without conflicts with other systems.
// same-system queries have runtime borrow checks when they conflict // same-system queries have runtime borrow checks when they conflict
unsafe { unsafe {
@ -455,7 +455,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
pub fn iter_many<EntityList: IntoIterator>( pub fn iter_many<EntityList: IntoIterator>(
&self, &self,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'_, '_, Q::ReadOnly, F::ReadOnly, EntityList::IntoIter> ) -> QueryManyIter<'_, 's, Q::ReadOnly, F::ReadOnly, EntityList::IntoIter>
where where
EntityList::Item: Borrow<Entity>, EntityList::Item: Borrow<Entity>,
{ {
@ -504,7 +504,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
pub fn iter_many_mut<EntityList: IntoIterator>( pub fn iter_many_mut<EntityList: IntoIterator>(
&mut self, &mut self,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'_, '_, Q, F, EntityList::IntoIter> ) -> QueryManyIter<'_, 's, Q, F, EntityList::IntoIter>
where where
EntityList::Item: Borrow<Entity>, EntityList::Item: Borrow<Entity>,
{ {
@ -527,7 +527,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
/// This function makes it possible to violate Rust's aliasing guarantees. You must make sure /// This function makes it possible to violate Rust's aliasing guarantees. You must make sure
/// this call does not result in multiple mutable references to the same component /// this call does not result in multiple mutable references to the same component
#[inline] #[inline]
pub unsafe fn iter_unsafe(&'s self) -> QueryIter<'w, 's, Q, F> { pub unsafe fn iter_unsafe(&self) -> QueryIter<'_, 's, Q, F> {
// SEMI-SAFETY: system runs without conflicts with other systems. // SEMI-SAFETY: system runs without conflicts with other systems.
// same-system queries have runtime borrow checks when they conflict // same-system queries have runtime borrow checks when they conflict
self.state self.state
@ -543,7 +543,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
#[inline] #[inline]
pub unsafe fn iter_combinations_unsafe<const K: usize>( pub unsafe fn iter_combinations_unsafe<const K: usize>(
&self, &self,
) -> QueryCombinationIter<'_, '_, Q, F, K> { ) -> QueryCombinationIter<'_, 's, Q, F, K> {
// SEMI-SAFETY: system runs without conflicts with other systems. // SEMI-SAFETY: system runs without conflicts with other systems.
// same-system queries have runtime borrow checks when they conflict // same-system queries have runtime borrow checks when they conflict
self.state.iter_combinations_unchecked_manual( self.state.iter_combinations_unchecked_manual(
@ -564,7 +564,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
pub unsafe fn iter_many_unsafe<EntityList: IntoIterator>( pub unsafe fn iter_many_unsafe<EntityList: IntoIterator>(
&self, &self,
entities: EntityList, entities: EntityList,
) -> QueryManyIter<'_, '_, Q, F, EntityList::IntoIter> ) -> QueryManyIter<'_, 's, Q, F, EntityList::IntoIter>
where where
EntityList::Item: Borrow<Entity>, EntityList::Item: Borrow<Entity>,
{ {
@ -635,7 +635,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
/// # bevy_ecs::system::assert_is_system(gravity_system); /// # bevy_ecs::system::assert_is_system(gravity_system);
/// ``` /// ```
#[inline] #[inline]
pub fn for_each_mut<'a, FN: FnMut(QueryItem<'a, Q>)>(&'a mut self, f: FN) { pub fn for_each_mut<'a>(&'a mut self, f: impl FnMut(QueryItem<'a, Q>)) {
// SAFETY: system runs without conflicts with other systems. same-system queries have runtime // SAFETY: system runs without conflicts with other systems. same-system queries have runtime
// borrow checks when they conflict // borrow checks when they conflict
unsafe { unsafe {
@ -701,10 +701,10 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
/// ///
/// [`ComputeTaskPool`]: bevy_tasks::prelude::ComputeTaskPool /// [`ComputeTaskPool`]: bevy_tasks::prelude::ComputeTaskPool
#[inline] #[inline]
pub fn par_for_each_mut<'a, FN: Fn(QueryItem<'a, Q>) + Send + Sync + Clone>( pub fn par_for_each_mut<'a>(
&'a mut self, &'a mut self,
batch_size: usize, batch_size: usize,
f: FN, f: impl Fn(QueryItem<'a, Q>) + Send + Sync + Clone,
) { ) {
// SAFETY: system runs without conflicts with other systems. same-system queries have runtime // SAFETY: system runs without conflicts with other systems. same-system queries have runtime
// borrow checks when they conflict // borrow checks when they conflict
@ -947,9 +947,9 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
/// this call does not result in multiple mutable references to the same component /// this call does not result in multiple mutable references to the same component
#[inline] #[inline]
pub unsafe fn get_unchecked( pub unsafe fn get_unchecked(
&'s self, &self,
entity: Entity, entity: Entity,
) -> Result<QueryItem<'w, Q>, QueryEntityError> { ) -> Result<QueryItem<'_, Q>, QueryEntityError> {
// SEMI-SAFETY: system runs without conflicts with other systems. // SEMI-SAFETY: system runs without conflicts with other systems.
// same-system queries have runtime borrow checks when they conflict // same-system queries have runtime borrow checks when they conflict
self.state self.state
@ -1374,7 +1374,7 @@ impl<'w, 's, Q: ReadOnlyWorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
/// # bevy_ecs::system::assert_is_system(print_selected_character_name_system); /// # bevy_ecs::system::assert_is_system(print_selected_character_name_system);
/// ``` /// ```
#[inline] #[inline]
pub fn get_inner(&'s self, entity: Entity) -> Result<ROQueryItem<'w, Q>, QueryEntityError> { pub fn get_inner(&self, entity: Entity) -> Result<ROQueryItem<'w, Q>, QueryEntityError> {
// SAFETY: system runs without conflicts with other systems. // SAFETY: system runs without conflicts with other systems.
// same-system queries have runtime borrow checks when they conflict // same-system queries have runtime borrow checks when they conflict
unsafe { unsafe {
@ -1411,7 +1411,7 @@ impl<'w, 's, Q: ReadOnlyWorldQuery, F: WorldQuery> Query<'w, 's, Q, F> {
/// # bevy_ecs::system::assert_is_system(report_names_system); /// # bevy_ecs::system::assert_is_system(report_names_system);
/// ``` /// ```
#[inline] #[inline]
pub fn iter_inner(&'s self) -> QueryIter<'w, 's, Q::ReadOnly, F::ReadOnly> { pub fn iter_inner(&self) -> QueryIter<'w, 's, Q::ReadOnly, F::ReadOnly> {
// SAFETY: system runs without conflicts with other systems. // SAFETY: system runs without conflicts with other systems.
// same-system queries have runtime borrow checks when they conflict // same-system queries have runtime borrow checks when they conflict
unsafe { unsafe {