Struct History

Source
pub struct History<T>(/* private fields */);
Expand description

Wrapper that keeps track of prior values that have been assigned to it.

It keeps values that have been set for it around for the duration of its lifetime, or until they are dropped by calling reset or forget.

Prior values can be iterated over as well.

§Example

use yewtil::History;

pub enum Msg {
    SetText(String),
    Reset,
    Forget,
}

pub struct Model {
    link: ComponentLink<Self>,
    text: History<String>,
}
impl Component for Model {
    type Message = Msg;

    fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
        Self {
            link,
            text: History::new("Hello World!".to_string()),
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::SetText(text) => self.text.neq_set(text),
            Msg::Reset => self.text.reset(),
            Msg::Forget => {
                self.text.forget();
                false
            }
        }
    }

    fn view(&self) -> Html {
        html! {
            <>
                <span>{ &*self.text }</span>
                <div>
                    <input
                        type="text"
                        value=(*self.text).clone()
                        oninput=self.link.callback(|data: InputData| Msg::SetText(data.value))
                    />
                    <button onclick=self.link.callback(|_| Msg::Reset)>{ "Reset to the oldest value" }</button>
                    <button onclick=self.link.callback(|_| Msg::Forget)>{ "Forget prior values" }</button>
                </div>
                <div>
                    <span>{ "History" }</span>
                    { for self.text.iter() }
                </div>
            </>
        }
    }
}

Implementations§

Source§

impl<T> History<T>

Source

pub fn new(value: T) -> Self

Creates a new history wrapper.

Source

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

Set the value represented by the History struct.

This pushes the new value into the front of a list, where the front-most value represents the most recent value.

§Example
let mut history = History::new(0);
history.set(1);

assert_eq!(*history, 1);
assert_eq!(history.count(), 2);
Source

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

Replaces the current value without creating a history entry.

§Example
let mut history = History::new(0);
history.replace(1);

assert_eq!(*history, 1);
assert_eq!(history.count(), 1);
Source

pub fn forget(&mut self) -> bool

Removes all prior values.

This effectively sets a new “checkpoint” that can be restored by calling reset.

The returned bool indicates if any entries were removed.

§Example
let mut history = History::new(0);
history.set(1);
history.set(2);

history.forget();
assert_eq!(*history, 2);
assert_eq!(history.count(), 1);
Source

pub fn reset(&mut self) -> bool

Remove all elements except the last one, making the oldest entry the “current value”.

The returned bool indicates if any entries were removed.

§Example
let mut history = History::new(0);
history.set(1);
history.set(2);

history.reset();
assert_eq!(*history, 0);
assert_eq!(history.count(), 1);
Source

pub fn dirty(&mut self) -> bool

Returns true if there is more than one entry in the history.

§Example
let mut history = History::new(0);
history.set(1);
assert!(history.dirty());
Source

pub fn count(&self) -> usize

Returns the number of entries in the history.

This will never be less than 1, as the first entry of the backing VecDeque is always occupied by the “current value” in the History struct.

§Example
let mut history = History::new(0);
assert_eq!(history.count(), 1);

history.set(1);
assert_eq!(history.count(), 2);
Source

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

Produces an iterator over references to history items ordered from newest to oldest.

Source

pub fn into_inner(self) -> T

Gets the current value.

Source§

impl<T: PartialEq> History<T>

Source

pub fn neq_set(&mut self, value: T) -> bool

Will only set the value if the provided value is different than the current value.

It returns true to indicate if the history’s current value was updated to be the provided value.

§Example
let mut history = History::new(0);
let did_set = history.neq_set(0);
assert!(!did_set);

let did_set = history.neq_set(1);
assert!(did_set);

Trait Implementations§

Source§

impl<T> AsRef<T> for History<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Deref for History<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> IntoIterator for History<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<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

Auto Trait Implementations§

§

impl<T> Freeze for History<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for History<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> 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, V> IntoOptPropValue<V> for T
where T: IntoPropValue<Option<V>>,

Source§

fn into_opt_prop_value(self) -> Option<V>

Convert self to an optional value of a Properties struct.
Source§

impl<T> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
Source§

impl<T, U> NeqAssign<U> for T
where T: BorrowMut<U>, U: PartialEq,

Source§

fn neq_assign(&mut self, new: U) -> bool

If self and new aren’t equal, assigns new to self and returns true, otherwise returns false. Read more
Source§

impl<T, U> NeqAssignBy<U> for T
where T: BorrowMut<U>,

Source§

fn neq_assign_by<F>(&mut self, new: U, eq: F) -> bool
where F: FnOnce(&U, &U) -> bool,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
Source§

impl<T> Any for T
where T: Any,