Skip to content

Commit

Permalink
Add helper for not pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Feb 8, 2023
1 parent 5efc226 commit aa35227
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions crates/bevy_ecs/src/system/system_piping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ where
pub mod adapter {
use crate::system::In;
use bevy_utils::tracing;
use std::fmt::Debug;
use std::{fmt::Debug, ops::Not};

/// Converts a regular function into a system adapter.
///
Expand Down Expand Up @@ -406,6 +406,34 @@ pub mod adapter {
/// ```
pub fn ignore<T>(In(_): In<T>) {}

/// System adapter that inverts the output of the previous system in a pipe.
///
/// # Examples
///
/// ```
/// use bevy_ecs::prelude::*;
/// // Building a new schedule/app...
/// let mut sched = Schedule::default();
/// sched.add_system(
/// // This system will always run.
/// my_system.run_if(always_false.pipe(system_adapter::negate))
/// )
/// // ...
/// # ;
/// # let mut world = World::new();
/// # sched.run(&mut world);
///
/// // A condition that always returns false.
/// fn always_false() -> bool {
/// false
/// }
///
/// # fn my_system() {}
/// ```
pub fn negate<T: Not>(In(val): In<T>) -> T::Output {
!val
}

#[cfg(test)]
#[test]
fn assert_systems() {
Expand All @@ -423,16 +451,12 @@ pub mod adapter {
unimplemented!()
}

fn not(In(val): In<bool>) -> bool {
!val
}

assert_is_system(returning::<Result<u32, std::io::Error>>.pipe(unwrap));
assert_is_system(returning::<Option<()>>.pipe(ignore));
assert_is_system(returning::<&str>.pipe(new(u64::from_str)).pipe(unwrap));
assert_is_system(exclusive_in_out::<(), Result<(), std::io::Error>>.pipe(error));
assert_is_system(returning::<bool>.pipe(exclusive_in_out::<bool, ()>));

returning::<()>.run_if(returning::<bool>.pipe(not));
returning::<()>.run_if(returning::<bool>.pipe(negate));
}
}

0 comments on commit aa35227

Please sign in to comment.