Struct 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 exceeds isize::MAX bytes.

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.

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)

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

Source

pub fn clear_saved(&mut self)

Clears the saved state of the target.

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 revert(&mut self, target: &mut E::Target) -> Vec<E::Output>

Revert the changes done to the target since the saved state.

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 duplicate of the value. Read more
1.0.0 · Source§

const 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> Freeze for History<E, S>
where S: Freeze,

§

impl<E, S> RefUnwindSafe for History<E, S>

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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 T
where T: for<'de> Deserialize<'de>,