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

Remove single and single_mut #14268

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/scheduling/run_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn run_condition_yes_with_query(criterion: &mut Criterion) {
group.measurement_time(std::time::Duration::from_secs(3));
fn empty() {}
fn yes_with_query(query: Query<&TestBool>) -> bool {
query.single().0
query.get_single().unwrap().0
}
for amount in 0..21 {
let mut schedule = Schedule::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ fn main() {
}

{
let data: &Foo = query.single();
let mut data2: Mut<Foo> = query.single_mut();
let data: &Foo = query.get_single().unwrap();
let mut data2: Mut<Foo> = query.get_single_mut().unwrap();
//~^ E0502
assert_eq!(data, &mut *data2); // oops UB
}

{
let mut data2: Mut<Foo> = query.single_mut();
let data: &Foo = query.single();
let mut data2: Mut<Foo> = query.get_single_mut().unwrap();
let data: &Foo = query.get_single().unwrap();
//~^ E0502
assert_eq!(data, &mut *data2); // oops UB
}
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_ecs/compile_fail/tests/ui/query_to_readonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ fn for_loops(mut query: Query<&mut Foo>) {
fn single_mut_query(mut query: Query<&mut Foo>) {
// this should fail to compile
{
let mut mut_foo = query.single_mut();
let mut mut_foo = query.get_single_mut().unwrap();

// This solves "temporary value dropped while borrowed"
let readonly_query = query.to_readonly();
//~^ E0502

let ref_foo = readonly_query.single();
let ref_foo = readonly_query.get_single().unwrap();

*mut_foo = Foo;

println!("{ref_foo:?}");
Expand All @@ -53,9 +53,9 @@ fn single_mut_query(mut query: Query<&mut Foo>) {
// This solves "temporary value dropped while borrowed"
let readonly_query = query.to_readonly();

let ref_foo = readonly_query.single();
let ref_foo = readonly_query.get_single().unwrap();

let mut mut_foo = query.single_mut();
let mut mut_foo = query.get_single_mut().unwrap();
//~^ E0502

println!("{ref_foo:?}");
Expand All @@ -68,9 +68,9 @@ fn single_mut_query(mut query: Query<&mut Foo>) {
// This solves "temporary value dropped while borrowed"
let readonly_query = query.to_readonly();

let readonly_foo = readonly_query.single();
let readonly_foo = readonly_query.get_single().unwrap();

let query_foo = query.single();
let query_foo = query.get_single().unwrap();

println!("{readonly_foo:?}, {query_foo:?}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fn main() {
let mut query_a = lens_a.query();
let mut query_b = lens_b.query();

let a = query_a.single_mut();
let b = query_b.single_mut(); // oops 2 mutable references to same Foo
let a = query_a.get_single_mut().unwrap();
let b = query_b.get_single_mut().unwrap(); // oops 2 mutable references to same Foo
assert_eq!(*a, *b);
}

Expand All @@ -34,8 +34,8 @@ fn main() {
let mut query_b = lens.query();
//~^ E0499

let a = query_a.single_mut();
let b = query_b.single_mut(); // oops 2 mutable references to same Foo
let a = query_a.get_single_mut().unwrap();
let b = query_b.get_single_mut().unwrap(); // oops 2 mutable references to same Foo
assert_eq!(*a, *b);
}
}
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,11 +1068,11 @@ mod tests {
#[test]
fn change_expiration() {
fn change_detected(query: Query<Ref<C>>) -> bool {
query.single().is_changed()
query.get_single().unwrap().is_changed()
}

fn change_expired(query: Query<Ref<C>>) -> bool {
query.single().is_changed()
query.get_single().unwrap().is_changed()
}

let mut world = World::new();
Expand Down Expand Up @@ -1114,7 +1114,7 @@ mod tests {
// Since the world is always ahead, as long as changes can't get older than `u32::MAX` (which we ensure),
// the wrapping difference will always be positive, so wraparound doesn't matter.
let mut query = world.query::<Ref<C>>();
assert!(query.single(&world).is_changed());
assert!(query.get_single(&world).unwrap().is_changed());
}

#[test]
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_ecs/src/query/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::{FilteredAccess, QueryData, QueryFilter};
/// .build();
///
/// // Consume the QueryState
/// let (entity, b) = query.single(&world);
/// let (entity, b) = query.get_single(&world);
///```
pub struct QueryBuilder<'w, D: QueryData = (), F: QueryFilter = ()> {
access: FilteredAccess<ComponentId>,
Expand Down Expand Up @@ -272,13 +272,13 @@ mod tests {
.with::<A>()
.without::<C>()
.build();
assert_eq!(entity_a, query_a.single(&world));
assert_eq!(entity_a, query_a.get_single(&world).unwrap());

let mut query_b = QueryBuilder::<Entity>::new(&mut world)
.with::<A>()
.without::<B>()
.build();
assert_eq!(entity_b, query_b.single(&world));
assert_eq!(entity_b, query_b.get_single(&world).unwrap());
}

#[test]
Expand All @@ -294,13 +294,13 @@ mod tests {
.with_id(component_id_a)
.without_id(component_id_c)
.build();
assert_eq!(entity_a, query_a.single(&world));
assert_eq!(entity_a, query_a.get_single(&world).unwrap());

let mut query_b = QueryBuilder::<Entity>::new(&mut world)
.with_id(component_id_a)
.without_id(component_id_b)
.build();
assert_eq!(entity_b, query_b.single(&world));
assert_eq!(entity_b, query_b.get_single(&world).unwrap());
}

#[test]
Expand Down Expand Up @@ -360,7 +360,7 @@ mod tests {
.data::<&B>()
.build();

let entity_ref = query.single(&world);
let entity_ref = query.get_single(&world).unwrap();

assert_eq!(entity, entity_ref.id());

Expand All @@ -383,7 +383,7 @@ mod tests {
.ref_id(component_id_b)
.build();

let entity_ref = query.single(&world);
let entity_ref = query.get_single(&world).unwrap();

assert_eq!(entity, entity_ref.id());

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/query/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum QueryEntityError {
}

/// An error that occurs when evaluating a [`Query`](crate::system::Query) or [`QueryState`](crate::query::QueryState) as a single expected result via
/// [`get_single`](crate::system::Query::get_single) or [`get_single_mut`](crate::system::Query::get_single_mut).
/// [`get_single()`](crate::system::Query::get_single()) or [`single_mut`](crate::system::Query::single_mut).
#[derive(Debug, Error)]
pub enum QuerySingleError {
/// No entity fits the query.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ mod tests {
let _: Option<&Foo> = q.get_manual(&world, e).ok();
let _: Option<[&Foo; 1]> = q.get_many(&world, [e]).ok();
let _: Option<&Foo> = q.get_single(&world).ok();
let _: &Foo = q.single(&world);
let _: &Foo = q.get_single(&world).unwrap();

// system param
let mut q = SystemState::<Query<&mut Foo>>::new(&mut world);
Expand All @@ -773,7 +773,7 @@ mod tests {
let _: Option<[&Foo; 1]> = q.get_many([e]).ok();
let _: Option<&Foo> = q.get_single().ok();
let _: [&Foo; 1] = q.many([e]);
let _: &Foo = q.single();
let _: &Foo = q.get_single().unwrap();
}

// regression test for https://github.com/bevyengine/bevy/pull/8029
Expand Down
70 changes: 17 additions & 53 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,25 +1514,6 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
});
}

/// Returns a single immutable query result when there is exactly one entity matching
/// the query.
///
/// This can only be called for read-only queries,
/// see [`single_mut`](Self::single_mut) for write-queries.
///
/// # Panics
///
/// Panics if the number of query results is not exactly one. Use
/// [`get_single`](Self::get_single) to return a `Result` instead of panicking.
#[track_caller]
#[inline]
pub fn single<'w>(&mut self, world: &'w World) -> ROQueryItem<'w, D> {
match self.get_single(world) {
Ok(items) => items,
Err(error) => panic!("Cannot get single mutable query result: {error}"),
}
}

/// Returns a single immutable query result when there is exactly one entity matching
/// the query.
///
Expand All @@ -1550,31 +1531,14 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {

// SAFETY: query is read only
unsafe {
self.as_readonly().get_single_unchecked_manual(
self.as_readonly().single_unchecked_manual(
world.as_unsafe_world_cell_readonly(),
world.last_change_tick(),
world.read_change_tick(),
)
}
}

/// Returns a single mutable query result when there is exactly one entity matching
/// the query.
///
/// # Panics
///
/// Panics if the number of query results is not exactly one. Use
/// [`get_single_mut`](Self::get_single_mut) to return a `Result` instead of panicking.
#[track_caller]
#[inline]
pub fn single_mut<'w>(&mut self, world: &'w mut World) -> D::Item<'w> {
// SAFETY: query has unique world access
match self.get_single_mut(world) {
Ok(items) => items,
Err(error) => panic!("Cannot get single query result: {error}"),
}
}

/// Returns a single mutable query result when there is exactly one entity matching
/// the query.
///
Expand All @@ -1591,7 +1555,7 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
let last_change_tick = world.last_change_tick();
// SAFETY: query has unique world access
unsafe {
self.get_single_unchecked_manual(
self.single_unchecked_manual(
world.as_unsafe_world_cell(),
last_change_tick,
change_tick,
Expand All @@ -1609,12 +1573,12 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
/// have unique access to the components they query.
#[inline]
pub unsafe fn get_single_unchecked<'w>(
pub unsafe fn single_unchecked<'w>(
&mut self,
world: UnsafeWorldCell<'w>,
) -> Result<D::Item<'w>, QuerySingleError> {
self.update_archetypes_unsafe_world_cell(world);
self.get_single_unchecked_manual(world, world.last_change_tick(), world.change_tick())
self.single_unchecked_manual(world, world.last_change_tick(), world.change_tick())
}

/// Returns a query result when there is exactly one entity matching the query,
Expand All @@ -1628,7 +1592,7 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// This does not check for mutable query correctness. To be safe, make sure mutable queries
/// have unique access to the components they query.
#[inline]
pub unsafe fn get_single_unchecked_manual<'w>(
pub unsafe fn single_unchecked_manual<'w>(
&self,
world: UnsafeWorldCell<'w>,
last_run: Tick,
Expand Down Expand Up @@ -1781,7 +1745,7 @@ mod tests {
let query_state = world.query::<(&A, &B)>();
let mut new_query_state = query_state.transmute::<&A>(world.components());
assert_eq!(new_query_state.iter(&world).len(), 1);
let a = new_query_state.single(&world);
let a = new_query_state.get_single(&world).unwrap();

assert_eq!(a.0, 1);
}
Expand All @@ -1795,7 +1759,7 @@ mod tests {
let query_state = world.query_filtered::<(&A, &B), Without<C>>();
let mut new_query_state = query_state.transmute::<&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);
let a = new_query_state.get_single(&world).unwrap();

assert_eq!(a.0, 0);
}
Expand All @@ -1808,7 +1772,7 @@ mod tests {

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

#[test]
Expand All @@ -1818,7 +1782,7 @@ mod tests {

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

let q = world.query::<Ref<A>>();
let _ = q.transmute::<&A>(world.components());
Expand Down Expand Up @@ -1889,7 +1853,7 @@ mod tests {

let query_state = world.query::<Option<&A>>();
let mut new_query_state = query_state.transmute::<&A>(world.components());
let x = new_query_state.single(&world);
let x = new_query_state.get_single(&world).unwrap();
assert_eq!(x.0, 1234);
}

Expand All @@ -1914,7 +1878,7 @@ mod tests {

let mut query = query;
// Our result is completely untyped
let entity_ref = query.single(&world);
let entity_ref = query.get_single(&world).unwrap();

assert_eq!(entity, entity_ref.id());
assert_eq!(0, entity_ref.get::<A>().unwrap().0);
Expand All @@ -1929,12 +1893,12 @@ mod tests {
let mut query = QueryState::<(Entity, &A, Has<B>)>::new(&mut world)
.transmute_filtered::<(Entity, Has<B>), Added<A>>(world.components());

assert_eq!((entity_a, false), query.single(&world));
assert_eq!((entity_a, false), query.get_single(&world).unwrap());

world.clear_trackers();

let entity_b = world.spawn((A(0), B(0))).id();
assert_eq!((entity_b, true), query.single(&world));
assert_eq!((entity_b, true), query.get_single(&world).unwrap());

world.clear_trackers();

Expand All @@ -1950,15 +1914,15 @@ mod tests {
.transmute_filtered::<Entity, Changed<A>>(world.components());

let mut change_query = QueryState::<&mut A>::new(&mut world);
assert_eq!(entity_a, detection_query.single(&world));
assert_eq!(entity_a, detection_query.get_single(&world).unwrap());

world.clear_trackers();

assert!(detection_query.get_single(&world).is_err());

change_query.single_mut(&mut world).0 = 1;
change_query.get_single_mut(&mut world).unwrap().0 = 1;

assert_eq!(entity_a, detection_query.single(&world));
assert_eq!(entity_a, detection_query.get_single(&world).unwrap());
}

#[test]
Expand Down Expand Up @@ -1986,7 +1950,7 @@ mod tests {
let mut new_query: QueryState<Entity, ()> =
query_1.join_filtered(world.components(), &query_2);

assert_eq!(new_query.single(&world), entity_ab);
assert_eq!(new_query.get_single(&world).unwrap(), entity_ab);
}

#[test]
Expand Down
Loading
Loading