Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion crates/bevy_ecs/src/schedule/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,33 @@ impl<Marker, In: SystemInput, F> SystemCondition<Marker, In> for F where
{
}

/// Conversion trait to turn something into a [`BoxedCondition`].
///
/// This trait is automatically implemented for all types that implement
/// [`SystemCondition`], as well as for [`BoxedCondition`] itself.
pub trait IntoBoxedCondition<Marker, In: SystemInput = ()> {
/// Converts `self` into a boxed run condition.
fn into_boxed_condition(this: Self) -> BoxedCondition<In>;
}

#[doc(hidden)]
pub struct UnboxedSystemMarker;

impl<Marker, In: SystemInput, S> IntoBoxedCondition<(UnboxedSystemMarker, Marker), In> for S
where
S: SystemCondition<Marker, In>,
{
fn into_boxed_condition(this: Self) -> BoxedCondition<In> {
Box::new(IntoSystem::into_system(this))
}
}

impl<In: SystemInput> IntoBoxedCondition<(), In> for BoxedCondition<In> {
fn into_boxed_condition(this: Self) -> BoxedCondition<In> {
this
}
}

/// A collection of [run conditions](SystemCondition) that may be useful in any bevy app.
pub mod common_conditions {
use super::{NotSystem, SystemCondition};
Expand Down Expand Up @@ -1249,14 +1276,16 @@ where

#[cfg(test)]
mod tests {
use alloc::boxed::Box;

use super::{common_conditions::*, SystemCondition};
use crate::error::{BevyError, DefaultErrorHandler, ErrorContext};
use crate::{
change_detection::ResMut,
component::Component,
message::Message,
query::With,
schedule::{IntoScheduleConfigs, Schedule},
schedule::{BoxedCondition, IntoScheduleConfigs, Schedule},
system::{IntoSystem, Local, Res, System},
world::World,
};
Expand Down Expand Up @@ -1434,4 +1463,25 @@ mod tests {
.configure_sets(Set.run_if(condition));
schedule.run(&mut world);
}

#[test]
fn boxed_run_if() {
#[derive(Resource, Default)]
struct MyResource;

let mut world = World::new();
world.init_resource::<Counter>();
let mut schedule = Schedule::default();

let condition: BoxedCondition =
Box::new(IntoSystem::into_system(resource_exists::<MyResource>));

schedule.add_systems(increment_counter.run_if(condition));

schedule.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);
world.init_resource::<MyResource>();
schedule.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);
}
}
Loading