Struct InterpolationPair

pub struct InterpolationPair<T> { /* private fields */ }
Expand description

Exactly two values, a presentation fraction, a hold flag and a local generation.

The pair never allocates its own storage or retains a sampler/output. Construction and snapping clone one value; commits move values without cloning. Allocations, side effects or panics in T::clone, T::drop or the supplied interpolator are caller-owned. Use small value poses for allocation-free presentation.

sample_with records its effective alpha for hold; it does not advance either endpoint. A pure interpolator and stable values are needed to reproduce the same image while held. See the module recipes for Vec2 and Vec3/Quat consumers.

Implementations§

§

impl<T> InterpolationPair<T>

pub fn new(value: T) -> InterpolationPair<T>
where T: Clone,

Initialize both endpoints from one value, unheld, at alpha 1 and generation 0.

Clones value once; see the module recipes for construction and use.

pub fn previous(&self) -> &T

The preceding committed value. Initially, and after a snap, both agree. See Self::commit for a runnable history example.

pub fn current(&self) -> &T

The most recently committed value; see Self::commit.

pub fn alpha(&self) -> f32

Last successful sample’s effective fraction, or 1 after new/commit/snap.

This fraction applies to the current pair only. Related presentation such as hazard time can use it after sampling. See Self::hold for an example.

pub fn is_held(&self) -> bool

Whether samples use the retained alpha; see Self::hold.

pub fn generation(&self) -> u64

Count explicit snaps since construction, wrapping from u64::MAX to 0.

Compare for change to invalidate presentation caches; this is neither a tick ID nor a globally unique identity. Clones copy the generation, new pairs start at 0, and a full wrap can alias an old value. Ordinary commits, samples, holds and resumes do not change it. See Self::snap.

pub fn commit(&mut self, value: T)

Shift current to previous and retain an actual update’s new value.

Drops the oldest value without cloning. Call after each accepted update, never just because a tick was scheduled or a render frame occurred. A catch-up batch keeps only its last two executed states. Holds are released and alpha becomes 1, so holding before the next sample selects this newest committed state. A commit does not advance the discontinuity generation.

use ferravex_engine::interpolation::InterpolationPair;
let mut history = InterpolationPair::new(0);
for executed_state in [1, 2, 3] { history.commit(executed_state); }
assert_eq!((history.previous(), history.current()), (&2, &3));
assert_eq!(history.generation(), 0);

pub fn sample_with<R>( &mut self, alpha: f32, interpolate: impl FnOnce(&T, &T, f32) -> R, ) -> Result<R, InterpolationError>

Sample borrowed endpoints with a caller-provided interpolation function.

Validates the supplied alpha even while held. Calls interpolate exactly once with (previous, current, effective_alpha), including at 0/1 or after a snap. While held, effective alpha is the retained fraction; otherwise it is the supplied fraction. Records it only after the callback returns. The callback may return any presentation type; the pair does not inspect that result or validate endpoint contents. A callback returning its own Err is still a completed sample, and produces Ok(Err(..)).

§Errors

Nonfinite or out-of-range alpha returns InterpolationError::InvalidAlpha without invoking the callback or mutating any state. Both signed zeros and exactly 1 are valid; no clamping or extrapolation is performed.

use ferravex_engine::interpolation::{InterpolationError, InterpolationPair};
let mut history = InterpolationPair::new(0.0_f32);
history.commit(8.0);
assert_eq!(history.sample_with(0.25, |a, b, t| a + (b - a) * t)?, 2.0);
assert_eq!(history.sample_with(f32::NAN, |_, _, _| panic!("not called")),
           Err::<(), _>(InterpolationError::InvalidAlpha));
assert_eq!(history.alpha(), 0.25);

pub fn hold(&mut self) -> bool

Hold the last successful sample’s alpha; return whether a hold was entered.

Repeated calls are idempotent. Before any sample following new/commit/snap, alpha is 1, so holding selects the current endpoint. This operation retains no third value and performs no interpolation. A commit or snap releases it.

use ferravex_engine::interpolation::InterpolationPair;
let mut history = InterpolationPair::new(0.0_f32);
history.commit(8.0);
let blend = |a: &f32, b: &f32, t| a + (b - a) * t;
assert_eq!(history.sample_with(0.75, blend)?, 6.0);
assert!(history.hold());
assert!(!history.hold());
assert!(history.is_held());
assert_eq!(history.sample_with(0.0, blend)?, 6.0);
assert_eq!(history.alpha(), 0.75);

pub fn resume(&mut self) -> bool

Release a hold; return whether one was active. Other state is unchanged.

The next sample uses its supplied fraction. Pass the active session’s retained fraction on a zero-tick resume, not an inactive frame’s zero. No smoothing or monotonic-alpha constraint is added: after an actual tick a smaller alpha refers to a newer interval and is expected. If an inspector displayed alpha 1 while paused, keep the hold until the next commit to avoid returning to a smaller retained session fraction.

use ferravex_engine::interpolation::InterpolationPair;
let mut history = InterpolationPair::new(0.0_f32);
history.commit(8.0);
let blend = |a: &f32, b: &f32, t| a + (b - a) * t;
history.sample_with(0.5, blend)?;
history.hold();
assert!(history.resume());
assert!(!history.resume());
assert_eq!(history.sample_with(0.75, blend)?, 6.0);

pub fn snap(&mut self, value: T) -> u64
where T: Clone,

Reset both endpoints to an accepted discontinuity and return its generation.

Clones the value once, releases any hold and sets alpha to 1. The local generation increments with wrapping even if the new value equals the old value. Use for teleport/respawn/restart instead of replacing the whole pair, so existing observers can detect reset. There is no old pose to blend from.

use ferravex_engine::interpolation::InterpolationPair;
let mut history = InterpolationPair::new(0.0_f32);
history.commit(8.0);
history.hold();
assert_eq!(history.snap(100.0), 1);
assert!(!history.is_held());
assert_eq!(history.generation(), 1);
assert_eq!((history.previous(), history.current()), (&100.0, &100.0));
assert_eq!(history.sample_with(0.25, |a, b, t| a + (b - a) * t)?, 100.0);

Trait Implementations§

§

impl<T> Clone for InterpolationPair<T>
where T: Clone,

§

fn clone(&self) -> InterpolationPair<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T> Debug for InterpolationPair<T>
where T: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<T> PartialEq for InterpolationPair<T>
where T: PartialEq,

§

fn eq(&self, other: &InterpolationPair<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<T> StructuralPartialEq for InterpolationPair<T>

Auto Trait Implementations§

§

impl<T> Freeze for InterpolationPair<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for InterpolationPair<T>
where T: RefUnwindSafe,

§

impl<T> Send for InterpolationPair<T>
where T: Send,

§

impl<T> Sync for InterpolationPair<T>
where T: Sync,

§

impl<T> Unpin for InterpolationPair<T>
where T: Unpin,

§

impl<T> UnwindSafe for InterpolationPair<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.