Undo

Struct Undo 

Source
pub struct Undo<'state, TState> { /* private fields */ }
Expand description

The Undo type wrapping a state that tracks updates and allows undoing or redoing them.

Implementations§

Source§

impl<'state, TState: Clone> Undo<'state, TState>

Source

pub fn new(state: TState) -> Self

Wraps the given state in an Undo, which will track all updates and allows undoing or redoing them.

§Example
use simple_undo::Undo;

let mut wrapper = Undo::new(5);
Source

pub fn unwrap(self) -> TState

Unwraps the inner state to an owned value, disabling the undo/redo feature.

§Example
let mut message = Undo::new(String::new());
message.update(|text| text.push_str("Hello "));
message.update(|text| text.push_str("world !"));

let result: String = message.unwrap();
assert_eq!(result, "Hello world !");
Source

pub fn update(&mut self, update_fn: impl Fn(&mut TState) + 'state)

Updates the current state with the given mutating function.

Note that future Undo::redo are reset.

§Example
let mut counter = Undo::new(0);
counter.update(|value| *value += 10);
counter.update(|value| *value -= 5);
counter.update(|value| *value += 3);
assert_eq!(*counter, 8);
Source

pub fn undo(&mut self)

Undo the last update done to the current state.

§Example
let mut counter = Undo::new(0);
counter.update(|value| *value += 1);
counter.update(|value| *value += 2);
assert_eq!(*counter, 3);

counter.undo();
assert_eq!(*counter, 1);
counter.undo();
assert_eq!(*counter, 0);
counter.undo(); // does nothing
assert_eq!(*counter, 0);
Source

pub fn redo(&mut self)

Redo the last update that have been undone using Undo::undo.

§Example
let mut counter = Undo::new(0);
counter.update(|value| *value += 1); // 1
counter.update(|value| *value += 2); // 3
counter.undo(); // 1
counter.undo(); // 0
assert_eq!(*counter, 0);

counter.redo();
assert_eq!(*counter, 1);
counter.redo();
assert_eq!(*counter, 3);
counter.redo(); // does nothing
assert_eq!(*counter, 3);

Trait Implementations§

Source§

impl<'state, TState: Clone> Deref for Undo<'state, TState>

Source§

type Target = TState

The resulting type after dereferencing.
Source§

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

Dereferences the value.

Auto Trait Implementations§

§

impl<'state, TState> Freeze for Undo<'state, TState>
where TState: Freeze,

§

impl<'state, TState> !RefUnwindSafe for Undo<'state, TState>

§

impl<'state, TState> !Send for Undo<'state, TState>

§

impl<'state, TState> !Sync for Undo<'state, TState>

§

impl<'state, TState> Unpin for Undo<'state, TState>
where TState: Unpin,

§

impl<'state, TState> !UnwindSafe for Undo<'state, TState>

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<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.