From 44fe0fd9d30453fe91f47b1eb8ec9caa0548f8d0 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sun, 3 Jan 2021 11:45:28 -0800 Subject: [PATCH 01/24] Bevy ECS V2 --- benches/Cargo.toml | 11 +- benches/benches/bevy_ecs/bench.rs | 94 -- benches/benches/bevy_ecs/stages.rs | 59 +- benches/benches/bevy_tasks/iter.rs | 2 +- crates/bevy_app/Cargo.toml | 2 + crates/bevy_app/src/app.rs | 9 +- crates/bevy_app/src/app_builder.rs | 68 +- crates/bevy_app/src/event.rs | 11 +- crates/bevy_app/src/lib.rs | 2 +- crates/bevy_app/src/schedule_runner.rs | 14 +- crates/bevy_asset/src/asset_server.rs | 2 +- crates/bevy_asset/src/assets.rs | 19 +- .../asset_count_diagnostics_plugin.rs | 2 +- crates/bevy_asset/src/handle.rs | 3 +- crates/bevy_asset/src/io/android_asset_io.rs | 2 +- crates/bevy_asset/src/io/file_asset_io.rs | 4 +- crates/bevy_asset/src/io/mod.rs | 2 +- crates/bevy_asset/src/io/wasm_asset_io.rs | 2 +- crates/bevy_asset/src/lib.rs | 25 +- crates/bevy_asset/src/loader.rs | 13 +- crates/bevy_audio/src/audio_output.rs | 13 +- crates/bevy_audio/src/lib.rs | 2 +- crates/bevy_core/src/label.rs | 114 +- crates/bevy_core/src/lib.rs | 50 +- crates/bevy_core/src/name.rs | 3 +- crates/bevy_core/src/task_pool_options.rs | 16 +- crates/bevy_core/src/time/fixed_timestep.rs | 143 +- crates/bevy_core/src/time/time.rs | 2 +- crates/bevy_core/src/time/timer.rs | 3 +- .../src/entity_count_diagnostics_plugin.rs | 15 +- .../src/frame_time_diagnostics_plugin.rs | 2 +- .../src/log_diagnostics_plugin.rs | 2 +- crates/bevy_ecs/Cargo.toml | 20 +- crates/bevy_ecs/LICENSE_APACHE | 202 --- crates/bevy_ecs/LICENSE_MIT | 202 --- crates/bevy_ecs/README.md | 7 - crates/bevy_ecs/macros/Cargo.toml | 7 +- crates/bevy_ecs/macros/src/lib.rs | 525 ++++---- crates/bevy_ecs/src/archetype.rs | 522 ++++++++ crates/bevy_ecs/src/bundle.rs | 233 ++++ crates/bevy_ecs/src/component/mod.rs | 296 +++++ crates/bevy_ecs/src/component/type_info.rs | 63 + crates/bevy_ecs/src/core/access.rs | 472 ------- crates/bevy_ecs/src/core/archetype.rs | 557 -------- crates/bevy_ecs/src/core/borrow.rs | 222 ---- crates/bevy_ecs/src/core/bundle.rs | 151 --- crates/bevy_ecs/src/core/entities.rs | 386 ------ crates/bevy_ecs/src/core/entity_builder.rs | 188 --- crates/bevy_ecs/src/core/filter.rs | 272 ---- crates/bevy_ecs/src/core/mod.rs | 64 - crates/bevy_ecs/src/core/query.rs | 775 ----------- crates/bevy_ecs/src/core/world.rs | 1150 ---------------- crates/bevy_ecs/src/core/world_builder.rs | 78 -- .../entity_map.rs => entity/map_entities.rs} | 2 +- crates/bevy_ecs/src/entity/mod.rs | 477 +++++++ crates/bevy_ecs/src/{core => entity}/serde.rs | 4 +- crates/bevy_ecs/src/lib.rs | 1165 ++++++++++++++++- crates/bevy_ecs/src/query/access.rs | 199 +++ crates/bevy_ecs/src/query/fetch.rs | 813 ++++++++++++ crates/bevy_ecs/src/query/filter.rs | 599 +++++++++ crates/bevy_ecs/src/query/iter.rs | 131 ++ crates/bevy_ecs/src/query/mod.rs | 60 + crates/bevy_ecs/src/query/state.rs | 420 ++++++ crates/bevy_ecs/src/reflect.rs | 165 +++ crates/bevy_ecs/src/resource/mod.rs | 5 - .../bevy_ecs/src/resource/resource_query.rs | 221 ---- crates/bevy_ecs/src/resource/resources.rs | 593 --------- crates/bevy_ecs/src/schedule/executor.rs | 23 +- .../src/schedule/executor_parallel.rs | 243 ++-- crates/bevy_ecs/src/schedule/label.rs | 2 + crates/bevy_ecs/src/schedule/mod.rs | 72 +- crates/bevy_ecs/src/schedule/stage.rs | 379 +++--- crates/bevy_ecs/src/schedule/state.rs | 20 +- .../bevy_ecs/src/schedule/system_container.rs | 14 +- .../src/schedule/system_descriptor.rs | 4 +- crates/bevy_ecs/src/schedule/system_set.rs | 5 +- crates/bevy_ecs/src/storage/blob_vec.rs | 376 ++++++ crates/bevy_ecs/src/storage/mod.rs | 13 + crates/bevy_ecs/src/storage/sparse_set.rs | 499 +++++++ crates/bevy_ecs/src/storage/table.rs | 499 +++++++ crates/bevy_ecs/src/system/commands.rs | 533 ++++---- .../bevy_ecs/src/system/exclusive_system.rs | 71 +- crates/bevy_ecs/src/system/into_system.rs | 672 ++-------- crates/bevy_ecs/src/system/mod.rs | 402 ++++++ crates/bevy_ecs/src/system/query.rs | 228 ++++ crates/bevy_ecs/src/system/query/mod.rs | 232 ---- crates/bevy_ecs/src/system/query/query_set.rs | 31 - crates/bevy_ecs/src/system/system.rs | 44 +- crates/bevy_ecs/src/system/system_chaining.rs | 70 +- crates/bevy_ecs/src/system/system_param.rs | 856 ++++++++---- crates/bevy_ecs/src/tests/tests.rs | 391 ------ crates/bevy_ecs/src/world/entity_ref.rs | 889 +++++++++++++ crates/bevy_ecs/src/world/mod.rs | 819 ++++++++++++ crates/bevy_ecs/src/world/pointer.rs | 49 + crates/bevy_ecs/src/world/spawn_batch.rs | 126 ++ crates/bevy_ecs/src/world/world_cell.rs | 355 +++++ crates/bevy_gilrs/src/gilrs_system.rs | 16 +- crates/bevy_gilrs/src/lib.rs | 4 +- crates/bevy_gltf/src/loader.rs | 12 +- crates/bevy_input/src/gamepad.rs | 2 +- crates/bevy_input/src/keyboard.rs | 6 +- crates/bevy_input/src/lib.rs | 2 +- crates/bevy_input/src/mouse.rs | 2 +- crates/bevy_input/src/system.rs | 2 +- crates/bevy_input/src/touch.rs | 2 +- crates/bevy_internal/src/default_plugins.rs | 2 - crates/bevy_log/src/lib.rs | 6 +- crates/bevy_pbr/src/entity.rs | 2 +- crates/bevy_pbr/src/lib.rs | 13 +- crates/bevy_pbr/src/light.rs | 3 +- .../bevy_pbr/src/render_graph/lights_node.rs | 16 +- crates/bevy_pbr/src/render_graph/mod.rs | 55 +- crates/bevy_reflect/Cargo.toml | 4 +- crates/bevy_reflect/src/impls/bevy_app.rs | 63 - crates/bevy_reflect/src/impls/bevy_ecs.rs | 245 ---- crates/bevy_reflect/src/lib.rs | 12 - .../bevy_render/src/camera/active_cameras.rs | 5 +- crates/bevy_render/src/camera/camera.rs | 10 +- crates/bevy_render/src/camera/projection.rs | 3 +- .../src/camera/visible_entities.rs | 4 +- crates/bevy_render/src/draw.rs | 7 +- crates/bevy_render/src/entity.rs | 2 +- crates/bevy_render/src/lib.rs | 25 +- crates/bevy_render/src/mesh/mesh.rs | 7 +- .../src/pipeline/render_pipelines.rs | 7 +- crates/bevy_render/src/render_graph/base.rs | 215 +-- .../bevy_render/src/render_graph/command.rs | 2 +- crates/bevy_render/src/render_graph/graph.rs | 19 +- crates/bevy_render/src/render_graph/node.rs | 9 +- .../src/render_graph/nodes/camera_node.rs | 17 +- .../src/render_graph/nodes/pass_node.rs | 37 +- .../nodes/render_resources_node.rs | 51 +- .../render_graph/nodes/shared_buffers_node.rs | 9 +- .../render_graph/nodes/texture_copy_node.rs | 9 +- .../nodes/window_swapchain_node.rs | 11 +- .../render_graph/nodes/window_texture_node.rs | 11 +- .../bevy_render/src/render_graph/schedule.rs | 3 +- crates/bevy_render/src/render_graph/system.rs | 15 +- .../src/renderer/render_context.rs | 2 +- .../render_resource/shared_buffers.rs | 11 +- crates/bevy_render/src/shader/shader.rs | 2 +- crates/bevy_render/src/shader/shader_defs.rs | 2 +- crates/bevy_render/src/texture/texture.rs | 2 +- crates/bevy_render/src/wireframe/mod.rs | 17 +- crates/bevy_scene/src/command.rs | 18 +- crates/bevy_scene/src/dynamic_scene.rs | 92 +- crates/bevy_scene/src/lib.rs | 5 +- crates/bevy_scene/src/scene.rs | 2 +- crates/bevy_scene/src/scene_loader.rs | 8 +- crates/bevy_scene/src/scene_spawner.rs | 233 ++-- crates/bevy_sprite/src/entity.rs | 2 +- crates/bevy_sprite/src/lib.rs | 22 +- crates/bevy_sprite/src/render/mod.rs | 62 +- crates/bevy_sprite/src/sprite.rs | 2 +- crates/bevy_text/src/lib.rs | 2 +- crates/bevy_text/src/text2d.rs | 7 +- .../bevy_transform/src/components/children.rs | 12 +- .../src/components/global_transform.rs | 3 +- .../bevy_transform/src/components/parent.rs | 28 +- .../src/components/transform.rs | 3 +- .../src/hierarchy/child_builder.rs | 176 ++- .../bevy_transform/src/hierarchy/hierarchy.rs | 96 +- .../hierarchy/hierarchy_maintenance_system.rs | 36 +- crates/bevy_transform/src/hierarchy/mod.rs | 2 - .../src/hierarchy/world_child_builder.rs | 96 -- crates/bevy_transform/src/lib.rs | 6 +- .../src/transform_propagate_system.rs | 35 +- crates/bevy_ui/src/entity.rs | 2 +- crates/bevy_ui/src/flex/convert.rs | 4 +- crates/bevy_ui/src/flex/mod.rs | 19 +- crates/bevy_ui/src/focus.rs | 5 +- crates/bevy_ui/src/lib.rs | 9 +- crates/bevy_ui/src/render/mod.rs | 96 +- crates/bevy_ui/src/ui_node.rs | 7 +- crates/bevy_ui/src/update.rs | 23 +- crates/bevy_ui/src/widget/image.rs | 5 +- crates/bevy_ui/src/widget/text.rs | 6 +- .../wgpu_resource_diagnostics_plugin.rs | 2 +- crates/bevy_wgpu/src/lib.rs | 27 +- .../src/renderer/wgpu_render_context.rs | 2 +- .../renderer/wgpu_render_graph_executor.rs | 19 +- crates/bevy_wgpu/src/wgpu_renderer.rs | 57 +- crates/bevy_window/src/lib.rs | 10 +- crates/bevy_window/src/system.rs | 2 +- crates/bevy_winit/src/converters.rs | 2 +- crates/bevy_winit/src/lib.rs | 97 +- examples/2d/contributors.rs | 12 +- examples/2d/sprite.rs | 2 +- examples/2d/sprite_sheet.rs | 2 +- examples/2d/text2d.rs | 2 +- examples/2d/texture_atlas.rs | 2 +- examples/3d/3d_scene.rs | 2 +- examples/3d/load_gltf.rs | 2 +- examples/3d/msaa.rs | 2 +- examples/3d/orthographic.rs | 2 +- examples/3d/parenting.rs | 2 +- examples/3d/spawner.rs | 2 +- examples/3d/texture.rs | 2 +- examples/3d/update_gltf_scene.rs | 6 +- examples/3d/z_sort_debug.rs | 2 +- examples/android/android.rs | 2 +- examples/app/custom_loop.rs | 2 +- examples/asset/asset_loading.rs | 2 +- examples/asset/custom_asset_io.rs | 6 +- examples/asset/hot_asset_reloading.rs | 2 +- examples/ecs/change_detection.rs | 14 +- examples/ecs/ecs_guide.rs | 24 +- examples/ecs/hierarchy.rs | 4 +- examples/ecs/parallel_query.rs | 24 +- examples/ecs/removal_detection.rs | 13 +- examples/ecs/state.rs | 12 +- examples/ecs/timers.rs | 2 +- examples/game/alien_cake_addict.rs | 12 +- examples/game/breakout.rs | 4 +- examples/ios/src/lib.rs | 2 +- examples/scene/scene.rs | 29 +- examples/shader/array_texture.rs | 4 +- examples/shader/hot_shader_reloading.rs | 2 +- examples/shader/mesh_custom_attribute.rs | 2 +- examples/shader/shader_custom_material.rs | 2 +- examples/shader/shader_defs.rs | 2 +- examples/tools/bevymark.rs | 13 +- examples/ui/button.rs | 8 +- examples/ui/font_atlas_debug.rs | 4 +- examples/ui/text.rs | 2 +- examples/ui/text_debug.rs | 2 +- examples/ui/ui.rs | 2 +- examples/wasm/assets_wasm.rs | 2 +- examples/window/multiple_windows.rs | 2 +- examples/window/scale_factor_override.rs | 2 +- 230 files changed, 12607 insertions(+), 9837 deletions(-) delete mode 100644 benches/benches/bevy_ecs/bench.rs delete mode 100644 crates/bevy_ecs/LICENSE_APACHE delete mode 100644 crates/bevy_ecs/LICENSE_MIT delete mode 100644 crates/bevy_ecs/README.md create mode 100644 crates/bevy_ecs/src/archetype.rs create mode 100644 crates/bevy_ecs/src/bundle.rs create mode 100644 crates/bevy_ecs/src/component/mod.rs create mode 100644 crates/bevy_ecs/src/component/type_info.rs delete mode 100644 crates/bevy_ecs/src/core/access.rs delete mode 100644 crates/bevy_ecs/src/core/archetype.rs delete mode 100644 crates/bevy_ecs/src/core/borrow.rs delete mode 100644 crates/bevy_ecs/src/core/bundle.rs delete mode 100644 crates/bevy_ecs/src/core/entities.rs delete mode 100644 crates/bevy_ecs/src/core/entity_builder.rs delete mode 100644 crates/bevy_ecs/src/core/filter.rs delete mode 100644 crates/bevy_ecs/src/core/mod.rs delete mode 100644 crates/bevy_ecs/src/core/query.rs delete mode 100644 crates/bevy_ecs/src/core/world.rs delete mode 100644 crates/bevy_ecs/src/core/world_builder.rs rename crates/bevy_ecs/src/{core/entity_map.rs => entity/map_entities.rs} (97%) create mode 100644 crates/bevy_ecs/src/entity/mod.rs rename crates/bevy_ecs/src/{core => entity}/serde.rs (94%) create mode 100644 crates/bevy_ecs/src/query/access.rs create mode 100644 crates/bevy_ecs/src/query/fetch.rs create mode 100644 crates/bevy_ecs/src/query/filter.rs create mode 100644 crates/bevy_ecs/src/query/iter.rs create mode 100644 crates/bevy_ecs/src/query/mod.rs create mode 100644 crates/bevy_ecs/src/query/state.rs create mode 100644 crates/bevy_ecs/src/reflect.rs delete mode 100644 crates/bevy_ecs/src/resource/mod.rs delete mode 100644 crates/bevy_ecs/src/resource/resource_query.rs delete mode 100644 crates/bevy_ecs/src/resource/resources.rs create mode 100644 crates/bevy_ecs/src/storage/blob_vec.rs create mode 100644 crates/bevy_ecs/src/storage/mod.rs create mode 100644 crates/bevy_ecs/src/storage/sparse_set.rs create mode 100644 crates/bevy_ecs/src/storage/table.rs create mode 100644 crates/bevy_ecs/src/system/query.rs delete mode 100644 crates/bevy_ecs/src/system/query/mod.rs delete mode 100644 crates/bevy_ecs/src/system/query/query_set.rs delete mode 100644 crates/bevy_ecs/src/tests/tests.rs create mode 100644 crates/bevy_ecs/src/world/entity_ref.rs create mode 100644 crates/bevy_ecs/src/world/mod.rs create mode 100644 crates/bevy_ecs/src/world/pointer.rs create mode 100644 crates/bevy_ecs/src/world/spawn_batch.rs create mode 100644 crates/bevy_ecs/src/world/world_cell.rs delete mode 100644 crates/bevy_reflect/src/impls/bevy_app.rs delete mode 100644 crates/bevy_reflect/src/impls/bevy_ecs.rs delete mode 100644 crates/bevy_transform/src/hierarchy/world_child_builder.rs diff --git a/benches/Cargo.toml b/benches/Cargo.toml index ea51487a66129..ca22ca2f3b835 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -1,19 +1,16 @@ [package] name = "benches" version = "0.1.0" -authors = ["Carter Anderson "] +authors = [ + "Bevy Contributors ", + "Carter Anderson ", +] edition = "2018" [dev-dependencies] criterion = "0.3" bevy = { path = "../" } -[[bench]] -name = "bench" -path = "benches/bevy_ecs/bench.rs" -harness = false -required-features = ["macros"] - [[bench]] name = "system_stage" path = "benches/bevy_ecs/stages.rs" diff --git a/benches/benches/bevy_ecs/bench.rs b/benches/benches/bevy_ecs/bench.rs deleted file mode 100644 index 2af2f4a320140..0000000000000 --- a/benches/benches/bevy_ecs/bench.rs +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright 2019 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// modified by Bevy contributors - -use bencher::{benchmark_group, benchmark_main, Bencher}; -use bevy_ecs::*; - -struct Position(f32); -struct Velocity(f32); - -fn spawn_tuple(b: &mut Bencher) { - let mut world = World::new(); - b.iter(|| { - world.spawn((Position(0.0), Velocity(0.0))); - }); -} - -fn spawn_static(b: &mut Bencher) { - #[derive(Bundle)] - struct Bundle { - pos: Position, - vel: Velocity, - } - - let mut world = World::new(); - b.iter(|| { - world.spawn(Bundle { - pos: Position(0.0), - vel: Velocity(0.0), - }); - }); -} - -fn spawn_batch(b: &mut Bencher) { - #[derive(Bundle)] - struct Bundle { - pos: Position, - vel: Velocity, - } - - let mut world = World::new(); - b.iter(|| { - world - .spawn_batch((0..1_000).map(|_| Bundle { - pos: Position(0.0), - vel: Velocity(0.0), - })) - .for_each(|_| {}); - world.clear(); - }); -} - -fn iterate_100k(b: &mut Bencher) { - let mut world = World::new(); - for i in 0..100_000 { - world.spawn((Position(-(i as f32)), Velocity(i as f32))); - } - b.iter(|| { - for (mut pos, vel) in &mut world.query_mut::<(&mut Position, &Velocity)>() { - pos.0 += vel.0; - } - }) -} - -fn build(b: &mut Bencher) { - let mut world = World::new(); - let mut builder = EntityBuilder::new(); - b.iter(|| { - builder.add(Position(0.0)).add(Velocity(0.0)); - world.spawn(builder.build()); - }); -} - -benchmark_group!( - benches, - spawn_tuple, - spawn_static, - spawn_batch, - iterate_100k, - build -); -benchmark_main!(benches); diff --git a/benches/benches/bevy_ecs/stages.rs b/benches/benches/bevy_ecs/stages.rs index 5668c3dc9d20a..01421622fbc15 100644 --- a/benches/benches/bevy_ecs/stages.rs +++ b/benches/benches/bevy_ecs/stages.rs @@ -1,13 +1,15 @@ -use bevy::ecs::{IntoSystem,World,SystemStage,Resources,Query,Stage}; +use bevy::ecs::{ + world::World, + schedule::{Stage, SystemStage}, + system::{IntoSystem, Query}, +}; use criterion::{criterion_group, criterion_main, Criterion}; criterion_group!(benches, empty_systems, busy_systems, contrived); criterion_main!(benches); -fn run_stage(stage: &mut SystemStage, world: &mut World, resources: &mut Resources) { - // !!NB!! Uncomment next line when running with old executor. - //stage.initialize(world, resources); - stage.run(world, resources); +fn run_stage(stage: &mut SystemStage, world: &mut World) { + stage.run(world); } struct A(f32); @@ -20,7 +22,6 @@ const ENTITY_BUNCH: usize = 5000; fn empty_systems(criterion: &mut Criterion) { let mut world = World::new(); - let mut resources = Resources::default(); let mut group = criterion.benchmark_group("empty_systems"); group.warm_up_time(std::time::Duration::from_millis(500)); group.measurement_time(std::time::Duration::from_secs(3)); @@ -30,10 +31,10 @@ fn empty_systems(criterion: &mut Criterion) { for _ in 0..amount { stage.add_system(empty.system()); } - run_stage(&mut stage, &mut world, &mut resources); + run_stage(&mut stage, &mut world); group.bench_function(&format!("{:03}_systems", amount), |bencher| { bencher.iter(|| { - run_stage(&mut stage, &mut world, &mut resources); + run_stage(&mut stage, &mut world); }); }); } @@ -47,10 +48,10 @@ fn empty_systems(criterion: &mut Criterion) { .add_system(empty.system()) .add_system(empty.system()); } - run_stage(&mut stage, &mut world, &mut resources); + run_stage(&mut stage, &mut world); group.bench_function(&format!("{:03}_systems", 5 * amount), |bencher| { bencher.iter(|| { - run_stage(&mut stage, &mut world, &mut resources); + run_stage(&mut stage, &mut world); }); }); } @@ -59,22 +60,21 @@ fn empty_systems(criterion: &mut Criterion) { fn busy_systems(criterion: &mut Criterion) { fn ab(mut q: Query<(&mut A, &mut B)>) { - for (mut a, mut b) in q.iter_mut() { + q.for_each_mut(|(mut a, mut b)| { std::mem::swap(&mut a.0, &mut b.0); - } + }); } fn cd(mut q: Query<(&mut C, &mut D)>) { - for (mut c, mut d) in q.iter_mut() { + q.for_each_mut(|(mut c, mut d)| { std::mem::swap(&mut c.0, &mut d.0); - } + }); } fn ce(mut q: Query<(&mut C, &mut E)>) { - for (mut c, mut e) in q.iter_mut() { + q.for_each_mut(|(mut c, mut e)| { std::mem::swap(&mut c.0, &mut e.0); - } + }); } let mut world = World::new(); - let mut resources = Resources::default(); let mut group = criterion.benchmark_group("busy_systems"); group.warm_up_time(std::time::Duration::from_millis(500)); group.measurement_time(std::time::Duration::from_secs(3)); @@ -95,7 +95,7 @@ fn busy_systems(criterion: &mut Criterion) { .add_system(cd.system()) .add_system(ce.system()); } - run_stage(&mut stage, &mut world, &mut resources); + run_stage(&mut stage, &mut world); group.bench_function( &format!( "{:02}x_entities_{:02}_systems", @@ -104,7 +104,7 @@ fn busy_systems(criterion: &mut Criterion) { ), |bencher| { bencher.iter(|| { - run_stage(&mut stage, &mut world, &mut resources); + run_stage(&mut stage, &mut world); }); }, ); @@ -115,25 +115,24 @@ fn busy_systems(criterion: &mut Criterion) { fn contrived(criterion: &mut Criterion) { fn s_0(mut q_0: Query<(&mut A, &mut B)>) { - for (mut c_0, mut c_1) in q_0.iter_mut() { + q_0.for_each_mut(|(mut c_0, mut c_1)| { std::mem::swap(&mut c_0.0, &mut c_1.0); - } + }); } fn s_1(mut q_0: Query<(&mut A, &mut C)>, mut q_1: Query<(&mut B, &mut D)>) { - for (mut c_0, mut c_1) in q_0.iter_mut() { + q_0.for_each_mut(|(mut c_0, mut c_1)| { std::mem::swap(&mut c_0.0, &mut c_1.0); - } - for (mut c_0, mut c_1) in q_1.iter_mut() { + }); + q_1.for_each_mut(|(mut c_0, mut c_1)| { std::mem::swap(&mut c_0.0, &mut c_1.0); - } + }); } fn s_2(mut q_0: Query<(&mut C, &mut D)>) { - for (mut c_0, mut c_1) in q_0.iter_mut() { + q_0.for_each_mut(|(mut c_0, mut c_1)| { std::mem::swap(&mut c_0.0, &mut c_1.0); - } + }); } let mut world = World::new(); - let mut resources = Resources::default(); let mut group = criterion.benchmark_group("contrived"); group.warm_up_time(std::time::Duration::from_millis(500)); group.measurement_time(std::time::Duration::from_secs(3)); @@ -153,7 +152,7 @@ fn contrived(criterion: &mut Criterion) { .add_system(s_1.system()) .add_system(s_2.system()); } - run_stage(&mut stage, &mut world, &mut resources); + run_stage(&mut stage, &mut world); group.bench_function( &format!( "{:02}x_entities_{:02}_systems", @@ -162,7 +161,7 @@ fn contrived(criterion: &mut Criterion) { ), |bencher| { bencher.iter(|| { - run_stage(&mut stage, &mut world, &mut resources); + run_stage(&mut stage, &mut world); }); }, ); diff --git a/benches/benches/bevy_tasks/iter.rs b/benches/benches/bevy_tasks/iter.rs index 2c58adfc2708b..aafa4b6f5b3db 100644 --- a/benches/benches/bevy_tasks/iter.rs +++ b/benches/benches/bevy_tasks/iter.rs @@ -26,7 +26,7 @@ where } fn bench_overhead(c: &mut Criterion) { - fn noop(_: &mut usize) {}; + fn noop(_: &mut usize) {} let mut v = (0..10000).collect::>(); c.bench_function("overhead_iter", |b| { diff --git a/crates/bevy_app/Cargo.toml b/crates/bevy_app/Cargo.toml index 52a5b3030c1f0..be1b36a05fc73 100644 --- a/crates/bevy_app/Cargo.toml +++ b/crates/bevy_app/Cargo.toml @@ -14,11 +14,13 @@ keywords = ["bevy"] [features] trace = [] +default = ["bevy_reflect"] [dependencies] # bevy bevy_derive = { path = "../bevy_derive", version = "0.4.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.4.0" } +bevy_reflect = { path = "../bevy_reflect", version = "0.4.0", optional = true } bevy_utils = { path = "../bevy_utils", version = "0.4.0" } # other diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 2b2e1a2a0ce44..fb56ad03ca281 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -1,5 +1,8 @@ use crate::app_builder::AppBuilder; -use bevy_ecs::{Resources, Schedule, Stage, World}; +use bevy_ecs::{ + schedule::{Schedule, Stage}, + world::World, +}; #[cfg(feature = "trace")] use bevy_utils::tracing::info_span; @@ -27,7 +30,6 @@ use bevy_utils::tracing::info_span; /// ``` pub struct App { pub world: World, - pub resources: Resources, pub runner: Box, pub schedule: Schedule, } @@ -36,7 +38,6 @@ impl Default for App { fn default() -> Self { Self { world: Default::default(), - resources: Default::default(), schedule: Default::default(), runner: Box::new(run_once), } @@ -53,7 +54,7 @@ impl App { } pub fn update(&mut self) { - self.schedule.run(&mut self.world, &mut self.resources); + self.schedule.run(&mut self.world); } pub fn run(mut self) { diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index 46f3d77c5b149..cf64b6a4eb263 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -5,9 +5,12 @@ use crate::{ CoreStage, PluginGroup, PluginGroupBuilder, StartupStage, }; use bevy_ecs::{ - clear_trackers_system, FromResources, IntoExclusiveSystem, IntoSystem, Resource, Resources, - RunOnce, Schedule, Stage, StageLabel, StateStage, SystemDescriptor, SystemSet, SystemStage, - World, + component::Component, + schedule::{ + RunOnce, Schedule, Stage, StageLabel, StateStage, SystemDescriptor, SystemSet, SystemStage, + }, + system::{IntoExclusiveSystem, IntoSystem}, + world::{FromWorld, World}, }; use bevy_utils::tracing::debug; @@ -22,10 +25,13 @@ impl Default for AppBuilder { app: App::default(), }; + #[cfg(feature = "bevy_reflect")] + app_builder.init_resource::(); + app_builder .add_default_stages() .add_event::() - .add_system_to_stage(CoreStage::Last, clear_trackers_system.exclusive_system()); + .add_system_to_stage(CoreStage::Last, World::clear_trackers.exclusive_system()); app_builder } } @@ -37,17 +43,17 @@ impl AppBuilder { } } - pub fn resources(&self) -> &Resources { - &self.app.resources + pub fn run(&mut self) { + let app = std::mem::take(&mut self.app); + app.run(); } - pub fn resources_mut(&mut self) -> &mut Resources { - &mut self.app.resources + pub fn world(&mut self) -> &World { + &self.app.world } - pub fn run(&mut self) { - let app = std::mem::take(&mut self.app); - app.run(); + pub fn world_mut(&mut self) -> &mut World { + &mut self.app.world } pub fn set_world(&mut self, world: World) -> &mut Self { @@ -171,7 +177,7 @@ impl AppBuilder { self } - pub fn on_state_enter( + pub fn on_state_enter( &mut self, stage: impl StageLabel, state: T, @@ -182,7 +188,7 @@ impl AppBuilder { }) } - pub fn on_state_update( + pub fn on_state_update( &mut self, stage: impl StageLabel, state: T, @@ -193,7 +199,7 @@ impl AppBuilder { }) } - pub fn on_state_exit( + pub fn on_state_exit( &mut self, stage: impl StageLabel, state: T, @@ -224,7 +230,7 @@ impl AppBuilder { pub fn add_event(&mut self) -> &mut Self where - T: Send + Sync + 'static, + T: Component, { self.insert_resource(Events::::default()) .add_system_to_stage(CoreStage::Event, Events::::update_system.system()) @@ -233,9 +239,9 @@ impl AppBuilder { /// Inserts a resource to the current [App] and overwrites any resource previously added of the same type. pub fn insert_resource(&mut self, resource: T) -> &mut Self where - T: Send + Sync + 'static, + T: Component, { - self.app.resources.insert(resource); + self.app.world.insert_resource(resource); self } @@ -243,19 +249,19 @@ impl AppBuilder { where T: 'static, { - self.app.resources.insert_non_send(resource); + self.app.world.insert_non_send(resource); self } pub fn init_resource(&mut self) -> &mut Self where - R: FromResources + Send + Sync + 'static, + R: FromWorld + Send + Sync + 'static, { // PERF: We could avoid double hashing here, since the `from_resources` call is guaranteed not to // modify the map. However, we would need to be borrowing resources both mutably and immutably, // so we would need to be extremely certain this is correct - if !self.resources().contains::() { - let resource = R::from_resources(&self.resources()); + if !self.world_mut().contains_resource::() { + let resource = R::from_world(self.world_mut()); self.insert_resource(resource); } self @@ -263,12 +269,12 @@ impl AppBuilder { pub fn init_non_send_resource(&mut self) -> &mut Self where - R: FromResources + 'static, + R: FromWorld + 'static, { // See perf comment in init_resource - if self.app.resources.get_non_send::().is_none() { - let resource = R::from_resources(&self.app.resources); - self.app.resources.insert_non_send(resource); + if self.app.world.get_non_send_resource::().is_none() { + let resource = R::from_world(self.world_mut()); + self.app.world.insert_non_send(resource); } self } @@ -305,4 +311,16 @@ impl AppBuilder { plugin_group_builder.finish(self); self } + + #[cfg(feature = "bevy_reflect")] + pub fn register_type(&mut self) -> &mut Self { + { + let registry = self + .world_mut() + .get_resource_mut::() + .unwrap(); + registry.write().register::(); + } + self + } } diff --git a/crates/bevy_app/src/event.rs b/crates/bevy_app/src/event.rs index ddc8fd2271eb5..4e19ef6acdf44 100644 --- a/crates/bevy_app/src/event.rs +++ b/crates/bevy_app/src/event.rs @@ -1,4 +1,7 @@ -use bevy_ecs::{Local, Res, ResMut, SystemParam}; +use bevy_ecs::{ + component::Component, + system::{Local, Res, ResMut, SystemParam}, +}; use bevy_utils::tracing::trace; use std::{fmt, marker::PhantomData}; @@ -123,7 +126,7 @@ fn map_instance_event(event_instance: &EventInstance) -> &T { /// Reads events of type `T` in order and tracks which events have already been read. #[derive(SystemParam)] -pub struct EventReader<'a, T: bevy_ecs::Resource> { +pub struct EventReader<'a, T: Component> { last_event_count: Local<'a, (usize, PhantomData)>, events: Res<'a, Events>, } @@ -207,7 +210,7 @@ fn internal_event_reader<'a, T>( } } -impl<'a, T: bevy_ecs::Resource> EventReader<'a, T> { +impl<'a, T: Component> EventReader<'a, T> { /// Iterates over the events this EventReader has not seen yet. This updates the EventReader's /// event counter, which means subsequent event reads will not include events that happened before now. pub fn iter(&mut self) -> impl DoubleEndedIterator { @@ -223,7 +226,7 @@ impl<'a, T: bevy_ecs::Resource> EventReader<'a, T> { } } -impl Events { +impl Events { /// "Sends" an `event` by writing it to the current event buffer. [EventReader]s can then read the event. pub fn send(&mut self, event: T) { let event_id = EventId { diff --git a/crates/bevy_app/src/lib.rs b/crates/bevy_app/src/lib.rs index e278ae119d9f2..8224120e01d52 100644 --- a/crates/bevy_app/src/lib.rs +++ b/crates/bevy_app/src/lib.rs @@ -22,7 +22,7 @@ pub mod prelude { }; } -use bevy_ecs::StageLabel; +use bevy_ecs::schedule::StageLabel; /// The names of the default App stages #[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)] diff --git a/crates/bevy_app/src/schedule_runner.rs b/crates/bevy_app/src/schedule_runner.rs index e3855b2302561..69dde8e477e9b 100644 --- a/crates/bevy_app/src/schedule_runner.rs +++ b/crates/bevy_app/src/schedule_runner.rs @@ -41,15 +41,15 @@ impl ScheduleRunnerSettings { } } -/// Configures an App to run its [Schedule](bevy_ecs::Schedule) according to a given [RunMode] +/// Configures an App to run its [Schedule](bevy_ecs::schedule::Schedule) according to a given [RunMode] #[derive(Default)] pub struct ScheduleRunnerPlugin {} impl Plugin for ScheduleRunnerPlugin { fn build(&self, app: &mut AppBuilder) { let settings = app - .resources_mut() - .get_or_insert_with(ScheduleRunnerSettings::default) + .world_mut() + .get_resource_or_insert_with(ScheduleRunnerSettings::default) .to_owned(); app.set_runner(move |mut app: App| { let mut app_exit_event_reader = ManualEventReader::::default(); @@ -63,7 +63,9 @@ impl Plugin for ScheduleRunnerPlugin { -> Result, AppExit> { let start_time = Instant::now(); - if let Some(app_exit_events) = app.resources.get_mut::>() { + if let Some(app_exit_events) = + app.world.get_resource_mut::>() + { if let Some(exit) = app_exit_event_reader.iter(&app_exit_events).last() { return Err(exit.clone()); @@ -72,7 +74,9 @@ impl Plugin for ScheduleRunnerPlugin { app.update(); - if let Some(app_exit_events) = app.resources.get_mut::>() { + if let Some(app_exit_events) = + app.world.get_resource_mut::>() + { if let Some(exit) = app_exit_event_reader.iter(&app_exit_events).last() { return Err(exit.clone()); diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 8da6ae34edf22..7d58a10a6f3a8 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -5,7 +5,7 @@ use crate::{ RefChange, RefChangeChannel, SourceInfo, SourceMeta, }; use anyhow::Result; -use bevy_ecs::Res; +use bevy_ecs::system::Res; use bevy_log::warn; use bevy_tasks::TaskPool; use bevy_utils::{HashMap, Uuid}; diff --git a/crates/bevy_asset/src/assets.rs b/crates/bevy_asset/src/assets.rs index ba28afb781d34..eb69de5abaf89 100644 --- a/crates/bevy_asset/src/assets.rs +++ b/crates/bevy_asset/src/assets.rs @@ -3,8 +3,10 @@ use crate::{ RefChange, }; use bevy_app::{prelude::Events, AppBuilder}; -use bevy_ecs::{FromResources, IntoSystem, ResMut}; -use bevy_reflect::RegisterTypeBuilder; +use bevy_ecs::{ + system::{IntoSystem, ResMut}, + world::FromWorld, +}; use bevy_utils::HashMap; use crossbeam_channel::Sender; use std::fmt::Debug; @@ -202,7 +204,7 @@ pub trait AddAsset { T: Asset; fn init_asset_loader(&mut self) -> &mut Self where - T: AssetLoader + FromResources; + T: AssetLoader + FromWorld; fn add_asset_loader(&mut self, loader: T) -> &mut Self where T: AssetLoader; @@ -214,7 +216,7 @@ impl AddAsset for AppBuilder { T: Asset, { let assets = { - let asset_server = self.resources().get::().unwrap(); + let asset_server = self.world().get_resource::().unwrap(); asset_server.register_asset_type::() }; @@ -233,17 +235,18 @@ impl AddAsset for AppBuilder { fn init_asset_loader(&mut self) -> &mut Self where - T: AssetLoader + FromResources, + T: AssetLoader + FromWorld, { - self.add_asset_loader(T::from_resources(self.resources())) + let result = T::from_world(self.world_mut()); + self.add_asset_loader(result) } fn add_asset_loader(&mut self, loader: T) -> &mut Self where T: AssetLoader, { - self.resources() - .get_mut::() + self.world_mut() + .get_resource_mut::() .expect("AssetServer does not exist. Consider adding it as a resource.") .add_loader(loader); self diff --git a/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs b/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs index fb0bd6c5e63ff..1fcf5e2ae1e0c 100644 --- a/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs +++ b/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs @@ -1,7 +1,7 @@ use crate::{Asset, Assets}; use bevy_app::prelude::*; use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics}; -use bevy_ecs::{IntoSystem, Res, ResMut}; +use bevy_ecs::system::{IntoSystem, Res, ResMut}; /// Adds "asset count" diagnostic to an App #[derive(Default)] diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index ad39e3438a69b..c33fcce4f3f0b 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -9,7 +9,8 @@ use crate::{ path::{AssetPath, AssetPathId}, Asset, Assets, }; -use bevy_reflect::{Reflect, ReflectComponent, ReflectDeserialize}; +use bevy_ecs::reflect::ReflectComponent; +use bevy_reflect::{Reflect, ReflectDeserialize}; use bevy_utils::Uuid; use crossbeam_channel::{Receiver, Sender}; use serde::{Deserialize, Serialize}; diff --git a/crates/bevy_asset/src/io/android_asset_io.rs b/crates/bevy_asset/src/io/android_asset_io.rs index dbb44b4f9c2af..91aa3770e1d5a 100644 --- a/crates/bevy_asset/src/io/android_asset_io.rs +++ b/crates/bevy_asset/src/io/android_asset_io.rs @@ -1,6 +1,6 @@ use crate::{AssetIo, AssetIoError}; use anyhow::Result; -use bevy_ecs::bevy_utils::BoxedFuture; +use bevy_utils::BoxedFuture; use std::{ ffi::CString, path::{Path, PathBuf}, diff --git a/crates/bevy_asset/src/io/file_asset_io.rs b/crates/bevy_asset/src/io/file_asset_io.rs index 0a06d66888e2d..bab6372651ff0 100644 --- a/crates/bevy_asset/src/io/file_asset_io.rs +++ b/crates/bevy_asset/src/io/file_asset_io.rs @@ -1,7 +1,7 @@ use crate::{filesystem_watcher::FilesystemWatcher, AssetIo, AssetIoError, AssetServer}; use anyhow::Result; -use bevy_ecs::{bevy_utils::BoxedFuture, Res}; -use bevy_utils::HashSet; +use bevy_ecs::system::Res; +use bevy_utils::{BoxedFuture, HashSet}; use crossbeam_channel::TryRecvError; use fs::File; use io::Read; diff --git a/crates/bevy_asset/src/io/mod.rs b/crates/bevy_asset/src/io/mod.rs index d7917972a04a6..a40b7b2d013b6 100644 --- a/crates/bevy_asset/src/io/mod.rs +++ b/crates/bevy_asset/src/io/mod.rs @@ -13,7 +13,7 @@ pub use file_asset_io::*; pub use wasm_asset_io::*; use anyhow::Result; -use bevy_ecs::bevy_utils::BoxedFuture; +use bevy_utils::BoxedFuture; use downcast_rs::{impl_downcast, Downcast}; use std::{ io, diff --git a/crates/bevy_asset/src/io/wasm_asset_io.rs b/crates/bevy_asset/src/io/wasm_asset_io.rs index ecac806744194..3e66981edcc1b 100644 --- a/crates/bevy_asset/src/io/wasm_asset_io.rs +++ b/crates/bevy_asset/src/io/wasm_asset_io.rs @@ -1,6 +1,6 @@ use crate::{AssetIo, AssetIoError}; use anyhow::Result; -use bevy_ecs::bevy_utils::BoxedFuture; +use bevy_utils::BoxedFuture; use js_sys::Uint8Array; use std::path::{Path, PathBuf}; use wasm_bindgen::JsCast; diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 3208a5efbee40..4b2214a5afda6 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -12,21 +12,24 @@ mod io; mod loader; mod path; +pub mod prelude { + pub use crate::{AddAsset, AssetEvent, AssetServer, Assets, Handle, HandleUntyped}; +} + pub use asset_server::*; pub use assets::*; +pub use bevy_utils::BoxedFuture; pub use handle::*; pub use info::*; pub use io::*; pub use loader::*; pub use path::*; -pub mod prelude { - pub use crate::{AddAsset, AssetEvent, AssetServer, Assets, Handle, HandleUntyped}; -} - use bevy_app::{prelude::Plugin, AppBuilder}; -use bevy_ecs::{IntoSystem, StageLabel, SystemStage}; -use bevy_reflect::RegisterTypeBuilder; +use bevy_ecs::{ + schedule::{StageLabel, SystemStage}, + system::IntoSystem, +}; use bevy_tasks::IoTaskPool; /// The names of asset stages in an App Schedule @@ -59,8 +62,8 @@ impl Default for AssetServerSettings { /// delegate to the default `AssetIo` for the platform. pub fn create_platform_default_asset_io(app: &mut AppBuilder) -> Box { let settings = app - .resources_mut() - .get_or_insert_with(AssetServerSettings::default); + .world_mut() + .get_resource_or_insert_with(AssetServerSettings::default); #[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))] let source = FileAssetIo::new(&settings.asset_folder); @@ -74,10 +77,10 @@ pub fn create_platform_default_asset_io(app: &mut AppBuilder) -> Box().is_none() { + if app.world().get_resource::().is_none() { let task_pool = app - .resources() - .get::() + .world() + .get_resource::() .expect("`IoTaskPool` resource not found.") .0 .clone(); diff --git a/crates/bevy_asset/src/loader.rs b/crates/bevy_asset/src/loader.rs index f405b6122e98c..5344d13e1e769 100644 --- a/crates/bevy_asset/src/loader.rs +++ b/crates/bevy_asset/src/loader.rs @@ -3,7 +3,10 @@ use crate::{ RefChangeChannel, }; use anyhow::Result; -use bevy_ecs::{Res, ResMut, Resource}; +use bevy_ecs::{ + component::Component, + system::{Res, ResMut}, +}; use bevy_reflect::{TypeUuid, TypeUuidDynamic}; use bevy_utils::{BoxedFuture, HashMap}; use crossbeam_channel::{Receiver, Sender}; @@ -136,7 +139,7 @@ impl<'a> LoadContext<'a> { /// The result of loading an asset of type `T` #[derive(Debug)] -pub struct AssetResult { +pub struct AssetResult { pub asset: T, pub id: HandleId, pub version: usize, @@ -144,12 +147,12 @@ pub struct AssetResult { /// A channel to send and receive [AssetResult]s #[derive(Debug)] -pub struct AssetLifecycleChannel { +pub struct AssetLifecycleChannel { pub sender: Sender>, pub receiver: Receiver>, } -pub enum AssetLifecycleEvent { +pub enum AssetLifecycleEvent { Create(AssetResult), Free(HandleId), } @@ -183,7 +186,7 @@ impl AssetLifecycle for AssetLifecycleChannel { } } -impl Default for AssetLifecycleChannel { +impl Default for AssetLifecycleChannel { fn default() -> Self { let (sender, receiver) = crossbeam_channel::unbounded(); AssetLifecycleChannel { sender, receiver } diff --git a/crates/bevy_audio/src/audio_output.rs b/crates/bevy_audio/src/audio_output.rs index 2268851bfff1c..075195c50be4f 100644 --- a/crates/bevy_audio/src/audio_output.rs +++ b/crates/bevy_audio/src/audio_output.rs @@ -1,6 +1,6 @@ use crate::{Audio, AudioSource, Decodable}; use bevy_asset::{Asset, Assets}; -use bevy_ecs::{Resources, World}; +use bevy_ecs::world::World; use rodio::{OutputStream, OutputStreamHandle, Sink}; use std::marker::PhantomData; @@ -59,16 +59,17 @@ where } /// Plays audio currently queued in the [Audio] resource through the [AudioOutput] resource -pub fn play_queued_audio_system(_world: &mut World, resources: &mut Resources) +pub fn play_queued_audio_system(world: &mut World) where P: Decodable,

