Struct undo::history::History

source ·
pub struct History<A, S = Nop> { /* private fields */ }
Expand description

A history tree of actions.

Unlike Record which maintains a linear undo history, History maintains an undo tree containing every edit made to the target.

Examples

let mut target = String::new();
let mut history = History::new();

history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
let abc = history.branch();

history.go_to(&mut target, abc, 1);
history.apply(&mut target, Push('f'));
history.apply(&mut target, Push('g'));
assert_eq!(target, "afg");

history.go_to(&mut target, abc, 3);
assert_eq!(target, "abc");

Implementations§

source§

impl<A> History<A>

source

pub fn new() -> History<A>

Returns a new history.

Examples found in repository?
examples/history.rs (line 28)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let mut history = History::new();
    let mut target = String::new();

    history.apply(&mut target, Push('a'));
    history.apply(&mut target, Push('b'));
    history.apply(&mut target, Push('c'));
    assert_eq!(target, "abc");

    let abc_branch = history.branch();
    let abc_current = history.current();

    history.undo(&mut target);
    assert_eq!(target, "ab");

    history.apply(&mut target, Push('d'));
    history.apply(&mut target, Push('e'));
    history.apply(&mut target, Push('f'));
    assert_eq!(target, "abdef");

    let abdef_branch = history.branch();
    let abdef_current = history.current();

    history.go_to(&mut target, abc_branch, abc_current);
    assert_eq!(target, "abc");

    history.go_to(&mut target, abdef_branch, abdef_current);
    assert_eq!(target, "abdef");

    println!("{}", history.display());
}
source§

impl<A, S> History<A, S>

source

pub fn builder() -> Builder<A, S>

Returns a new history builder.

source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more actions.

Panics

Panics if the new capacity overflows usize.

source

pub fn capacity(&self) -> usize

Returns the capacity of the history.

source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the history as much as possible.

source

pub fn len(&self) -> usize

Returns the number of actions in the current branch of the history.

source

pub fn is_empty(&self) -> bool

Returns true if the current branch of the history is empty.

source

pub fn limit(&self) -> usize

Returns the limit of the history.

source

pub fn connect(&mut self, slot: S) -> Option<S>

Sets how the signal should be handled when the state changes.

source

pub fn disconnect(&mut self) -> Option<S>

Removes and returns the slot if it exists.

source

pub fn is_saved(&self) -> bool

Returns true if the target is in a saved state, false otherwise.

source

pub fn can_undo(&self) -> bool

Returns true if the history can undo.

source

pub fn can_redo(&self) -> bool

Returns true if the history can redo.

source

pub fn branch(&self) -> usize

Returns the current branch.

Examples found in repository?
examples/history.rs (line 36)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let mut history = History::new();
    let mut target = String::new();

    history.apply(&mut target, Push('a'));
    history.apply(&mut target, Push('b'));
    history.apply(&mut target, Push('c'));
    assert_eq!(target, "abc");

    let abc_branch = history.branch();
    let abc_current = history.current();

    history.undo(&mut target);
    assert_eq!(target, "ab");

    history.apply(&mut target, Push('d'));
    history.apply(&mut target, Push('e'));
    history.apply(&mut target, Push('f'));
    assert_eq!(target, "abdef");

    let abdef_branch = history.branch();
    let abdef_current = history.current();

    history.go_to(&mut target, abc_branch, abc_current);
    assert_eq!(target, "abc");

    history.go_to(&mut target, abdef_branch, abdef_current);
    assert_eq!(target, "abdef");

    println!("{}", history.display());
}
source

pub fn current(&self) -> usize

Returns the position of the current action.

