Struct undo::UndoGroup
[−]
[src]
pub struct UndoGroup<'a, E> { /* fields omitted */ }
A collection of UndoStack
s.
An UndoGroup
is useful when working with multiple stacks and only one of them should
be active at a given time, eg. a text editor with multiple documents opened. However, if only
a single stack is needed, it is easier to just use the stack directly.
The PopCmd
given in the examples below is defined as:
#[derive(Clone, Copy)] struct PopCmd { vec: *mut Vec<i32>, e: Option<i32>, } impl UndoCmd for PopCmd { type Err = (); fn redo(&mut self) -> undo::Result<()> { self.e = unsafe { let ref mut vec = *self.vec; vec.pop() }; Ok(()) } fn undo(&mut self) -> undo::Result<()> { unsafe { let ref mut vec = *self.vec; let e = self.e.ok_or(())?; vec.push(e); } Ok(()) } }
Methods
impl<'a, E: 'a> UndoGroup<'a, E>
[src]
fn new() -> UndoGroup<'a, E>
fn with_capacity(capacity: usize) -> UndoGroup<'a, E>
Creates a new UndoGroup
with the specified capacity.
Examples
let group = UndoGroup::<()>::with_capacity(10); assert!(group.capacity() >= 10);
fn capacity(&self) -> usize
Returns the capacity of the UndoGroup
.
Examples
let group = UndoGroup::<()>::with_capacity(10); assert!(group.capacity() >= 10);
fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more stacks to be inserted in the given group.
The group may reserve more space to avoid frequent reallocations.
Panics
Panics if the new capacity overflows usize.
Examples
let mut group = UndoGroup::<()>::new(); group.add(UndoStack::new()); group.reserve(10); assert!(group.capacity() >= 11);
fn shrink_to_fit(&mut self)
Shrinks the capacity of the UndoGroup
as much as possible.
Examples
let mut group = UndoGroup::<()>::with_capacity(10); group.add(UndoStack::new()); group.add(UndoStack::new()); group.add(UndoStack::new()); assert!(group.capacity() >= 10); group.shrink_to_fit(); assert!(group.capacity() >= 3);
fn add(&mut self, stack: UndoStack<'a, E>) -> Id
Adds an UndoStack
to the group and returns an unique id for this stack.
Examples
let mut group = UndoGroup::<()>::new(); let a = group.add(UndoStack::new()); let b = group.add(UndoStack::new()); let c = group.add(UndoStack::new());
fn remove(&mut self, Id: Id) -> Option<UndoStack<'a, E>>
Removes the UndoStack
with the specified id and returns the stack.
Returns None
if the stack was not found.
Examples
let mut group = UndoGroup::<()>::new(); let a = group.add(UndoStack::new()); let stack = group.remove(a); assert!(stack.is_some());
fn set_active(&mut self, Id: &Id)
Sets the UndoStack
with the specified id as the current active one.
Examples
let mut group = UndoGroup::<()>::new(); let a = group.add(UndoStack::new()); group.set_active(&a);
fn clear_active(&mut self)
Clears the current active UndoStack
.
Examples
let mut group = UndoGroup::<()>::new(); let a = group.add(UndoStack::new()); group.set_active(&a); group.clear_active();
fn is_clean(&self) -> Option<bool>
Calls is_clean
on the active UndoStack
, if there is one.
Returns None
if there is no active stack.
Examples
let mut vec = vec![1, 2, 3]; let mut group = UndoGroup::new(); let cmd = PopCmd { vec: &mut vec, e: None }; let a = group.add(UndoStack::new()); assert_eq!(group.is_clean(), None); group.set_active(&a); assert_eq!(group.is_clean(), Some(true)); // An empty stack is always clean. group.push(cmd); assert_eq!(group.is_clean(), Some(true)); group.undo(); assert_eq!(group.is_clean(), Some(false));
fn is_dirty(&self) -> Option<bool>
Calls is_dirty
on the active UndoStack
, if there is one.
Returns None
if there is no active stack.
Examples
let mut vec = vec![1, 2, 3]; let mut group = UndoGroup::new(); let cmd = PopCmd { vec: &mut vec, e: None }; let a = group.add(UndoStack::new()); assert_eq!(group.is_dirty(), None); group.set_active(&a); assert_eq!(group.is_dirty(), Some(false)); // An empty stack is always clean. group.push(cmd); assert_eq!(group.is_dirty(), Some(false)); group.undo(); assert_eq!(group.is_dirty(), Some(true));
fn push<T>(&mut self, cmd: T) -> Option<Result<E>> where T: UndoCmd<Err=E> + 'a
Calls push
on the active UndoStack
, if there is one.
Returns Some(Ok)
if everything went fine, Some(Err)
if something went wrong, and None
if there is no active stack.
Examples
let mut vec = vec![1, 2, 3]; let mut group = UndoGroup::new(); let cmd = PopCmd { vec: &mut vec, e: None }; let a = group.add(UndoStack::new()); group.set_active(&a); group.push(cmd); group.push(cmd); group.push(cmd); assert!(vec.is_empty());
fn redo(&mut self) -> Option<Result<E>>
Calls redo
on the active UndoStack
, if there is one.
Returns Some(Ok)
if everything went fine, Some(Err)
if something went wrong, and None
if there is no active stack.
Examples
let mut vec = vec![1, 2, 3]; let mut group = UndoGroup::new(); let cmd = PopCmd { vec: &mut vec, e: None }; let a = group.add(UndoStack::new()); group.set_active(&a); group.push(cmd); group.push(cmd); group.push(cmd); assert!(vec.is_empty()); group.undo(); group.undo(); group.undo(); assert_eq!(vec, vec![1, 2, 3]); group.redo(); group.redo(); group.redo(); assert!(vec.is_empty());
fn undo(&mut self) -> Option<Result<E>>
Calls undo
on the active UndoStack
, if there is one.
Returns Some(Ok)
if everything went fine, Some(Err)
if something went wrong, and None
if there is no active stack.
Examples
let mut vec = vec![1, 2, 3]; let mut group = UndoGroup::new(); let cmd = PopCmd { vec: &mut vec, e: None }; let a = group.add(UndoStack::new()); group.set_active(&a); group.push(cmd); group.push(cmd); group.push(cmd); assert!(vec.is_empty()); group.undo(); group.undo(); group.undo(); assert_eq!(vec, vec![1, 2, 3]);