[][src]Struct undo::Record

pub struct Record<R> { /* fields omitted */ }

A record of commands.

The record can roll the receivers 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 receiver through signal. The user can give the record a function that is called each time the state changes by using the builder.

Examples

#[derive(Debug)]
struct Add(char);

impl Command<String> for Add {
    fn apply(&mut self, s: &mut String) -> undo::Result {
        s.push(self.0);
        Ok(())
    }

    fn undo(&mut self, s: &mut String) -> undo::Result {
        self.0 = s.pop().ok_or("`s` is empty")?;
        Ok(())
    }
}

fn main() -> undo::Result {
    let mut record = Record::default();
    record.apply(Add('a'))?;
    record.apply(Add('b'))?;
    record.apply(Add('c'))?;
    assert_eq!(record.as_receiver(), "abc");
    record.undo().unwrap()?;
    record.undo().unwrap()?;
    record.undo().unwrap()?;
    assert_eq!(record.as_receiver(), "");
    record.redo().unwrap()?;
    record.redo().unwrap()?;
    record.redo().unwrap()?;
    assert_eq!(record.as_receiver(), "abc");
    Ok(())
}

Methods

impl<R> Record<R>[src]

pub fn new(
    receiver: impl Into<R>
) -> Record<R>
[src]

Returns a new record.

pub fn builder() -> RecordBuilder<R>[src]

Returns a builder for a record.

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

Reserves capacity for at least additional more commands.

Panics

Panics if the new capacity overflows usize.

pub fn capacity(&self) -> usize[src]

Returns the capacity of the record.

pub fn len(&self) -> usize[src]

Returns the number of commands in the record.

pub fn is_empty(&self) -> bool[src]

Returns true if the record is empty.

pub fn limit(&self) -> usize[src]

Returns the limit of the record.

pub fn set_limit(&mut self, limit: usize) -> usize[src]

Sets the limit of the record and returns the new limit.

If this limit is reached it will start popping of commands at the beginning of the record when new commands are applied. No limit is set by default which means it may grow indefinitely.

If limit < len the first commands will be removed until len == limit. However, if the current active command is going to be removed, the limit is instead adjusted to len - active so the active command is not removed.

Panics

Panics if limit is 0.

pub fn connect(
    &mut self,
    f: impl FnMut(Signal) + Send + Sync + 'static
) -> Option<impl FnMut(Signal) + Send + Sync + 'static>
[src]

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

The previous signal handler is returned if it exists.

pub fn can_undo(&self) -> bool[src]

Returns true if the record can undo.

pub fn can_redo(&self) -> bool[src]

Returns true if the record can redo.

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

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

pub fn is_saved(&self) -> bool[src]

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

pub fn revert(&mut self) -> Option<Result>[src]

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

pub fn current(&self) -> usize[src]

Returns the position of the current command.

pub fn clear(&mut self)[src]

Removes all commands from the record without undoing them.

pub fn apply(
    &mut self,
    command: impl Command<R> + 'static
) -> Result where
    R: 'static, 
[src]

Pushes the command to the top of the record and executes its apply method.

Errors

If an error occur when executing apply the error is returned and the state of the record is left unchanged.

pub fn undo(&mut self) -> Option<Result>[src]

Calls the undo method for the active command and sets the previous one as the new active one.

Errors

If an error occur when executing undo the error is returned and the state of the record is left unchanged.

pub fn redo(&mut self) -> Option<Result>[src]

Calls the redo method for the active command and sets the next one as the new active one.

Errors

If an error occur when executing redo the error is returned and the state of the record is left unchanged.

pub fn go_to(&mut self, current: usize) -> Option<Result>[src]

Repeatedly calls undo or redo until the command at current is reached.

Errors

If an error occur when executing undo or redo the error is returned and the state of the record is left unchanged.

pub fn time_travel<Tz: TimeZone>(&mut self, to: &DateTime<Tz>) -> Option<Result>[src]

Go back or forward in time.

pub fn extend<C: Command<R> + 'static>(
    &mut self,
    commands: impl IntoIterator<Item = C>
) -> Result where
    R: 'static, 
[src]

Applies each command in the iterator.

Errors

If an error occur when executing apply the error is returned and the state of the record is left unchanged. The remaining commands in the iterator are discarded.

pub fn checkpoint(&mut self) -> Checkpoint<Record<R>, R>[src]

Returns a checkpoint.

pub fn queue(&mut self) -> Queue<Record<R>, R>[src]

Returns a queue.

pub fn as_receiver(&self) -> &R[src]

Returns a reference to the receiver.

pub fn as_mut_receiver(&mut self) -> &mut R[src]

Returns a mutable reference to the receiver.

This method should only be used when doing changes that should not be able to be undone.

pub fn into_receiver(self) -> R[src]

Consumes the record, returning the receiver.

Trait Implementations

impl<R: Default> Default for Record<R>[src]

impl<R> From<Record<R>> for History<R>[src]

impl<R> From<R> for Record<R>[src]

impl<R> From<History<R>> for Record<R>[src]

impl<R> AsMut<R> for Record<R>[src]

impl<R> AsRef<R> for Record<R>[src]

impl<R: Debug> Debug for Record<R>[src]

Auto Trait Implementations

impl<R> Send for Record<R> where
    R: Send

impl<R> Sync for Record<R> where
    R: Sync

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.