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>
impl<T: Clone> Pairs<T>
Sourcepub fn by_key<K: Eq + Hash>(
from: &[T],
to: &[T],
key: impl FnMut(&T) -> K,
absent: impl FnMut(&T, Change) -> T,
) -> Self
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
toisChange::Entering;absent(item, Change::Entering)says where it starts. An item only infromisChange::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>
impl<T> Pairs<T>
Sourcepub fn at_with<U>(&self, t: f64, f: impl FnMut(&Pair<T>, f64) -> U) -> Vec<U>
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).
Sourcepub fn at_each<U>(
&self,
progress: impl FnMut(usize, &Pair<T>) -> f64,
f: impl FnMut(&Pair<T>, f64) -> U,
) -> Vec<U>
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).
Sourcepub fn settled(&self) -> impl Iterator<Item = &T>
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.
Sourcepub fn counts(&self) -> (usize, usize, usize)
pub fn counts(&self) -> (usize, usize, usize)
How many items of each kind of Change there are: (kept, entering, leaving).
pub fn iter(&self) -> Iter<'_, Pair<T>> ⓘ
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Trait Implementations§
Source§impl<T> FromIterator<Pair<T>> for Pairs<T>
impl<T> FromIterator<Pair<T>> for Pairs<T>
Source§impl<T> IntoIterator for Pairs<T>
impl<T> IntoIterator for Pairs<T>
Source§impl<'a, T> IntoIterator for &'a Pairs<T>
impl<'a, T> IntoIterator for &'a Pairs<T>
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§
§impl<T> AnyEq for T
impl<T> AnyEq for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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