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>
impl<T> History<T>
Sourcepub fn set(&mut self, value: T)
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);
Sourcepub fn replace(&mut self, value: T)
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);
Sourcepub fn forget(&mut self) -> bool
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);
Sourcepub fn reset(&mut self) -> bool
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);
Sourcepub fn dirty(&mut self) -> bool
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());
Sourcepub fn count(&self) -> usize
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);
Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Produces an iterator over references to history items ordered from newest to oldest.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Gets the current value.
Source§impl<T: PartialEq> History<T>
impl<T: PartialEq> History<T>
Sourcepub fn neq_set(&mut self, value: T) -> bool
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§
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> 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, V> IntoOptPropValue<V> for Twhere
T: IntoPropValue<Option<V>>,
impl<T, V> IntoOptPropValue<V> for Twhere
T: IntoPropValue<Option<V>>,
Source§fn into_opt_prop_value(self) -> Option<V>
fn into_opt_prop_value(self) -> Option<V>
self
to an optional value of a Properties
struct.Source§impl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
Source§fn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
self
to a value of a Properties
struct.Source§impl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
Source§fn into_prop_value(self) -> T
fn into_prop_value(self) -> T
self
to a value of a Properties
struct.Source§impl<T, U> NeqAssign<U> for T
impl<T, U> NeqAssign<U> for T
Source§fn neq_assign(&mut self, new: U) -> bool
fn neq_assign(&mut self, new: U) -> bool
self
and new
aren’t equal, assigns new
to self
and returns true, otherwise returns false. Read more