[−][src]Function pareen::cond
pub fn cond<T, V, F, G, H, Cond, A, B>(
cond: Cond,
a: A,
b: B
) -> Anim<impl Fun<T = T, V = V>> where
T: Copy,
F: Fun<T = T, V = bool>,
G: Fun<T = T, V = V>,
H: Fun<T = T, V = V>,
Cond: Into<Anim<F>>,
A: Into<Anim<G>>,
B: Into<Anim<H>>,
Return the value of one of two animations depending on a condition.
This allows returning animations of different types conditionally.
Note that the condition cond
may either be a value true
and false
, or
it may itself be a dynamic animation of type bool
.
For dynamic conditions, in many cases it suffices to use either
Anim::switch
or
Anim::seq
instead of this function.
Examples
Constant conditions
The following example does not compile, because the branches have different types:
ⓘThis example deliberately fails to compile
let cond = true; let anim = if cond { pareen::constant(1) } else { pareen::id() };
However, this does compile:
let cond = true; let anim = pareen::cond(cond, 1, pareen::id()); assert_eq!(anim.eval(2), 1);
Dynamic conditions
let cond = pareen::fun(|t| t * t <= 4); let anim_1 = 1; let anim_2 = pareen::id(); let anim = pareen::cond(cond, anim_1, anim_2); assert_eq!(anim.eval(1), 1); // 1 * 1 <= 4 assert_eq!(anim.eval(2), 1); // 2 * 2 <= 4 assert_eq!(anim.eval(3), 3); // 3 * 3 > 4