Skip to content

Commit

Permalink
Revert "ecs: remove &mut requirement on query iterators"
Browse files Browse the repository at this point in the history
This reverts commit 6dc1d07.
  • Loading branch information
cart committed Jul 22, 2020
1 parent 6dc1d07 commit 0c2e26d
Show file tree
Hide file tree
Showing 33 changed files with 93 additions and 88 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_core/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn entity_labels_system(
mut query: Query<(Entity, &Labels)>,
) {
let entity_labels = entity_labels.deref_mut();
for (entity, labels) in query.iter() {
for (entity, labels) in &mut query.iter() {
let current_labels = entity_labels
.entity_labels
.entry(entity)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Timer {
}

pub fn timer_system(time: Res<Time>, mut query: Query<&mut Timer>) {
for mut timer in query.iter() {
for mut timer in &mut query.iter() {
timer.tick(time.delta_seconds);
}
}
2 changes: 1 addition & 1 deletion crates/bevy_ecs/hecs/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn iterate_100k(b: &mut Bencher) {
world.spawn((Position(-(i as f32)), Velocity(i as f32)));
}
b.iter(|| {
for (mut pos, vel) in world.query::<(&mut Position, &Velocity)>() {
for (mut pos, vel) in &mut world.query::<(&mut Position, &Velocity)>() {
pos.0 += vel.0;
}
})
Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_ecs/hecs/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl<'w, Q: Query> QueryBorrow<'w, Q> {
/// Execute the query
///
/// Must be called only once per query.
pub fn iter(mut self) -> QueryIter<'w, Q> {
pub fn iter<'q>(&'q mut self) -> QueryIter<'q, 'w, Q> {
self.borrow();
QueryIter {
borrow: self,
Expand Down Expand Up @@ -434,27 +434,27 @@ impl<'w, Q: Query> Drop for QueryBorrow<'w, Q> {
}
}

impl<'w, Q: Query> IntoIterator for QueryBorrow<'w, Q> {
type Item = <Q::Fetch as Fetch<'w>>::Item;
type IntoIter = QueryIter<'w, Q>;
impl<'q, 'w, Q: Query> IntoIterator for &'q mut QueryBorrow<'w, Q> {
type Item = <Q::Fetch as Fetch<'q>>::Item;
type IntoIter = QueryIter<'q, 'w, Q>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

/// Iterator over the set of entities with the components in `Q`
pub struct QueryIter<'w, Q: Query> {
borrow: QueryBorrow<'w, Q>,
pub struct QueryIter<'q, 'w, Q: Query> {
borrow: &'q mut QueryBorrow<'w, Q>,
archetype_index: u32,
iter: Option<ChunkIter<Q>>,
}

unsafe impl<'w, Q: Query> Send for QueryIter<'w, Q> {}
unsafe impl<'w, Q: Query> Sync for QueryIter<'w, Q> {}
unsafe impl<'q, 'w, Q: Query> Send for QueryIter<'q, 'w, Q> {}
unsafe impl<'q, 'w, Q: Query> Sync for QueryIter<'q, 'w, Q> {}

impl<'w, Q: Query> Iterator for QueryIter<'w, Q> {
type Item = <Q::Fetch as Fetch<'w>>::Item;
impl<'q, 'w, Q: Query> Iterator for QueryIter<'q, 'w, Q> {
type Item = <Q::Fetch as Fetch<'q>>::Item;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -489,7 +489,7 @@ impl<'w, Q: Query> Iterator for QueryIter<'w, Q> {
}
}

impl<'w, Q: Query> ExactSizeIterator for QueryIter<'w, Q> {
impl<'q, 'w, Q: Query> ExactSizeIterator for QueryIter<'q, 'w, Q> {
fn len(&self) -> usize {
self.borrow
.archetypes
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/hecs/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ impl World {
///
/// See `remove`.
pub fn remove_one<T: Component>(&mut self, entity: Entity) -> Result<T, ComponentError> {
std::println!("reomve {} ", std::any::type_name::<T>());
self.remove::<(T,)>(entity).map(|(x,)| x)
}

Expand Down
11 changes: 11 additions & 0 deletions crates/bevy_ecs/hecs/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ fn clear() {
assert_eq!(world.iter().count(), 0);
}

#[test]
#[should_panic(expected = "twice on the same borrow")]
fn alias() {
let mut world = World::new();
world.spawn(("abc", 123));
world.spawn(("def", 456, true));
let mut q = world.query::<&mut i32>();
let _a = q.iter().collect::<Vec<_>>();
let _b = q.iter().collect::<Vec<_>>();
}

#[test]
fn remove_missing() {
let mut world = World::new();
Expand Down
4 changes: 0 additions & 4 deletions crates/bevy_ecs/src/system/into_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ impl<'a, Q: HecsQuery> Query<'a, Q> {
.archetype_access
.immutable
.contains(location.archetype as usize)
|| self
.archetype_access
.mutable
.contains(location.archetype as usize)
{
self.world
.get(entity)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/camera/active_cameras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn active_cameras_system(
) {
for (name, active_camera) in active_cameras.cameras.iter_mut() {
if let None = active_camera {
for (camera_entity, camera) in query.iter() {
for (camera_entity, camera) in &mut query.iter() {
if let Some(ref current_name) = camera.name {
if current_name == name {
*active_camera = Some(camera_entity);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn camera_system<T: CameraProjection + Component>(
}
}

for (mut camera, mut camera_projection) in query.iter() {
for (mut camera, mut camera_projection) in &mut query.iter() {
if let Some(window) = match camera.window {
WindowReference::Id(id) => {
if changed_window_ids.contains(&id) {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/camera/visible_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ pub fn visible_entities_system(
mut draw_query: Query<(Entity, &Draw)>,
draw_transform_query: Query<(&Draw, &Transform)>,
) {
for (_camera, camera_transform, mut visible_entities) in camera_query.iter() {
for (_camera, camera_transform, mut visible_entities) in &mut camera_query.iter() {
visible_entities.value.clear();
let camera_position = camera_transform.value.w_axis().truncate();

let mut no_transform_order = 0.0;
let mut transparent_entities = Vec::new();
for (entity, draw) in draw_query.iter() {
for (entity, draw) in &mut draw_query.iter() {
if !draw.is_visible {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub trait Drawable {
}

pub fn clear_draw_system(mut query: Query<&mut Draw>) {
for mut draw in query.iter() {
for mut draw in &mut query.iter() {
draw.clear_render_commands();
}
}
2 changes: 1 addition & 1 deletion crates/bevy_render/src/mesh/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ pub fn mesh_resource_provider_system(
}

// TODO: remove this once batches are pipeline specific and deprecate assigned_meshes draw target
for (handle, mut render_pipelines) in query.iter() {
for (handle, mut render_pipelines) in &mut query.iter() {
if let Some(mesh) = meshes.get(&handle) {
for render_pipeline in render_pipelines.pipelines.iter_mut() {
render_pipeline.specialization.primitive_topology = mesh.primitive_topology;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/pipeline/render_pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn draw_render_pipelines_system(
mut render_resource_bindings: ResMut<RenderResourceBindings>,
mut query: Query<(&mut Draw, &mut RenderPipelines)>,
) {
for (mut draw, mut render_pipelines) in query.iter() {
for (mut draw, mut render_pipelines) in &mut query.iter() {
let mut drawable = DrawableRenderPipelines {
render_pipelines: &mut render_pipelines,
render_resource_bindings: &mut render_resource_bindings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ fn render_resources_node_system<T: RenderResources>(
let render_resource_context = &**render_resource_context;
state.uniform_buffer_arrays.reset_changed_item_counts();
// update uniforms info
for (uniforms, draw, _render_pipelines) in query.iter() {
for (uniforms, draw, _render_pipelines) in &mut query.iter() {
if !draw.is_visible {
return;
}
Expand All @@ -438,7 +438,7 @@ fn render_resources_node_system<T: RenderResources>(
.uniform_buffer_arrays
.update_staging_buffer(render_resource_context);

for (uniforms, draw, mut render_pipelines) in query.iter() {
for (uniforms, draw, mut render_pipelines) in &mut query.iter() {
if !draw.is_visible {
return;
}
Expand All @@ -456,7 +456,7 @@ fn render_resources_node_system<T: RenderResources>(
staging_buffer,
0..state.uniform_buffer_arrays.staging_buffer_size as u64,
&mut |mut staging_buffer, _render_resource_context| {
for (uniforms, draw, mut render_pipelines) in query.iter() {
for (uniforms, draw, mut render_pipelines) in &mut query.iter() {
if !draw.is_visible {
return;
}
Expand All @@ -479,7 +479,7 @@ fn render_resources_node_system<T: RenderResources>(
} else {
// TODO: can we just remove this?
let mut staging_buffer: [u8; 0] = [];
for (uniforms, draw, mut render_pipelines) in query.iter() {
for (uniforms, draw, mut render_pipelines) in &mut query.iter() {
if !draw.is_visible {
return;
}
Expand Down Expand Up @@ -657,7 +657,7 @@ fn asset_render_resources_node_system<T: RenderResources>(
}
}

for (asset_handle, _draw, mut render_pipelines) in query.iter() {
for (asset_handle, _draw, mut render_pipelines) in &mut query.iter() {
if let Some(asset_bindings) = asset_render_resource_bindings.get(*asset_handle) {
render_pipelines.bindings.extend(asset_bindings);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_render/src/shader/shader_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn shader_defs_system<T>(mut query: Query<(&T, &mut RenderPipelines)>)
where
T: ShaderDefs + Send + Sync + 'static,
{
for (shader_defs, mut render_pipelines) in query.iter() {
for (shader_defs, mut render_pipelines) in &mut query.iter() {
for shader_def in shader_defs.iter_shader_defs() {
for render_pipeline in render_pipelines.pipelines.iter_mut() {
render_pipeline
Expand All @@ -73,7 +73,7 @@ where
}

pub fn clear_shader_defs_system(mut query: Query<&mut RenderPipelines>) {
for mut render_pipelines in query.iter() {
for mut render_pipelines in &mut query.iter() {
for render_pipeline in render_pipelines.pipelines.iter_mut() {
render_pipeline
.specialization
Expand All @@ -90,7 +90,7 @@ pub fn asset_shader_defs_system<T>(
) where
T: ShaderDefs + Send + Sync + 'static,
{
for (asset_handle, mut render_pipelines) in query.iter() {
for (asset_handle, mut render_pipelines) in &mut query.iter() {
let shader_defs = assets.get(&asset_handle).unwrap();
for shader_def in shader_defs.iter_shader_defs() {
for render_pipeline in render_pipelines.pipelines.iter_mut() {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn sprite_system(
textures: Res<Assets<Texture>>,
mut query: Query<(&mut Sprite, &Handle<ColorMaterial>)>,
) {
for (mut sprite, handle) in query.iter() {
for (mut sprite, handle) in &mut query.iter() {
let material = materials.get(&handle).unwrap();
if let Some(texture_handle) = material.texture {
if let Some(texture) = textures.get(&texture_handle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn missing_previous_parent_system(
mut query: Query<Without<PreviousParent, (Entity, &Parent)>>,
) {
// Add missing `PreviousParent` components
for (entity, _parent) in query.iter() {
for (entity, _parent) in &mut query.iter() {
log::trace!("Adding missing PreviousParent to {:?}", entity);
commands.insert_one(entity, PreviousParent(None));
}
Expand All @@ -24,7 +24,7 @@ pub fn parent_update_system(
) {
// Entities with a missing `Parent` (ie. ones that have a `PreviousParent`), remove
// them from the `Children` of the `PreviousParent`.
for (entity, previous_parent) in removed_parent_query.iter() {
for (entity, previous_parent) in &mut removed_parent_query.iter() {
log::trace!("Parent was removed from {:?}", entity);
if let Some(previous_parent_entity) = previous_parent.0 {
if let Ok(mut previous_parent_children) =
Expand All @@ -40,7 +40,7 @@ pub fn parent_update_system(
let mut children_additions = HashMap::<Entity, SmallVec<[Entity; 8]>>::with_capacity(16);

// Entities with a changed Parent (that also have a PreviousParent, even if None)
for (entity, parent, mut previous_parent) in changed_parent_query.iter() {
for (entity, parent, mut previous_parent) in &mut changed_parent_query.iter() {
log::trace!("Parent changed for {:?}", entity);

// If the `PreviousParent` is not None.
Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_transform/src/local_transform_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn local_transform_translation_system(
>,
>,
) {
for (mut local, translation) in query.iter() {
for (mut local, translation) in &mut query.iter() {
*local = LocalTransform(Mat4::from_translation(translation.0));
}
}
Expand All @@ -26,7 +26,7 @@ pub fn local_transform_rotation_system(
>,
>,
) {
for (mut local, rotation) in query.iter() {
for (mut local, rotation) in &mut query.iter() {
*local = LocalTransform(Mat4::from_quat(rotation.0));
}
}
Expand All @@ -39,7 +39,7 @@ pub fn local_transform_scale_system(
>,
>,
) {
for (mut local, scale) in query.iter() {
for (mut local, scale) in &mut query.iter() {
*local = LocalTransform(Mat4::from_scale(Vec3::new(scale.0, scale.0, scale.0)));
}
}
Expand All @@ -52,7 +52,7 @@ pub fn local_transform_non_uniform_scale_system(
>,
>,
) {
for (mut local, non_uniform_scale) in query.iter() {
for (mut local, non_uniform_scale) in &mut query.iter() {
*local = LocalTransform(Mat4::from_scale(non_uniform_scale.0));
}
}
Expand All @@ -62,7 +62,7 @@ pub fn local_transform_translation_rotation_system(
Without<Scale, Without<NonUniformScale, (&mut LocalTransform, &Translation, &Rotation)>>,
>,
) {
for (mut local, translation, rotation) in query.iter() {
for (mut local, translation, rotation) in &mut query.iter() {
*local = LocalTransform(Mat4::from_rotation_translation(rotation.0, translation.0));
}
}
Expand All @@ -72,7 +72,7 @@ pub fn local_transform_translation_scale_system(
Without<Rotation, Without<NonUniformScale, (&mut LocalTransform, &Translation, &Scale)>>,
>,
) {
for (mut local, translation, scale) in query.iter() {
for (mut local, translation, scale) in &mut query.iter() {
*local = LocalTransform(Mat4::from_scale_rotation_translation(
Vec3::new(scale.0, scale.0, scale.0),
Quat::default(),
Expand All @@ -86,7 +86,7 @@ pub fn local_transform_translation_non_uniform_scale_system(
Without<Rotation, Without<Scale, (&mut LocalTransform, &Translation, &NonUniformScale)>>,
>,
) {
for (mut local, translation, non_uniform_scale) in query.iter() {
for (mut local, translation, non_uniform_scale) in &mut query.iter() {
*local = LocalTransform(Mat4::from_scale_rotation_translation(
non_uniform_scale.0,
Quat::default(),
Expand All @@ -100,7 +100,7 @@ pub fn local_transform_rotation_scale_system(
Without<Translation, Without<NonUniformScale, (&mut LocalTransform, &Rotation, &Scale)>>,
>,
) {
for (mut local, rotation, scale) in query.iter() {
for (mut local, rotation, scale) in &mut query.iter() {
*local = LocalTransform(Mat4::from_scale_rotation_translation(
Vec3::new(scale.0, scale.0, scale.0),
rotation.0,
Expand All @@ -114,7 +114,7 @@ pub fn local_transform_rotation_non_uniform_scale_system(
Without<Translation, Without<Scale, (&mut LocalTransform, &Rotation, &NonUniformScale)>>,
>,
) {
for (mut local, rotation, non_uniform_scale) in query.iter() {
for (mut local, rotation, non_uniform_scale) in &mut query.iter() {
*local = LocalTransform(Mat4::from_scale_rotation_translation(
non_uniform_scale.0,
rotation.0,
Expand All @@ -128,7 +128,7 @@ pub fn local_transform_translation_rotation_scale_system(
Without<NonUniformScale, (&mut LocalTransform, &Translation, &Rotation, &Scale)>,
>,
) {
for (mut local, translation, rotation, scale) in query.iter() {
for (mut local, translation, rotation, scale) in &mut query.iter() {
*local = LocalTransform(Mat4::from_scale_rotation_translation(
Vec3::new(scale.0, scale.0, scale.0),
rotation.0,
Expand All @@ -150,7 +150,7 @@ pub fn local_transform_translation_rotation_non_uniform_scale_system(
>,
>,
) {
for (mut local, translation, rotation, non_uniform_scale) in query.iter() {
for (mut local, translation, rotation, non_uniform_scale) in &mut query.iter() {
*local = LocalTransform(Mat4::from_scale_rotation_translation(
non_uniform_scale.0,
rotation.0,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_transform/src/transform_propagate_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn transform_propagate_system(
mut children_query: Query<&Children>,
mut local_transform_query: Query<&LocalTransform>,
) {
for (children, mut transform, local_transform) in root_query.iter() {
for (children, mut transform, local_transform) in &mut root_query.iter() {
if let Some(local_transform) = local_transform {
transform.value = local_transform.0;
}
Expand Down
Loading

0 comments on commit 0c2e26d

Please sign in to comment.