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>
impl<'state, TState: Clone> Undo<'state, TState>
Sourcepub fn new(state: TState) -> Self
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);Sourcepub fn unwrap(self) -> TState
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 !");Sourcepub fn update(&mut self, update_fn: impl Fn(&mut TState) + 'state)
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);Sourcepub fn undo(&mut self)
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);Sourcepub fn redo(&mut self)
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§
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> 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
Mutably borrows from an owned value. Read more