Skip to main content

Pairs

Struct Pairs 

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

Two states of a collection, matched item by item, ready to be sampled at any t.

Built once per change, sampled once per frame: matching is the part that costs something and it does not depend on t. Pairs::by_key matches by an identity the caller derives; a caller whose own rules already know what moved where (a sliding-tile game knows which tiles merged) collects its Pairs directly (Pairs: FromIterator).

use day_core::tween::{Change, Lerp, Pairs, lerp};

#[derive(Clone, Debug, PartialEq)]
struct Bar { name: &'static str, height: f64 }

impl Lerp for Bar {
    fn lerp(&self, to: &Self, t: f64) -> Self {
        Bar { name: to.name, height: lerp(self.height, to.height, t) }
    }
}

let bar = |name, height| Bar { name, height };
// "b" shrinks, "c" grows out of nothing, "a" collapses away.
let before = [bar("a", 3.0), bar("b", 5.0)];
let after = [bar("b", 1.0), bar("c", 4.0)];
let pairs = Pairs::by_key(&before, &after, |b| b.name, |b, _| bar(b.name, 0.0));
assert_eq!(pairs.at(0.5), vec![bar("b", 3.0), bar("c", 2.0), bar("a", 1.5)]);
assert_eq!(pairs[2].change, Change::Leaving);

Implementations§

Source§

impl<T: Clone> Pairs<T>

Source

pub fn by_key<K: Eq + Hash>( from: &[T], to: &[T], key: impl FnMut(&T) -> K, absent: impl FnMut(&T, Change) -> T, ) -> Self

Match from with to by key.

  • An item whose key is on both sides is Change::Kept. Keys may repeat: the nth item with a key on one side matches the nth with that key on the other, so identical tokens (three red gems in a column) pair up in order rather than all with the first.
  • An item only in to is Change::Entering; absent(item, Change::Entering) says where it starts. An item only in from is Change::Leaving; absent(item, Change::Leaving) says where it ends. That stand-in is the whole of what entering and leaving mean: grown from a baseline, faded out, slid in from a neighbor. The caller decides; the match does not.

The result is in to’s order, then the leaving items in from’s order, so a renderer drawing in sequence draws the arriving picture’s order, with what is departing on top.

Source§

impl<T> Pairs<T>

Source

pub fn at_with<U>(&self, t: f64, f: impl FnMut(&Pair<T>, f64) -> U) -> Vec<U>

Every item t of the way along, in order, through f: for items whose in-between depends on more than the two ends (a chart mark needs both charts’ scales to place itself).

Source

pub fn at_each<U>( &self, progress: impl FnMut(usize, &Pair<T>) -> f64, f: impl FnMut(&Pair<T>, f64) -> U, ) -> Vec<U>

Each item at its own progress: progress(index, pair) gives the t for that item, for sequences that do not move in lockstep (cards dealt one after another, see stagger).

Source

pub fn settled(&self) -> impl Iterator<Item = &T>

The arriving state: every item that is not leaving, at its end. What to draw once the transition is over.

Source

pub fn counts(&self) -> (usize, usize, usize)

How many items of each kind of Change there are: (kept, entering, leaving).

Source

pub fn iter(&self) -> Iter<'_, Pair<T>> ⓘ

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source§

impl<T: Lerp> Pairs<T>

Source

pub fn at(&self, t: f64) -> Vec<T>

Every item t of the way along, in order. Leaving items are included until the caller stops sampling: at t = 1 they sit at their stand-ins.

Trait Implementations§

Source§

impl<T: Clone> Clone for Pairs<T>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Pairs<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Pairs<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> FromIterator<Pair<T>> for Pairs<T>

Source§

fn from_iter<I: IntoIterator<Item = Pair<T>>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> Index<usize> for Pairs<T>

Source§

type Output = Pair<T>

The returned type after indexing.
Source§

fn index(&self, i: usize) -> &Pair<T>

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IntoIterator for Pairs<T>

Source§

type Item = Pair<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<Pair<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a Pairs<T>

Source§

type Item = &'a Pair<T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Pair<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for Pairs<T>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq> StructuralPartialEq for Pairs<T>

Auto Trait Implementations§

§

impl<T> Freeze for Pairs<T>

§

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

§

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

§

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

§

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

§

impl<T> UnsafeUnpin for Pairs<T>

§

impl<T> UnwindSafe for Pairs<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
§

impl<T> AnyEq for T
where T: Any + PartialEq,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

fn as_any(&self) -> &(dyn Any + 'static)

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
§

impl<T> ErasedDestructor for T
where T: 'static,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.
Day API ↩ Guides· daybrite.dev