pub struct Previous<T: Clone + PartialEq + 'static> { /* private fields */ }Expand description
Tracks the previously observed value of some external reactive source.
Typical use: in a render closure, call
previous.record(current) at the top, then read
previous.get_previous().get() to find out what the
value was on the previous render. The two reads are
decoupled so callers can record and read independently.
§Why a Signal<Option<T>>?
Because the very first call to record has no
“previous” to report. The signal starts at None
and flips to Some(value) after the first record.
Render code can branch on the Option for
“first render vs subsequent”.
§Lombok caveat
Previous cannot use Lombok New because the
previous: Signal<Option<T>> field would require
T: Default to satisfy Signal::default(). The
struct intentionally keeps T: Clone + PartialEq + 'static (no Default), and the constructor is
hand-written in impl.rs to wrap the field with
Signal::create(None).
Implementations§
Source§impl<T: Clone + PartialEq + 'static> Previous<T>
Inherent implementation of Previous.
impl<T: Clone + PartialEq + 'static> Previous<T>
Inherent implementation of Previous.
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Previous with no recorded value.
The previous signal starts at None.
Sourcepub fn record(&self, current: T)
pub fn record(&self, current: T)
Records current as the new previous value. The
next call to get_previous_snapshot() will return
Some(current).
This is typically called at the top of a render closure so the signal stores the value just seen.
§Arguments
T: Clone + PartialEq + 'static- A generic type parameter.
Sourcepub fn get_previous_snapshot(&self) -> Option<T>
pub fn get_previous_snapshot(&self) -> Option<T>
Returns a snapshot of the previously recorded
value, or None if no value has been recorded yet.
§Returns
Option<T>- The previous captured value, orNone.
Trait Implementations§
impl<T> Copy for Previous<T>
Previous<T> is Copy when T is — Signal<Option<T>> is
already Copy (the signal registry hands out cheap usize
addresses), so this blanket impl is sound.
Source§impl<T: Clone + PartialEq + 'static> Default for Previous<T>
Default-construction for Previous.
impl<T: Clone + PartialEq + 'static> Default for Previous<T>
Default-construction for Previous.