Skip to content

Commit

Permalink
update benches, examples, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Mar 31, 2024
1 parent b80a99f commit 2edfdfe
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 42 deletions.
6 changes: 3 additions & 3 deletions benches/benches/bevy_ecs/scheduling/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ pub fn empty_schedule_run(criterion: &mut Criterion) {
let mut schedule = Schedule::default();
schedule.set_executor_kind(bevy_ecs::schedule::ExecutorKind::SingleThreaded);
group.bench_function("SingleThreaded", |bencher| {
bencher.iter(|| schedule.run(&mut app.world));
bencher.iter(|| schedule.run(app.world_mut()));
});

let mut schedule = Schedule::default();
schedule.set_executor_kind(bevy_ecs::schedule::ExecutorKind::MultiThreaded);
group.bench_function("MultiThreaded", |bencher| {
bencher.iter(|| schedule.run(&mut app.world));
bencher.iter(|| schedule.run(app.world_mut()));
});

let mut schedule = Schedule::default();
schedule.set_executor_kind(bevy_ecs::schedule::ExecutorKind::Simple);
group.bench_function("Simple", |bencher| {
bencher.iter(|| schedule.run(&mut app.world));
bencher.iter(|| schedule.run(app.world_mut()));
});
group.finish();
}
2 changes: 1 addition & 1 deletion examples/2d/mesh2d_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ pub const COLORED_MESH2D_SHADER_HANDLE: Handle<Shader> =
impl Plugin for ColoredMesh2dPlugin {
fn build(&self, app: &mut App) {
// Load our custom shader
let mut shaders = app.world.resource_mut::<Assets<Shader>>();
let mut shaders = app.world_mut().resource_mut::<Assets<Shader>>();
shaders.insert(
&COLORED_MESH2D_SHADER_HANDLE,
Shader::from_wgsl(COLORED_MESH2D_SHADER, file!()),
Expand Down
2 changes: 1 addition & 1 deletion examples/app/custom_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn my_runner(mut app: App) {
println!("Type stuff into the console");
for line in io::stdin().lines() {
{
let mut input = app.world.resource_mut::<Input>();
let mut input = app.world_mut().resource_mut::<Input>();
input.0 = line.unwrap();
}
app.update();
Expand Down
4 changes: 2 additions & 2 deletions examples/ecs/custom_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ fn main() {
//
// Note that we modify `MainScheduleOrder` directly in `main` and not in a startup system. The reason for this is
// that the `MainScheduleOrder` cannot be modified from systems that are run as part of the `Main` schedule.
let mut main_schedule_order = app.world.resource_mut::<MainScheduleOrder>();
let mut main_schedule_order = app.world_mut().resource_mut::<MainScheduleOrder>();
main_schedule_order.insert_after(Update, SingleThreadedUpdate);

// Adding a custom startup schedule works similarly, but needs to use `insert_startup_after`
// instead of `insert_after`.
app.add_schedule(Schedule::new(CustomStartup));

let mut main_schedule_order = app.world.resource_mut::<MainScheduleOrder>();
let mut main_schedule_order = app.world_mut().resource_mut::<MainScheduleOrder>();
main_schedule_order.insert_startup_after(PreStartup, CustomStartup);

app.add_systems(SingleThreadedUpdate, single_threaded_update_system)
Expand Down
24 changes: 12 additions & 12 deletions examples/ecs/system_stepping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() {
* Stepping::continue_frame() is called
* System has been configured to always run"#
);
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.add_schedule(Update).enable();
app.update();

Expand All @@ -65,7 +65,7 @@ fn main() {
Stepping, step means run the next system across all the schedules
that have been added to the Stepping resource."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.step_frame();
app.update();

Expand All @@ -89,7 +89,7 @@ fn main() {
case, we previously performed a step, running one system in Update.
This continue will cause all remaining systems in Update to run."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.continue_frame();
app.update();

Expand All @@ -102,7 +102,7 @@ fn main() {
systems."#
);
for _ in 0..4 {
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.step_frame();
app.update();
}
Expand All @@ -116,10 +116,10 @@ fn main() {
execute all systems in the frame. Stepping::always_run() allows
us to granularly allow systems to run when stepping is enabled."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.always_run(Update, update_system_two);
for _ in 0..3 {
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.step_frame();
app.update();
}
Expand All @@ -132,7 +132,7 @@ fn main() {
Stepping::never_run() allows us to disable systems while Stepping
is enabled."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.never_run(Update, update_system_two);
stepping.continue_frame();
app.update();
Expand All @@ -155,14 +155,14 @@ fn main() {
During the final continue pre_update_system() and
update_system_three() run."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.set_breakpoint(Update, update_system_two);
stepping.continue_frame();
app.update();
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.step_frame();
app.update();
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.continue_frame();
app.update();

Expand All @@ -172,7 +172,7 @@ fn main() {
through all systems
Result: All systems will run"#
);
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.clear_breakpoint(Update, update_system_two);
stepping.continue_frame();
app.update();
Expand All @@ -184,7 +184,7 @@ fn main() {
call Stepping::step_frame() or Stepping::continue_frame() to run
systems in the Update schedule."#
);
let mut stepping = app.world.resource_mut::<Stepping>();
let mut stepping = app.world_mut().resource_mut::<Stepping>();
stepping.disable();
app.update();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/games/stepping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Plugin for SteppingPlugin {
// We need an independent schedule so we have access to all other
// schedules through the `Stepping` resource
app.init_schedule(DebugSchedule);
let mut order = app.world.resource_mut::<MainScheduleOrder>();
let mut order = app.world_mut().resource_mut::<MainScheduleOrder>();
order.insert_after(Update, DebugSchedule);

// create our stepping resource
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/compute_shader_game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Plugin for GameOfLifeComputePlugin {
prepare_bind_group.in_set(RenderSet::PrepareBindGroups),
);

let mut render_graph = render_app.world.resource_mut::<RenderGraph>();
let mut render_graph = render_app.world_mut().resource_mut::<RenderGraph>();
render_graph.add_node(GameOfLifeLabel, GameOfLifeNode::default());
render_graph.add_node_edge(GameOfLifeLabel, bevy::render::graph::CameraDriverLabel);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/shader/post_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Plugin for PostProcessPlugin {
));

// We need to get the render app from the main app
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

Expand Down Expand Up @@ -97,7 +97,7 @@ impl Plugin for PostProcessPlugin {

fn finish(&self, app: &mut App) {
// We need to get the render app from the main app
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

Expand Down
4 changes: 2 additions & 2 deletions examples/shader/texture_binding_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ impl Plugin for GpuFeatureSupportChecker {
fn build(&self, _app: &mut App) {}

fn finish(&self, app: &mut App) {
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

let render_device = render_app.world.resource::<RenderDevice>();
let render_device = render_app.world().resource::<RenderDevice>();

// Check if the device support the required feature. If not, exit the example.
// In a real application, you should setup a fallback for the missing feature
Expand Down
2 changes: 1 addition & 1 deletion examples/stress_tests/many_lights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ struct LogVisibleLights;

impl Plugin for LogVisibleLights {
fn build(&self, app: &mut App) {
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

Expand Down
10 changes: 5 additions & 5 deletions examples/time/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ fn runner(mut app: App) {
}
"f" => {
println!("FAST: setting relative speed to 2x");
app.world
app.world_mut()
.resource_mut::<Time<Virtual>>()
.set_relative_speed(2.0);
}
"n" => {
println!("NORMAL: setting relative speed to 1x");
app.world
app.world_mut()
.resource_mut::<Time<Virtual>>()
.set_relative_speed(1.0);
}
"s" => {
println!("SLOW: setting relative speed to 0.5x");
app.world
app.world_mut()
.resource_mut::<Time<Virtual>>()
.set_relative_speed(0.5);
}
"p" => {
println!("PAUSE: pausing virtual clock");
app.world.resource_mut::<Time<Virtual>>().pause();
app.world_mut().resource_mut::<Time<Virtual>>().pause();
}
"u" => {
println!("UNPAUSE: resuming virtual clock");
app.world.resource_mut::<Time<Virtual>>().unpause();
app.world_mut().resource_mut::<Time<Virtual>>().unpause();
}
"q" => {
println!("QUITTING!");
Expand Down
24 changes: 13 additions & 11 deletions tests/how_to_test_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn did_hurt_enemy() {

// Setup test entities
let enemy_id = app
.world
.world_mut()
.spawn(Enemy {
hit_points: 5,
score_value: 3,
Expand All @@ -73,8 +73,8 @@ fn did_hurt_enemy() {
app.update();

// Check resulting changes
assert!(app.world.get::<Enemy>(enemy_id).is_some());
assert_eq!(app.world.get::<Enemy>(enemy_id).unwrap().hit_points, 4);
assert!(app.world().get::<Enemy>(enemy_id).is_some());
assert_eq!(app.world().get::<Enemy>(enemy_id).unwrap().hit_points, 4);
}

#[test]
Expand All @@ -93,7 +93,7 @@ fn did_despawn_enemy() {

// Setup test entities
let enemy_id = app
.world
.world_mut()
.spawn(Enemy {
hit_points: 1,
score_value: 1,
Expand All @@ -104,10 +104,10 @@ fn did_despawn_enemy() {
app.update();

// Check enemy was despawned
assert!(app.world.get::<Enemy>(enemy_id).is_none());
assert!(app.world().get::<Enemy>(enemy_id).is_none());

// Get `EnemyDied` event reader
let enemy_died_events = app.world.resource::<Events<EnemyDied>>();
let enemy_died_events = app.world().resource::<Events<EnemyDied>>();
let mut enemy_died_reader = enemy_died_events.get_reader();
let enemy_died = enemy_died_reader.read(enemy_died_events).next().unwrap();

Expand All @@ -132,16 +132,18 @@ fn spawn_enemy_using_input_resource() {
app.update();

// Check resulting changes, one entity has been spawned with `Enemy` component
assert_eq!(app.world.query::<&Enemy>().iter(&app.world).len(), 1);
assert_eq!(app.world_mut().query::<&Enemy>().iter(app.world()).len(), 1);

// Clear the `just_pressed` status for all `KeyCode`s
app.world.resource_mut::<ButtonInput<KeyCode>>().clear();
app.world_mut()
.resource_mut::<ButtonInput<KeyCode>>()
.clear();

// Run systems
app.update();

// Check resulting changes, no new entity has been spawned
assert_eq!(app.world.query::<&Enemy>().iter(&app.world).len(), 1);
assert_eq!(app.world_mut().query::<&Enemy>().iter(app.world()).len(), 1);
}

#[test]
Expand All @@ -159,13 +161,13 @@ fn update_score_on_event() {
app.add_systems(Update, update_score);

// Send an `EnemyDied` event
app.world
app.world_mut()
.resource_mut::<Events<EnemyDied>>()
.send(EnemyDied(3));

// Run systems
app.update();

// Check resulting changes
assert_eq!(app.world.resource::<Score>().0, 3);
assert_eq!(app.world().resource::<Score>().0, 3);
}

0 comments on commit 2edfdfe

Please sign in to comment.