Skip to main content

Combine

Trait Combine 

Source
pub trait Combine<A, B>
where A: System, B: System,
{ type In: SystemInput; type Out; // Required method fn combine<T>( input: <Self::In as SystemInput>::Inner<'_>, data: &mut T, a: impl FnOnce(<<A as System>::In as SystemInput>::Inner<'_>, &mut T) -> Result<<A as System>::Out, RunSystemError>, b: impl FnOnce(<<B as System>::In as SystemInput>::Inner<'_>, &mut T) -> Result<<B as System>::Out, RunSystemError>, ) -> Result<Self::Out, RunSystemError>; }
Expand description

Customizes the behavior of a CombinatorSystem.

§Examples

use bevy_ecs::prelude::*;
use bevy_ecs::system::{CombinatorSystem, Combine, RunSystemError};

// A system combinator that performs an exclusive-or (XOR)
// operation on the output of two systems.
pub type Xor<A, B> = CombinatorSystem<XorMarker, A, B>;

// This struct is used to customize the behavior of our combinator.
pub struct XorMarker;

impl<A, B> Combine<A, B> for XorMarker
where
    A: System<In = (), Out = bool>,
    B: System<In = (), Out = bool>,
{
    type In = ();
    type Out = bool;

    fn combine<T>(
        _input: Self::In,
        data: &mut T,
        a: impl FnOnce(A::In, &mut T) -> Result<A::Out, RunSystemError>,
        b: impl FnOnce(B::In, &mut T) -> Result<B::Out, RunSystemError>,
    ) -> Result<Self::Out, RunSystemError> {
        Ok(a((), data).unwrap_or(false) ^ b((), data).unwrap_or(false))
    }
}

app.add_systems(my_system.run_if(Xor::new(
    IntoSystem::into_system(resource_equals(A(1))),
    IntoSystem::into_system(resource_equals(B(1))),
    // The name of the combined system.
    "a ^ b".into(),
)));

Required Associated Types§

Source

type In: SystemInput

The input type for a CombinatorSystem.

Source

type Out

The output type for a CombinatorSystem.

Required Methods§

Source

fn combine<T>( input: <Self::In as SystemInput>::Inner<'_>, data: &mut T, a: impl FnOnce(<<A as System>::In as SystemInput>::Inner<'_>, &mut T) -> Result<<A as System>::Out, RunSystemError>, b: impl FnOnce(<<B as System>::In as SystemInput>::Inner<'_>, &mut T) -> Result<<B as System>::Out, RunSystemError>, ) -> Result<Self::Out, RunSystemError>

When used in a CombinatorSystem, this function customizes how the two composite systems are invoked and their outputs are combined.

See the trait-level docs for Combine for an example implementation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§