Skip to content

Commit 9646694

Browse files
tim-blackbirdjames7132
authored andcommitted
Implement From<bool> for ShouldRun. (bevyengine#5306)
Make writing simple yes/no run criteria easier. Co-authored-by: devil-ira <justthecooldude@gmail.com>
1 parent 47d9137 commit 9646694

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

benches/benches/bevy_ecs/scheduling/run_criteria.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,7 @@ pub fn run_criteria_yes_with_query(criterion: &mut Criterion) {
147147
group.measurement_time(std::time::Duration::from_secs(3));
148148
fn empty() {}
149149
fn yes_with_query(query: Query<&TestBool>) -> ShouldRun {
150-
let test_bool = query.single();
151-
if test_bool.0 {
152-
ShouldRun::Yes
153-
} else {
154-
ShouldRun::No
155-
}
150+
query.single().0.into()
156151
}
157152
for amount in 0..21 {
158153
let mut stage = SystemStage::parallel();
@@ -184,11 +179,7 @@ pub fn run_criteria_yes_with_resource(criterion: &mut Criterion) {
184179
group.measurement_time(std::time::Duration::from_secs(3));
185180
fn empty() {}
186181
fn yes_with_resource(res: Res<TestBool>) -> ShouldRun {
187-
if res.0 {
188-
ShouldRun::Yes
189-
} else {
190-
ShouldRun::No
191-
}
182+
res.0.into()
192183
}
193184
for amount in 0..21 {
194185
let mut stage = SystemStage::parallel();

crates/bevy_ecs/src/schedule/run_criteria.rs

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ impl ShouldRun {
5656
}
5757
}
5858

59+
impl From<bool> for ShouldRun {
60+
fn from(value: bool) -> Self {
61+
if value {
62+
ShouldRun::Yes
63+
} else {
64+
ShouldRun::No
65+
}
66+
}
67+
}
68+
5969
#[derive(Default)]
6070
pub(crate) struct BoxedRunCriteria {
6171
criteria_system: Option<BoxedSystem<(), ShouldRun>>,

examples/ecs/system_sets.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ fn run_for_a_second(time: Res<Time>, mut done: ResMut<Done>) -> ShouldRun {
104104

105105
/// Another run criteria, simply using a resource.
106106
fn is_done(done: Res<Done>) -> ShouldRun {
107-
if done.0 {
108-
ShouldRun::Yes
109-
} else {
110-
ShouldRun::No
111-
}
107+
done.0.into()
112108
}
113109

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

0 commit comments

Comments
 (0)