Struct undo::record::Record

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

A linear record of Edit commands.

The record can roll the targets state backwards and forwards by using the undo and redo methods. In addition, the record can notify the user about changes to the stack or the target through Event. The user can give the record a function that is called each time the state changes by using the Builder.

When adding a new edit command to the record the previously undone commands will be discarded. If you want to keep all edits you can use History instead.

Examples

let mut target = String::new();
let mut record = Record::new();

record.edit(&mut target, Add('a'));
record.edit(&mut target, Add('b'));
record.edit(&mut target, Add('c'));
assert_eq!(target, "abc");

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

record.redo(&mut target);
record.redo(&mut target);
assert_eq!(target, "ab");

// 'c' will be discarded.
record.edit(&mut target, Add('d'));
assert_eq!(target, "abd");

Implementations§

source§

impl<E> Record<E>

source

pub fn new() -> Record<E>

Returns a new record.

source§

impl<E, S> Record<E, S>

source

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

Returns a new record 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 record.

source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the record as much as possible.

source

pub fn len(&self) -> usize

Returns the number of edits in the record.

source

pub fn is_empty(&self) -> bool

Returns true if the record is empty.

source

pub fn limit(&self) -> usize

Returns the limit of the record.

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 can_undo(&self) -> bool

Returns true if the record can undo.

source

pub fn can_redo(&self) -> bool

Returns true if the record can redo.

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<usize>

Returns the index of the saved state.

source

pub fn head(&self) -> usize

Returns the current index in the record.

source

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

Returns the entry at the index.

source

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

Returns an iterator over the entries.

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 record.

source§

impl<E, S: Slot> Record<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 record without undoing them.

source§

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

source

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

Pushes the edit on top of the record 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, index: usize) -> Vec<E::Output>

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

source§

impl<E: Display, S> Record<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 Record::undo.

source

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

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

Trait Implementations§

source§

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

source§

fn clone(&self) -> Record<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 Record<E, S>

source§

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

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

impl<E> Default for Record<E>

source§

fn default() -> Record<E>

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

impl<'de, E, S> Deserialize<'de> for Record<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 Record<E, S>> for Display<'a, E, S>

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

fn from(record: &'a mut Record<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 Record<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 Record<E, S>where E: RefUnwindSafe, S: RefUnwindSafe,

§

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

§

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

§

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

§

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