Skip to content

Commit

Permalink
restrict_fetch -> transmute_fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
hymm committed Oct 10, 2023
1 parent 227dc00 commit 4b0923c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ pub unsafe trait WorldQuery {

/// Adds component access for when [`WorldQuery`] is not an exact match.
/// i.e. Option<&T> where the matched archetypes don't necessarily contain
/// the data the WorldQuery takes access to in [`update_component_access`]
/// the data the WorldQuery takes access to in [`Self::update_component_access`]
fn optional_access(
state: &Self::State,
access: &mut Access<ComponentId>,
Expand Down
34 changes: 17 additions & 17 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {

/// This is used to transform a [`Query`](crate::system::Query) into a more generic [`Query`](crate::system::Query).
/// This can be useful for passsing to another function tha might take the more generalize query.
/// See [`Query::restrict_fetch`](crate::system::Query::restrict_fetch) for more details.
/// See [`Query::transmute_fetch`](crate::system::Query::transmute_fetch) for more details.
///
/// You should not call `update_archetypes` on the returned [`QueryState`] as the result will be unpredictable.
/// You should not call [`update_archetypes`](Self::update_archetypes) on the returned [`QueryState`] as the result will be unpredictable.
/// You might end up with a mix of archetypes that only matched the original query + archetypes that only match
/// the new [`QueryState`]. Most of the safe methods on [`QueryState`] call [`QueryState::update_archetypes`] internally.
pub(crate) fn restrict_fetch<NewQ: WorldQuery>(
pub(crate) fn transmute_fetch<NewQ: WorldQuery>(
&self,
components: &Components,
) -> QueryState<NewQ, ()> {
Expand Down Expand Up @@ -1615,7 +1615,7 @@ mod tests {
world.spawn((A(22), B));

let query_state = world.query::<(&A, &B)>();
let mut new_query_state = query_state.restrict_fetch::<&A>(world.components());
let mut new_query_state = query_state.transmute_fetch::<&A>(world.components());
assert_eq!(new_query_state.iter(&world).len(), 1);
let a = new_query_state.single(&world);

Expand All @@ -1629,7 +1629,7 @@ mod tests {
world.spawn((A(23), B, C(5)));

let query_state = world.query_filtered::<(&A, &B), Without<C>>();
let mut new_query_state = query_state.restrict_fetch::<&A>(world.components());
let mut new_query_state = query_state.transmute_fetch::<&A>(world.components());
// even though we change the query to not have Without<C>, we do not get the component with C.
let a = new_query_state.single(&world);

Expand All @@ -1643,7 +1643,7 @@ mod tests {
let entity = world.spawn(A(10)).id();

let q = world.query::<()>();
let mut q = q.restrict_fetch::<Entity>(world.components());
let mut q = q.transmute_fetch::<Entity>(world.components());
assert_eq!(q.single(&world), entity);
}

Expand All @@ -1653,11 +1653,11 @@ mod tests {
world.spawn(A(10));

let q = world.query::<&A>();
let mut new_q = q.restrict_fetch::<Ref<A>>(world.components());
let mut new_q = q.transmute_fetch::<Ref<A>>(world.components());
assert!(new_q.single(&world).is_added());

let q = world.query::<Ref<A>>();
let _ = q.restrict_fetch::<&A>(world.components());
let _ = q.transmute_fetch::<&A>(world.components());
}

#[test]
Expand All @@ -1666,8 +1666,8 @@ mod tests {
world.spawn(A(10));

let q = world.query::<&mut A>();
let _ = q.restrict_fetch::<Ref<A>>(world.components());
let _ = q.restrict_fetch::<&A>(world.components());
let _ = q.transmute_fetch::<Ref<A>>(world.components());
let _ = q.transmute_fetch::<&A>(world.components());
}

#[test]
Expand All @@ -1676,7 +1676,7 @@ mod tests {
world.spawn(A(10));

let q = world.query::<EntityMut>();
let _ = q.restrict_fetch::<EntityRef>(world.components());
let _ = q.transmute_fetch::<EntityRef>(world.components());
}

#[test]
Expand All @@ -1685,7 +1685,7 @@ mod tests {
world.spawn((A(22), B));

let query_state = world.query::<(Option<&A>, &B)>();
let _ = query_state.restrict_fetch::<Option<&A>>(world.components());
let _ = query_state.transmute_fetch::<Option<&A>>(world.components());
}

#[test]
Expand All @@ -1697,7 +1697,7 @@ mod tests {
world.spawn(A(22));

let query_state = world.query::<&A>();
let mut _new_query_state = query_state.restrict_fetch::<(&A, &B)>(world.components());
let mut _new_query_state = query_state.transmute_fetch::<(&A, &B)>(world.components());
}

#[test]
Expand All @@ -1709,7 +1709,7 @@ mod tests {
world.spawn(A(22));

let query_state = world.query_filtered::<(&A, &B), Added<B>>();
let mut _new_query_state = query_state.restrict_fetch::<&A>(world.components());
let mut _new_query_state = query_state.transmute_fetch::<&A>(world.components());
}

#[test]
Expand All @@ -1719,7 +1719,7 @@ mod tests {
world.spawn(A(22));

let query_state = world.query::<&A>();
let mut _new_query_state = query_state.restrict_fetch::<&mut A>(world.components());
let mut _new_query_state = query_state.transmute_fetch::<&mut A>(world.components());
}

#[test]
Expand All @@ -1731,7 +1731,7 @@ mod tests {
world.spawn(C(22));

let query_state = world.query::<Option<&A>>();
let mut new_query_state = query_state.restrict_fetch::<&A>(world.components());
let mut new_query_state = query_state.transmute_fetch::<&A>(world.components());
let x = new_query_state.single(&world);
assert_eq!(x.0, 1234);
}
Expand All @@ -1743,7 +1743,7 @@ mod tests {
world.init_component::<A>();

let q = world.query::<EntityRef>();
let _ = q.restrict_fetch::<&A>(world.components());
let _ = q.transmute_fetch::<&A>(world.components());
}
}
}
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,16 +433,16 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
/// }
///
/// fn system_2(mut query: Query<(&mut A, &B)>) {
/// let mut lens = query.restrict_fetch::<&A>();
/// let mut lens = query.transmute_fetch::<&A>();
/// function_that_uses_a_query_lens(&mut lens);
/// }
///
/// # let mut schedule = Schedule::default();
/// # schedule.add_systems((system_1, system_2));
/// # schedule.run(&mut world);
/// ```
pub fn restrict_fetch<NewQ: WorldQuery>(&mut self) -> QueryLens<'_, NewQ> {
let new_state = self.state.restrict_fetch(self.world.components());
pub fn transmute_fetch<NewQ: WorldQuery>(&mut self) -> QueryLens<'_, NewQ> {
let new_state = self.state.transmute_fetch(self.world.components());

QueryLens {
state: new_state,
Expand All @@ -454,7 +454,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {

/// helper method to get a [`QueryLens`] with the same fetch as the existing query
pub fn into_query_lens(&mut self) -> QueryLens<'_, Q> {
self.restrict_fetch()
self.transmute_fetch()
}

/// Returns an [`Iterator`] over the read-only query items.
Expand Down

0 comments on commit 4b0923c

Please sign in to comment.