ranim_core/animation/eval/pure.rs
1//! Pure (closed-form) evaluation adapters.
2
3use super::Eval;
4
5/// Adapter turning a raw closure `Fn(f64) -> T` into an [`Eval`] segment.
6///
7/// A closure is an anonymous type, so it cannot implement `Eval` by name; this
8/// named wrapper is the lightweight way to write a pure segment from a closure:
9///
10/// ```rust,ignore
11/// let animation = Pure::new(|alpha| Square::new(alpha)).with_duration(2.0);
12/// ```
13///
14/// Named pure animations implement `Eval` directly and do not need this
15/// wrapper.
16pub struct Pure<F>(pub F);
17
18impl<F> Pure<F> {
19 /// Wrap a closure into an `Eval` animation segment.
20 pub fn new(f: F) -> Self {
21 Self(f)
22 }
23}
24
25impl<T, F> Eval for Pure<F>
26where
27 F: Fn(f64) -> T,
28{
29 type Output = T;
30
31 fn eval_alpha(&self, alpha: f64) -> T {
32 (self.0)(alpha)
33 }
34}