pub trait IntoSignalEither: Sizedwhere
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§
Sourcefn left_either<R>(self) -> SignalEither<Self, R>where
R: Signal,
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)Sourcefn right_either<L>(self) -> SignalEither<L, Self>where
L: Signal,
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 typesDyn 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.