Struct undo::UndoGroup [] [src]

pub struct UndoGroup<'a, E> { /* fields omitted */ }

A collection of UndoStacks.

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]

Creates a new UndoGroup.

Examples

let group = UndoGroup::<()>::new();

Creates a new UndoGroup with the specified capacity.

Examples

let group = UndoGroup::<()>::with_capacity(10);
assert!(group.capacity() >= 10);

Returns the capacity of the UndoGroup.

Examples

let group = UndoGroup::<()>::with_capacity(10);
assert!(group.capacity() >= 10);

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);

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);

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());

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());

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);

Clears the current active UndoStack.

Examples

let mut group = UndoGroup::<()>::new();
let a = group.add(UndoStack::new());
group.set_active(&a);
group.clear_active();

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));

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));

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());

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());

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]);

Trait Implementations

impl<'a, E: Debug> Debug for UndoGroup<'a, E>
[src]

Formats the value using the given formatter.

impl<'a, E: Default> Default for UndoGroup<'a, E>
[src]

Returns the "default value" for a type. Read more