Skip to content

Commit

Permalink
Fix trait bounds for run conditions (#7688)
Browse files Browse the repository at this point in the history
# Objective

The trait `Condition<>` is implemented for any type that can be converted into a `ReadOnlySystem` which takes no inputs and returns a bool. However, due to the current implementation, consumers of the trait cannot rely on the fact that `<T as Condition>::System` implements `ReadOnlySystem`. In cases such as the `not` combinator (added in #7559), we are required to add redundant `T::System: ReadOnlySystem` trait bounds, even though this should be implied by the `Condition<>` trait.

## Solution

Add a hidden associated type which allows the compiler to figure out that the `System` associated type implements `ReadOnlySystem`.
  • Loading branch information
JoJoJet committed Feb 16, 2023
1 parent 95c8d88 commit 8130729
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions crates/bevy_ecs/src/schedule/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ impl<Params, F> Condition<Params> for F where F: sealed::Condition<Params> {}
mod sealed {
use crate::system::{IntoSystem, ReadOnlySystem};

pub trait Condition<Params>: IntoSystem<(), bool, Params> {}
pub trait Condition<Params>:
IntoSystem<(), bool, Params, System = Self::ReadOnlySystem>
{
// This associated type is necessary to let the compiler
// know that `Self::System` is `ReadOnlySystem`.
type ReadOnlySystem: ReadOnlySystem<In = (), Out = bool>;
}

impl<Params, F> Condition<Params> for F
where
F: IntoSystem<(), bool, Params>,
F::System: ReadOnlySystem,
{
type ReadOnlySystem = F::System;
}
}

Expand Down Expand Up @@ -133,12 +140,9 @@ pub mod common_conditions {
/// #
/// # fn my_system() { unreachable!() }
/// ```
pub fn not<Params, C: Condition<Params>>(
condition: C,
) -> impl ReadOnlySystem<In = (), Out = bool>
where
C::System: ReadOnlySystem,
{
pub fn not<Params>(
condition: impl Condition<Params>,
) -> impl ReadOnlySystem<In = (), Out = bool> {
condition.pipe(|In(val): In<bool>| !val)
}
}

0 comments on commit 8130729

Please sign in to comment.