Examples found in repository?
examples/history.rs (line 37)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let mut history = History::new();
    let mut target = String::new();

    history.apply(&mut target, Push('a'));
    history.apply(&mut target, Push('b'));
    history.apply(&mut target, Push('c'));
    assert_eq!(target, "abc");

    let abc_branch = history.branch();
    let abc_current = history.current();

    history.undo(&mut target);
    assert_eq!(target, "ab");

    history.apply(&mut target, Push('d'));
    history.apply(&mut target, Push('e'));
    history.apply(&mut target, Push('f'));
    assert_eq!(target, "abdef");

    let abdef_branch = history.branch();
    let abdef_current = history.current();

    history.go_to(&mut target, abc_branch, abc_current);
    assert_eq!(target, "abc");

    history.go_to(&mut target, abdef_branch, abdef_current);
    assert_eq!(target, "abdef");

    println!("{}", history.display());
}
source

pub fn display(&self) -> Display<'_, A, S>

Returns a structure for configurable formatting of the history.

Examples found in repository?
examples/history.rs (line 56)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let mut history = History::new();
    let mut target = String::new();

    history.apply(&mut target, Push('a'));
    history.apply(&mut target, Push('b'));
    history.apply(&mut target, Push('c'));
    assert_eq!(target, "abc");

    let abc_branch = history.branch();
    let abc_current = history.current();

    history.undo(&mut target);
    assert_eq!(target, "ab");

    history.apply(&mut target, Push('d'));
    history.apply(&mut target, Push('e'));
    history.apply(&mut target, Push('f'));
    assert_eq!(target, "abdef");

    let abdef_branch = history.branch();
    let abdef_current = history.current();

    history.go_to(&mut target, abc_branch, abc_current);
    assert_eq!(target, "abc");

    history.go_to(&mut target, abdef_branch, abdef_current);
    assert_eq!(target, "abdef");

    println!("{}", history.display());
}
source

pub fn actions(&self) -> impl Iterator<Item = &A>

Returns an iterator over the actions in the current branch.

source

pub fn queue(&mut self) -> Queue<'_, A, S>

Returns a queue.

source

pub fn checkpoint(&mut self) -> Checkpoint<'_, A, S>

Returns a checkpoint.

source§

impl<A: Action, S: Slot> History<A, S>

source

pub fn apply(&mut self, target: &mut A::Target, action: A) -> A::Output

Pushes the Action to the top of the history and executes its apply method.

Examples found in repository?
examples/history.rs (line 31)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let mut history = History::new();
    let mut target = String::new();

    history.apply(&mut target, Push('a'));
    history.apply(&mut target, Push('b'));
    history.apply(&mut target, Push('c'));
    assert_eq!(target, "abc");

    let abc_branch = history.branch();
    let abc_current = history.current();

    history.undo(&mut target);
    assert_eq!(target, "ab");

    history.apply(&mut target, Push('d'));
    history.apply(&mut target, Push('e'));
    history.apply(&mut target, Push('f'));
    assert_eq!(target, "abdef");

    let abdef_branch = history.branch();
    let abdef_current = history.current();

    history.go_to(&mut target, abc_branch, abc_current);
    assert_eq!(target, "abc");

    history.go_to(&mut target, abdef_branch, abdef_current);
    assert_eq!(target, "abdef");

    println!("{}", history.display());
}
source

pub fn undo(&mut self, target: &mut A::Target) -> Option<A::Output>

Calls the Action::undo method for the active action and sets the previous one as the new active one.

Examples found in repository?
examples/history.rs (line 39)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let mut history = History::new();
    let mut target = String::new();

    history.apply(&mut target, Push('a'));
    history.apply(&mut target, Push('b'));
    history.apply(&mut target, Push('c'));
    assert_eq!(target, "abc");

    let abc_branch = history.branch();
    let abc_current = history.current();

    history.undo(&mut target);
    assert_eq!(target, "ab");

    history.apply(&mut target, Push('d'));
    history.apply(&mut target, Push('e'));
    history.apply(&mut target, Push('f'));
    assert_eq!(target, "abdef");

    let abdef_branch = history.branch();
    let abdef_current = history.current();

    history.go_to(&mut target, abc_branch, abc_current);
    assert_eq!(target, "abc");

    history.go_to(&mut target, abdef_branch, abdef_current);
    assert_eq!(target, "abdef");

    println!("{}", history.display());
}
source

