Struct yewtil::History[][src]

pub struct History<T>(_);
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

impl<T> History<T>[src]

pub fn new(value: T) -> Self[src]

Creates a new history wrapper.

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

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);

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

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);

pub fn forget(&mut self) -> bool[src]

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);

pub fn reset(&mut self) -> bool[src]

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);

pub fn dirty(&mut self) -> bool[src]

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());

pub fn count(&self) -> usize[src]

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);

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

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

pub fn into_inner(self) -> T[src]

Gets the current value.

impl<T: PartialEq> History<T>[src]

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

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

impl<T> AsRef<T> for History<T>[src]

fn as_ref(&self) -> &T[src]

Performs the conversion.

impl<T> Deref for History<T>[src]

type Target = T

The resulting type after dereferencing.

fn deref(&self) -> &Self::Target[src]

Dereferences the value.

impl<T> IntoIterator for History<T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, V> IntoOptPropValue<V> for T where
    T: IntoPropValue<Option<V>>, 
[src]

pub fn into_opt_prop_value(self) -> Option<V>[src]

Convert self to an optional value of a Properties struct.

impl<T> IntoPropValue<Option<T>> for T[src]

pub fn into_prop_value(self) -> Option<T>[src]

Convert self to a value of a Properties struct.

impl<T> IntoPropValue<T> for T[src]

pub fn into_prop_value(self) -> T[src]

Convert self to a value of a Properties struct.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T> Any for T where
    T: Any