Skip to content

Commit 3d9f97d

Browse files
committed
Accept boxed conditions for run_if, and cleanup IntoScheduleConfigs
1 parent b2f5212 commit 3d9f97d

File tree

3 files changed

+179
-179
lines changed

3 files changed

+179
-179
lines changed

crates/bevy_ecs/src/schedule/condition.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,33 @@ impl<Marker, In: SystemInput, F> SystemCondition<Marker, In> for F where
377377
{
378378
}
379379

380+
/// Conversion trait to turn something into a [`BoxedCondition`].
381+
///
382+
/// This trait is automatically implemented for all types that implement
383+
/// [`SystemCondition`], as well as for [`BoxedCondition`] itself.
384+
pub trait IntoBoxedCondition<Marker, In: SystemInput = ()> {
385+
/// Converts `self` into a boxed run condition.
386+
fn into_boxed_condition(this: Self) -> BoxedCondition<In>;
387+
}
388+
389+
#[doc(hidden)]
390+
pub struct UnboxedSystemMarker;
391+
392+
impl<Marker, In: SystemInput, S> IntoBoxedCondition<(UnboxedSystemMarker, Marker), In> for S
393+
where
394+
S: SystemCondition<Marker, In>,
395+
{
396+
fn into_boxed_condition(this: Self) -> BoxedCondition<In> {
397+
Box::new(IntoSystem::into_system(this))
398+
}
399+
}
400+
401+
impl<In: SystemInput> IntoBoxedCondition<(), In> for BoxedCondition<In> {
402+
fn into_boxed_condition(this: Self) -> BoxedCondition<In> {
403+
this
404+
}
405+
}
406+
380407
/// A collection of [run conditions](SystemCondition) that may be useful in any bevy app.
381408
pub mod common_conditions {
382409
use super::{NotSystem, SystemCondition};
@@ -1249,14 +1276,16 @@ where
12491276

12501277
#[cfg(test)]
12511278
mod tests {
1279+
use alloc::boxed::Box;
1280+
12521281
use super::{common_conditions::*, SystemCondition};
12531282
use crate::error::{BevyError, DefaultErrorHandler, ErrorContext};
12541283
use crate::{
12551284
change_detection::ResMut,
12561285
component::Component,
12571286
message::Message,
12581287
query::With,
1259-
schedule::{IntoScheduleConfigs, Schedule},
1288+
schedule::{BoxedCondition, IntoScheduleConfigs, Schedule},
12601289
system::{IntoSystem, Local, Res, System},
12611290
world::World,
12621291
};
@@ -1434,4 +1463,25 @@ mod tests {
14341463
.configure_sets(Set.run_if(condition));
14351464
schedule.run(&mut world);
14361465
}
1466+
1467+
#[test]
1468+
fn boxed_run_if() {
1469+
#[derive(Resource, Default)]
1470+
struct MyResource;
1471+
1472+
let mut world = World::new();
1473+
world.init_resource::<Counter>();
1474+
let mut schedule = Schedule::default();
1475+
1476+
let condition: BoxedCondition =
1477+
Box::new(IntoSystem::into_system(resource_exists::<MyResource>));
1478+
1479+
schedule.add_systems(increment_counter.run_if(condition));
1480+
1481+
schedule.run(&mut world);
1482+
assert_eq!(world.resource::<Counter>().0, 0);
1483+
world.init_resource::<MyResource>();
1484+
schedule.run(&mut world);
1485+
assert_eq!(world.resource::<Counter>().0, 1);
1486+
}
14371487
}

0 commit comments

Comments
 (0)