pub fn redo(&mut self, target: &mut A::Target) -> Option<A::Output>

Calls the Action::redo method for the active action and sets the next one as the new active one.

source

pub fn set_saved(&mut self, saved: bool)

Marks the target as currently being in a saved or unsaved state.

source

pub fn clear(&mut self)

Removes all actions from the history without undoing them.

source

pub fn go_to( &mut self, target: &mut A::Target, branch: usize, current: usize ) -> Vec<A::Output>

Repeatedly calls Action::undo or Action::redo until the action in branch at current is reached.

Examples found in repository?
examples/history.rs (line 50)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    let mut history = History::new();
    let mut target = String::new();

    history.apply(&mut target, Push('a'));
    history.apply(&mut target, Push('b'));
    history.apply(&mut target, Push('c'));
    assert_eq!(target, "abc");

    let abc_branch = history.branch();
    let abc_current = history.current();

    history.undo(&mut target);
    assert_eq!(target, "ab");

    history.apply(&mut target, Push('d'));
    history.apply(&mut target, Push('e'));
    history.apply(&mut target, Push('f'));
    assert_eq!(target, "abdef");

    let abdef_branch = history.branch();
    let abdef_current = history.current();

    history.go_to(&mut target, abc_branch, abc_current);
    assert_eq!(target, "abc");

    history.go_to(&mut target, abdef_branch, abdef_current);
    assert_eq!(target, "abdef");

    println!("{}", history.display());
}
source§

impl<A: ToString, S> History<A, S>

source

pub fn undo_string(&self) -> Option<String>

Returns the string of the action which will be undone in the next call to History::undo.

source

pub fn redo_string(&self) -> Option<String>

Returns the string of the action which will be redone in the next call to History::redo.

Trait Implementations§

source§

impl<A: Clone, S: Clone> Clone for History<A, S>

source§

fn clone(&self) -> History<A, S>

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<A: Debug, S: Debug> Debug for History<A, S>

source§

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

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

impl<A> Default for History<A>

source§

fn default() -> History<A>

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

impl<'de, A, S> Deserialize<'de> for History<A, S>where A: Deserialize<'de>, S: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a, A, S> From<&'a History<A, S>> for Display<'a, A, S>

source§

fn from(history: &'a History<A, S>) -> Self

Converts to this type from the input type.
source§

impl<'a, A, S> From<&'a mut History<A, S>> for Checkpoint<'a, A, S>

source§

fn from(history: &'a mut History<A, S>) -> Self

Converts to this type from the input type.
source§

impl<'a, A, S> From<&'a mut History<A, S>> for Queue<'a, A, S>

source§

fn from(history: &'a mut History<A, S>) -> Self

Converts to this type from the input type.
source§

impl<A, F> From<History<A, F>> for Record<A, F>

source§

fn from(history: History<A, F>) -> Record<A, F>

Converts to this type from the input type.
source§

impl<A, S> From<Record<A, S>> for History<A, S>

source§

fn from(record: Record<A, S>) -> Self

Converts to this type from the input type.
source§

impl<A, S> Serialize for History<A, S>where A: Serialize, S: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<A, S> RefUnwindSafe for History<A, S>where A: RefUnwindSafe, S: RefUnwindSafe,

§

impl<A, S> Send for History<A, S>where A: Send, S: Send,

§

impl<A, S> Sync for History<A, S>where A: Sync, S: Sync,

§

impl<A, S> Unpin for History<A, S>where A: Unpin, S: Unpin,

§

impl<A, S> UnwindSafe for History<A, S>where A: UnwindSafe + RefUnwindSafe, S: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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<T> ToOwned for Twhere T: Clone,

§

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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,