Skip to content

Commit

Permalink
Implement From<bool> for ShouldRun. (bevyengine#5306)
Browse files Browse the repository at this point in the history
Make writing simple yes/no run criteria easier.


Co-authored-by: devil-ira <justthecooldude@gmail.com>
  • Loading branch information
2 people authored and ItsDoot committed Feb 1, 2023
1 parent 447f0d5 commit 77504f5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
13 changes: 2 additions & 11 deletions benches/benches/bevy_ecs/scheduling/run_criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ pub fn run_criteria_yes_with_query(criterion: &mut Criterion) {
group.measurement_time(std::time::Duration::from_secs(3));
fn empty() {}
fn yes_with_query(query: Query<&TestBool>) -> ShouldRun {
let test_bool = query.single();
if test_bool.0 {
ShouldRun::Yes
} else {
ShouldRun::No
}
query.single().0.into()
}
for amount in 0..21 {
let mut stage = SystemStage::parallel();
Expand Down Expand Up @@ -184,11 +179,7 @@ pub fn run_criteria_yes_with_resource(criterion: &mut Criterion) {
group.measurement_time(std::time::Duration::from_secs(3));
fn empty() {}
fn yes_with_resource(res: Res<TestBool>) -> ShouldRun {
if res.0 {
ShouldRun::Yes
} else {
ShouldRun::No
}
res.0.into()
}
for amount in 0..21 {
let mut stage = SystemStage::parallel();
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_ecs/src/schedule/run_criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ impl ShouldRun {
}
}

impl From<bool> for ShouldRun {
fn from(value: bool) -> Self {
if value {
ShouldRun::Yes
} else {
ShouldRun::No
}
}
}

#[derive(Default)]
pub(crate) struct BoxedRunCriteria {
criteria_system: Option<BoxedSystem<(), ShouldRun>>,
Expand Down
6 changes: 1 addition & 5 deletions examples/ecs/system_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ fn run_for_a_second(time: Res<Time>, mut done: ResMut<Done>) -> ShouldRun {

/// Another run criteria, simply using a resource.
fn is_done(done: Res<Done>) -> ShouldRun {
if done.0 {
ShouldRun::Yes
} else {
ShouldRun::No
}
done.0.into()
}

/// Used with [`RunCritera::pipe`], inverts the result of the
Expand Down

0 comments on commit 77504f5

Please sign in to comment.