::Decoder: rodio::Source + Send + Sync, <

::Decoder as Iterator>::Item: rodio::Sample + Send + Sync, { - let audio_output = resources.get_non_send::>().unwrap(); - let mut audio = resources.get_mut::>().unwrap(); + let world = world.cell(); + let audio_output = world.get_non_send::>().unwrap(); + let mut audio = world.get_resource_mut::>().unwrap(); - if let Some(audio_sources) = resources.get::>() { + if let Some(audio_sources) = world.get_resource::>() { audio_output.try_play_queued(&*audio_sources, &mut *audio); - } + }; } diff --git a/crates/bevy_audio/src/lib.rs b/crates/bevy_audio/src/lib.rs index c72a43ce02bea..f9f9dfb41f869 100644 --- a/crates/bevy_audio/src/lib.rs +++ b/crates/bevy_audio/src/lib.rs @@ -12,7 +12,7 @@ pub use audio_source::*; use bevy_app::prelude::*; use bevy_asset::AddAsset; -use bevy_ecs::IntoExclusiveSystem; +use bevy_ecs::system::IntoExclusiveSystem; /// Adds support for audio playback to an App #[derive(Default)] diff --git a/crates/bevy_core/src/label.rs b/crates/bevy_core/src/label.rs index 1a70d3220bd3d..9bd5aee9d4fa5 100644 --- a/crates/bevy_core/src/label.rs +++ b/crates/bevy_core/src/label.rs @@ -1,5 +1,10 @@ -use bevy_ecs::prelude::*; -use bevy_reflect::{Reflect, ReflectComponent}; +use bevy_ecs::{ + entity::Entity, + query::Changed, + reflect::ReflectComponent, + system::{Query, RemovedComponents, ResMut}, +}; +use bevy_reflect::Reflect; use bevy_utils::{HashMap, HashSet}; use std::{ borrow::Cow, @@ -74,15 +79,16 @@ impl EntityLabels { pub(crate) fn entity_labels_system( mut entity_labels: ResMut, + removed_labels: RemovedComponents, query: Query<(Entity, &Labels), Changed>, ) { let entity_labels = entity_labels.deref_mut(); - for entity in query.removed::() { - if let Some(labels) = entity_labels.entity_labels.get(entity) { + for entity in removed_labels.iter() { + if let Some(labels) = entity_labels.entity_labels.get(&entity) { for label in labels.iter() { if let Some(entities) = entity_labels.label_entities.get_mut(label) { - entities.retain(|e| e != entity); + entities.retain(|e| *e != entity); } } } @@ -114,17 +120,21 @@ pub(crate) fn entity_labels_system( #[cfg(test)] mod tests { + use bevy_ecs::{ + schedule::{Schedule, Stage, SystemStage}, + system::IntoSystem, + world::World, + }; + use super::*; - use bevy_ecs::Stage; - fn setup() -> (World, Resources, bevy_ecs::Schedule) { - let world = World::new(); - let mut resources = Resources::default(); - resources.insert(EntityLabels::default()); - let mut schedule = bevy_ecs::Schedule::default(); + fn setup() -> (World, Schedule) { + let mut world = World::new(); + world.insert_resource(EntityLabels::default()); + let mut schedule = Schedule::default(); schedule.add_stage("test", SystemStage::single_threaded()); schedule.add_system_to_stage("test", entity_labels_system.system()); - (world, resources, schedule) + (world, schedule) } fn holy_cow() -> Labels { @@ -137,12 +147,12 @@ mod tests { #[test] fn adds_spawned_entity() { - let (mut world, mut resources, mut schedule) = setup(); + let (mut world, mut schedule) = setup(); - let e1 = world.spawn((holy_cow(),)); - schedule.run(&mut world, &mut resources); + let e1 = world.spawn().insert(holy_cow()).id(); + schedule.run(&mut world); - let entity_labels = resources.get::().unwrap(); + let entity_labels = world.get_resource::().unwrap(); assert_eq!(entity_labels.get("holy"), &[e1], "holy"); assert_eq!(entity_labels.get("cow"), &[e1], "cow"); assert_eq!(entity_labels.get("shalau"), &[], "shalau"); @@ -150,14 +160,14 @@ mod tests { #[test] fn add_labels() { - let (mut world, mut resources, mut schedule) = setup(); - let e1 = world.spawn((holy_cow(),)); - schedule.run(&mut world, &mut resources); + let (mut world, mut schedule) = setup(); + let e1 = world.spawn().insert(holy_cow()).id(); + schedule.run(&mut world); world.get_mut::(e1).unwrap().insert("shalau"); - schedule.run(&mut world, &mut resources); + schedule.run(&mut world); - let entity_labels = resources.get::().unwrap(); + let entity_labels = world.get_resource::().unwrap(); assert_eq!(entity_labels.get("holy"), &[e1], "holy"); assert_eq!(entity_labels.get("cow"), &[e1], "cow"); assert_eq!(entity_labels.get("shalau"), &[e1], "shalau"); @@ -165,14 +175,14 @@ mod tests { #[test] fn remove_labels() { - let (mut world, mut resources, mut schedule) = setup(); - let e1 = world.spawn((holy_cow(),)); - schedule.run(&mut world, &mut resources); + let (mut world, mut schedule) = setup(); + let e1 = world.spawn().insert(holy_cow()).id(); + schedule.run(&mut world); world.get_mut::(e1).unwrap().remove("holy"); - schedule.run(&mut world, &mut resources); + schedule.run(&mut world); - let entity_labels = resources.get::().unwrap(); + let entity_labels = world.get_resource::().unwrap(); assert_eq!(entity_labels.get("holy"), &[], "holy"); assert_eq!(entity_labels.get("cow"), &[e1], "cow"); assert_eq!(entity_labels.get("shalau"), &[], "shalau"); @@ -180,14 +190,14 @@ mod tests { #[test] fn removes_despawned_entity() { - let (mut world, mut resources, mut schedule) = setup(); - let e1 = world.spawn((holy_cow(),)); - schedule.run(&mut world, &mut resources); + let (mut world, mut schedule) = setup(); + let e1 = world.spawn().insert(holy_cow()).id(); + schedule.run(&mut world); - world.despawn(e1).unwrap(); - schedule.run(&mut world, &mut resources); + assert!(world.despawn(e1)); + schedule.run(&mut world); - let entity_labels = resources.get::().unwrap(); + let entity_labels = world.get_resource::().unwrap(); assert_eq!(entity_labels.get("holy"), &[], "holy"); assert_eq!(entity_labels.get("cow"), &[], "cow"); assert_eq!(entity_labels.get("shalau"), &[], "shalau"); @@ -195,14 +205,14 @@ mod tests { #[test] fn removes_labels_when_component_removed() { - let (mut world, mut resources, mut schedule) = setup(); - let e1 = world.spawn((holy_cow(),)); - schedule.run(&mut world, &mut resources); + let (mut world, mut schedule) = setup(); + let e1 = world.spawn().insert(holy_cow()).id(); + schedule.run(&mut world); - world.remove_one::(e1).unwrap(); - schedule.run(&mut world, &mut resources); + world.entity_mut(e1).remove::().unwrap(); + schedule.run(&mut world); - let entity_labels = resources.get::().unwrap(); + let entity_labels = world.get_resource::().unwrap(); assert_eq!(entity_labels.get("holy"), &[], "holy"); assert_eq!(entity_labels.get("cow"), &[], "cow"); assert_eq!(entity_labels.get("shalau"), &[], "shalau"); @@ -210,14 +220,14 @@ mod tests { #[test] fn adds_another_spawned_entity() { - let (mut world, mut resources, mut schedule) = setup(); - let e1 = world.spawn((holy_cow(),)); - schedule.run(&mut world, &mut resources); + let (mut world, mut schedule) = setup(); + let e1 = world.spawn().insert(holy_cow()).id(); + schedule.run(&mut world); - let e2 = world.spawn((holy_shamoni(),)); - schedule.run(&mut world, &mut resources); + let e2 = world.spawn().insert(holy_shamoni()).id(); + schedule.run(&mut world); - let entity_labels = resources.get::().unwrap(); + let entity_labels = world.get_resource::().unwrap(); assert_eq!(entity_labels.get("holy"), &[e1, e2], "holy"); assert_eq!(entity_labels.get("cow"), &[e1], "cow"); assert_eq!(entity_labels.get("shamoni"), &[e2], "shamoni"); @@ -226,17 +236,17 @@ mod tests { #[test] fn removes_despawned_entity_but_leaves_other() { - let (mut world, mut resources, mut schedule) = setup(); - let e1 = world.spawn((holy_cow(),)); - schedule.run(&mut world, &mut resources); + let (mut world, mut schedule) = setup(); + let e1 = world.spawn().insert(holy_cow()).id(); + schedule.run(&mut world); - let e2 = world.spawn((holy_shamoni(),)); - schedule.run(&mut world, &mut resources); + let e2 = world.spawn().insert(holy_shamoni()).id(); + schedule.run(&mut world); - world.despawn(e1).unwrap(); - schedule.run(&mut world, &mut resources); + assert!(world.despawn(e1)); + schedule.run(&mut world); - let entity_labels = resources.get::().unwrap(); + let entity_labels = world.get_resource::().unwrap(); assert_eq!(entity_labels.get("holy"), &[e2], "holy"); assert_eq!(entity_labels.get("cow"), &[], "cow"); assert_eq!(entity_labels.get("shamoni"), &[e2], "shamoni"); diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 12c7b1377cc6d..e26be88c1337b 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -5,8 +5,6 @@ mod name; mod task_pool_options; mod time; -use bevy_ecs::IntoSystem; -use bevy_reflect::RegisterTypeBuilder; pub use bytes::*; pub use float_ord::*; pub use label::*; @@ -19,6 +17,7 @@ pub mod prelude { } use bevy_app::prelude::*; +use bevy_ecs::{entity::Entity, system::IntoSystem}; use std::ops::Range; /// Adds core functionality to Apps. @@ -28,15 +27,16 @@ pub struct CorePlugin; impl Plugin for CorePlugin { fn build(&self, app: &mut AppBuilder) { // Setup the default bevy task pools - app.resources_mut() - .get_cloned::() + app.world_mut() + .get_resource::() + .cloned() .unwrap_or_else(DefaultTaskPoolOptions::default) - .create_default_pools(app.resources_mut()); + .create_default_pools(app.world_mut()); app.init_resource::