history_stack

Struct UndoStack

Source
pub struct UndoStack<T> { /* private fields */ }
Expand description

A structure which allows you to undo and redo changes based on saved states of T.

To use, simply save, undo, and redo later if needed;

// Create with initial state
let mut undo = UndoStack::new(5u8);

// make a savepoint and get a reference to the new current value
// our stack looks like [5, 5] currently, our current value being the second
let newref = undo.save();

// we modified the new current value, our stack looks like [5, 10] now
*newref *= 2;

// but we made a mistake! we want to go back now, and since we are
// sure we saved earlier we can unwrap here to get the Ok variant
// our stack still looks like [5, 10], but now we point to the 5
let oldref = undo.undo().unwrap();

// turns out it wasnt a mistake, lets redo and unwrap to be sure we got the newer value
undo.redo().unwrap();

// UndoStack implements Deref and DerefMut, we can make sure we got the new value like this
assert_eq!(undo, 10);

This is useful when you want to be able to make changes in a way where you can undo a change, and then reapply it later, but do not wish to write a complex incremental structure that could track changes like that. This type provides a generic (read: you can use it on anything) interface to achieve that effect, even if it may use more memory than a more targeted approach.

UndoStack is also “transparently T”, meaning the default traits it implements all act like the current value of T, so hashing UndoStack<T> and T produce the same hash, Eq and Ord work the same etc. This also includes Display, but does not include Debug.

Implementations§

Source§

impl<T> UndoStack<T>

Source

pub fn new(start: T) -> Self

Creates a new UndoStack with a starting value to act as the current value

Source

pub fn save(&mut self) -> &mut T
where T: Clone,

Saves the current T to history and invalidates any data that may be used to redo This will Drop any T that exist later in history than the current edit point.

Returns a reference to the new current value

§Panics

This will panic if allocation failed

Source

pub fn push(&mut self, new_current: T) -> &mut T

Pushes the given value to the stack, making it the new current value and invalidating future history, returns a reference to the new current value

This is functionally identical to save but does not have a Clone bound, instead sourcing its new value from the caller.

§Panics

This will panic if allocation failed

Source

pub fn undo(&mut self) -> Result<&mut T, &mut T>

If there is a previous state in the history stack, backtrack to that and return Ok(&mut T) to the new current value, otherwise return Err(&mut T) to the unchanged current value.

Source

pub fn redo(&mut self) -> Result<&mut T, &mut T>

If there is a future state in the history stack that has been undone from, redo to that position and return Ok(&mut T) of the new current value after advancing, else return Err(&mut T) of the current unchanged value, if there was no future history.

Trait Implementations§

Source§

impl<T: Clone> Clone for UndoStack<T>

Source§

fn clone(&self) -> UndoStack<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for UndoStack<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for UndoStack<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Deref for UndoStack<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T> DerefMut for UndoStack<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: Display> Display for UndoStack<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Hash> Hash for UndoStack<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord> Ord for UndoStack<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq<T> for UndoStack<T>

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> PartialEq for UndoStack<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd<T> for UndoStack<T>

Source§

fn partial_cmp(&self, other: &T) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: PartialOrd> PartialOrd for UndoStack<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq> Eq for UndoStack<T>

Auto Trait Implementations§

§

impl<T> Freeze for UndoStack<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for UndoStack<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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<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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.