Struct undo::history::History

source ·
pub struct History<E, S = ()> { /* private fields */ }
Expand description

A history tree of Edit commands.

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

See this example for an interactive view of the history tree.

Examples

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

history.edit(&mut target, Add('a'));
history.edit(&mut target, Add('b'));
history.edit(&mut target, Add('c'));
let abc = history.head();

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

// Instead of discarding 'b' and 'c', a new branch is created.
history.edit(&mut target, Add('f'));
history.edit(&mut target, Add('g'));
assert_eq!(target, "afg");

// We can now switch back to the original branch.
history.go_to(&mut target, abc);
assert_eq!(target, "abc");

Implementations§

source§

impl<E> History<E>

source

pub fn new() -> History<E>

Returns a new history.

source§

impl<E, S> History<E, S>

source

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

Returns a new history builder.

source

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

Reserves capacity for at least additional more edits.

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 edits 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 event 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 saved(&self) -> Option<At>

Return the position of the saved state.

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 head(&self) -> At

Returns the current position in the history.

source

pub fn next_branch_head(&self) -> Option<At>

Returns the head of the next branch in the history.

This will be the first edit that was stored in the branch. This can be used in combination with History::go_to to go to the next branch.

source

pub fn prev_branch_head(&self) -> Option<At>

Returns the head of the previous branch in the history.

This will be the first edit that was stored in the branch. This can be used in combination with History::go_to to go to the previous branch.

source

pub fn get_entry(&self, index: usize) -> Option<&Entry<E>>

Returns the entry at the index in the current root branch.

Use History::get_branch if you want to get entry from other branches.

source

pub fn entries(&self) -> impl Iterator<Item = &Entry<E>>

Returns an iterator over the entries in the current root branch.

source

pub fn get_branch(&self, id: usize) -> Option<&Branch<E>>

Returns the branch with the given id.

source

pub fn branches(&self) -> impl Iterator<Item = (&usize, &Branch<E>)>

Returns an iterator over the branches in the history.

This does not include the current root branch.

source

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

Returns a queue.

source

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

Returns a checkpoint.

source

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

Returns a structure for configurable formatting of the history.

source§

impl<E, S: Slot> History<E, S>

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 edits from the history without undoing them.

source§

impl<E: Edit, S: Slot> History<E, S>

source

pub fn edit(&mut self, target: &mut E::Target, edit: E) -> E::Output

Pushes the Edit to the top of the history and executes its Edit::edit method.

source

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

Calls the Edit::undo method for the active edit and sets the previous one as the new active one.

source

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

Calls the Edit::redo method for the active edit and sets the next one as the new active one.

source

pub fn go_to(&mut self, target: &mut E::Target, at: At) -> Vec<E::Output>

Repeatedly calls Edit::undo or Edit::redo until the edit at at is reached.

source§

impl<E: Display, S> History<E, S>

source

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

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

source

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

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

Trait Implementations§

source§

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

source§

fn clone(&self) -> History<E, 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<E: Debug, S: Debug> Debug for History<E, S>

source§

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

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

impl<E> Default for History<E>

source§

fn default() -> History<E>

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

impl<'de, E, S> Deserialize<'de> for History<E, S>where E: 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, E, S> From<&'a History<E, S>> for Display<'a, E, S>

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl<E, S> Serialize for History<E, S>where E: 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<E, S> RefUnwindSafe for History<E, S>where E: RefUnwindSafe, S: RefUnwindSafe,

§

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

§

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

§

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

§

impl<E, S> UnwindSafe for History<E, S>where E: 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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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<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.
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.
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>,