From 5720934499daaddd4467dcdd2b9fdc38156b59b2 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Mon, 9 Nov 2020 18:06:51 -0500 Subject: [PATCH 1/4] Mark Query and QuerySet contructors as pub(crate) --- crates/bevy_ecs/src/system/query/mod.rs | 2 +- crates/bevy_ecs/src/system/query/query_set.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/system/query/mod.rs b/crates/bevy_ecs/src/system/query/mod.rs index e8ec4c4f48520..b61e621c27422 100644 --- a/crates/bevy_ecs/src/system/query/mod.rs +++ b/crates/bevy_ecs/src/system/query/mod.rs @@ -31,7 +31,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { /// This will create a Query that could violate memory safety rules. Make sure that this is only called in /// ways that ensure the Queries have unique mutable access. #[inline] - pub unsafe fn new( + pub(crate) unsafe fn new( world: &'a World, component_access: &'a TypeAccess, ) -> Self { diff --git a/crates/bevy_ecs/src/system/query/query_set.rs b/crates/bevy_ecs/src/system/query/query_set.rs index 0380781dc7a61..bcbaef1d2b107 100644 --- a/crates/bevy_ecs/src/system/query/query_set.rs +++ b/crates/bevy_ecs/src/system/query/query_set.rs @@ -20,7 +20,7 @@ impl QuerySet { /// # Safety /// This will create a set of Query types that could violate memory safety rules. Make sure that this is only called in /// ways that ensure the Queries have unique mutable access. - pub unsafe fn new(world: &World, component_access: &TypeAccess) -> Self { + pub(crate) unsafe fn new(world: &World, component_access: &TypeAccess) -> Self { QuerySet { value: T::new(world, component_access), } From f753bd5fb8ea878d0ab6eef603965dbd0bce3127 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Mon, 9 Nov 2020 18:09:31 -0500 Subject: [PATCH 2/4] Remove unused unsafe methods --- crates/bevy_ecs/src/system/query/mod.rs | 35 ------------------------- 1 file changed, 35 deletions(-) diff --git a/crates/bevy_ecs/src/system/query/mod.rs b/crates/bevy_ecs/src/system/query/mod.rs index b61e621c27422..3b7ae3298bd74 100644 --- a/crates/bevy_ecs/src/system/query/mod.rs +++ b/crates/bevy_ecs/src/system/query/mod.rs @@ -59,15 +59,6 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { unsafe { self.world.query_unchecked() } } - /// Iterates over the query results - /// # Safety - /// This allows aliased mutability. You must make sure this call does not result in multiple mutable references to the same component - #[inline] - pub unsafe fn iter_unsafe(&self) -> QueryIter<'_, Q> { - // SAFE: system runs without conflicts with other systems. same-system queries have runtime borrow checks when they conflict - self.world.query_unchecked() - } - #[inline] pub fn par_iter(&self, batch_size: usize) -> ParIter<'_, Q> where @@ -108,19 +99,6 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { } } - /// Gets the query result for the given `entity` - /// # Safety - /// This allows aliased mutability. You must make sure this call does not result in multiple mutable references to the same component - #[inline] - pub unsafe fn get_unsafe( - &self, - entity: Entity, - ) -> Result<::Item, QueryError> { - self.world - .query_one_unchecked::(entity) - .map_err(|_err| QueryError::NoSuchEntity) - } - /// Gets a reference to the entity's component of the given type. This will fail if the entity does not have /// the given component type or if the given component type does not match this query. pub fn get_component(&self, entity: Entity) -> Result<&T, QueryError> { @@ -169,19 +147,6 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { } } - /// Gets a mutable reference to the entity's component of the given type. This will fail if the entity does not have - /// the given component type - /// # Safety - /// This allows aliased mutability. You must make sure this call does not result in multiple mutable references to the same component - pub unsafe fn get_component_unsafe( - &self, - entity: Entity, - ) -> Result, QueryError> { - self.world - .get_mut_unchecked(entity) - .map_err(QueryError::ComponentError) - } - pub fn removed(&self) -> &[Entity] { self.world.removed::() } From 7877ed7af028dc92ed0ff743d9a9a2d6c15b11c2 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Mon, 9 Nov 2020 19:00:17 -0500 Subject: [PATCH 3/4] cargo fmt --- crates/bevy_ecs/src/system/query/query_set.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/query/query_set.rs b/crates/bevy_ecs/src/system/query/query_set.rs index bcbaef1d2b107..c3b6802e77cdd 100644 --- a/crates/bevy_ecs/src/system/query/query_set.rs +++ b/crates/bevy_ecs/src/system/query/query_set.rs @@ -20,7 +20,10 @@ impl QuerySet { /// # Safety /// This will create a set of Query types that could violate memory safety rules. Make sure that this is only called in /// ways that ensure the Queries have unique mutable access. - pub(crate) unsafe fn new(world: &World, component_access: &TypeAccess) -> Self { + pub(crate) unsafe fn new( + world: &World, + component_access: &TypeAccess, + ) -> Self { QuerySet { value: T::new(world, component_access), } From 410755121d856ec3ce0c83ff315d036c950bafbb Mon Sep 17 00:00:00 2001 From: memoryruins Date: Tue, 10 Nov 2020 02:46:55 -0500 Subject: [PATCH 4/4] Revert "Remove unused unsafe methods" This reverts commit f753bd5fb8ea878d0ab6eef603965dbd0bce3127. --- crates/bevy_ecs/src/system/query/mod.rs | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/bevy_ecs/src/system/query/mod.rs b/crates/bevy_ecs/src/system/query/mod.rs index 3b7ae3298bd74..b61e621c27422 100644 --- a/crates/bevy_ecs/src/system/query/mod.rs +++ b/crates/bevy_ecs/src/system/query/mod.rs @@ -59,6 +59,15 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { unsafe { self.world.query_unchecked() } } + /// Iterates over the query results + /// # Safety + /// This allows aliased mutability. You must make sure this call does not result in multiple mutable references to the same component + #[inline] + pub unsafe fn iter_unsafe(&self) -> QueryIter<'_, Q> { + // SAFE: system runs without conflicts with other systems. same-system queries have runtime borrow checks when they conflict + self.world.query_unchecked() + } + #[inline] pub fn par_iter(&self, batch_size: usize) -> ParIter<'_, Q> where @@ -99,6 +108,19 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { } } + /// Gets the query result for the given `entity` + /// # Safety + /// This allows aliased mutability. You must make sure this call does not result in multiple mutable references to the same component + #[inline] + pub unsafe fn get_unsafe( + &self, + entity: Entity, + ) -> Result<::Item, QueryError> { + self.world + .query_one_unchecked::(entity) + .map_err(|_err| QueryError::NoSuchEntity) + } + /// Gets a reference to the entity's component of the given type. This will fail if the entity does not have /// the given component type or if the given component type does not match this query. pub fn get_component(&self, entity: Entity) -> Result<&T, QueryError> { @@ -147,6 +169,19 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { } } + /// Gets a mutable reference to the entity's component of the given type. This will fail if the entity does not have + /// the given component type + /// # Safety + /// This allows aliased mutability. You must make sure this call does not result in multiple mutable references to the same component + pub unsafe fn get_component_unsafe( + &self, + entity: Entity, + ) -> Result, QueryError> { + self.world + .get_mut_unchecked(entity) + .map_err(QueryError::ComponentError) + } + pub fn removed(&self) -> &[Entity] { self.world.removed::() }