Skip to main content

IntoSignalEither

Trait IntoSignalEither 

Source
pub trait IntoSignalEither: Sized
where Self: Signal,
{ // Provided methods fn left_either<R>(self) -> SignalEither<Self, R> where R: Signal { ... } fn right_either<L>(self) -> SignalEither<L, Self> where L: Signal { ... } }
Expand description

Blanket trait for transforming Signals into SignalEither::Left or SignalEither::Right.

Provided Methods§

Source

fn left_either<R>(self) -> SignalEither<Self, R>
where R: Signal,

Wrap this Signal in the SignalEither::Left variant.

Useful for conditional branching where different Signal types need to be returned from the same function or closure, particularly with combinators like .switch or .flatten.

§Example
use bevy_ecs::prelude::*;
use jonmo::prelude::*;

#[derive(Resource)]
struct UseSquare(bool);

let mut world = World::new();
world.insert_resource(UseSquare(true));

let signal = signal::from_system(|In(_), res: Res<UseSquare>| res.0)
    .map(move |In(use_square): In<bool>| {
        if use_square {
            signal::from_system(|In(_)| 10)
                .map(|In(x): In<i32>| x * x)
                .left_either()
        } else {
            signal::from_system(|In(_)| 42).right_either()
        }
    })
    .flatten();
// Both branches produce compatible SignalEither types despite having different
// concrete Signal types (Map vs Source)
Source

fn right_either<L>(self) -> SignalEither<L, Self>
where L: Signal,

Wrap this Signal in the SignalEither::Right variant.

Useful for conditional branching where different Signal types need to be returned from the same function or closure, particularly with combinators like .switch or .flatten.

§Example
use bevy_ecs::prelude::*;
use jonmo::prelude::*;

#[derive(Resource)]
struct Mode(bool);

let mut world = World::new();
world.insert_resource(Mode(false));

let signal = signal::from_system(|In(_), res: Res<Mode>| res.0)
    .map(move |In(mode): In<bool>| {
        if mode {
            signal::from_system(|In(_)| "A").left_either()
        } else {
            signal::from_system(|In(_)| "B")
                .filter(|In(x): In<&str>| x.len() > 0)
                .right_either()
        }
    })
    .flatten();
// Both branches are compatible via SignalEither despite different types

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§