[][src]Struct redo::Checkpoint

pub struct Checkpoint<'a, T, C> { /* fields omitted */ }

A checkpoint wrapper.

Wraps a record or history and gives it checkpoint functionality. This allows the record or history to cancel all changes made since creating the checkpoint.

Examples

struct Add(char);

impl Command<String> for Add {
    type Error = &'static str;

    fn apply(&mut self, s: &mut String) -> Result<(), Self::Error> {
        s.push(self.0);
        Ok(())
    }

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

fn main() -> Result<(), &'static str> {
    let mut record = Record::default();
    let mut cp = record.checkpoint();
    cp.apply(Add('a'))?;
    cp.apply(Add('b'))?;
    cp.apply(Add('c'))?;
    assert_eq!(cp.as_receiver(), "abc");
    cp.cancel()?;
    assert_eq!(record.as_receiver(), "");
    Ok(())
}

Methods

impl<'a, T, C> Checkpoint<'a, T, C>[src]

pub fn new(inner: &'a mut T) -> Checkpoint<'a, T, C>[src]

Returns a checkpoint.

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

Reserves capacity for at least additional more commands in the checkpoint.

Panics

Panics if the new capacity overflows usize.

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

Returns the capacity of the checkpoint.

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

Returns the number of commands in the checkpoint.

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

Returns true if the checkpoint is empty.

pub fn commit(self)[src]

Commits the changes and consumes the checkpoint.

impl<R, C: Command<R>, F: FnMut(Signal), '_> Checkpoint<'_, Record<R, C, F>, C>[src]

pub fn apply(&mut self, command: C) -> Result<(), C::Error>[src]

Calls the apply method.

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

Calls the undo method.

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

Calls the redo method.

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

Calls the go_to method.

pub fn extend(
    &mut self,
    commands: impl IntoIterator<Item = C>
) -> Result<(), C::Error>
[src]

Calls the extend method.

pub fn cancel(self) -> Result<(), C::Error>[src]

Cancels the changes and consumes the checkpoint.

Errors

If an error occur when canceling the changes, the error is returned and the remaining commands are not canceled.

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

Returns a checkpoint.

pub fn queue(&mut self) -> Queue<Record<R, C, F>, C>[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.

impl<R, C: Command<R>, F: FnMut(Signal), '_> Checkpoint<'_, History<R, C, F>, C>[src]

pub fn apply(&mut self, command: C) -> Result<(), C::Error>[src]

Calls the apply method.

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

Calls the undo method.

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

Calls the redo method.

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

Calls the go_to method.

pub fn extend(
    &mut self,
    commands: impl IntoIterator<Item = C>
) -> Result<(), C::Error>
[src]

Calls the extend method.

pub fn cancel(self) -> Result<(), C::Error>[src]

Cancels the changes and consumes the checkpoint.

Errors

If an error occur when canceling the changes, the error is returned and the remaining commands are not canceled.

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

Returns a checkpoint.

pub fn queue(&mut self) -> Queue<History<R, C, F>, C>[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.

Trait Implementations

impl<R, C: Command<R>, F: FnMut(Signal), '_> AsMut<R> for Checkpoint<'_, Record<R, C, F>, C>[src]

impl<R, C: Command<R>, F: FnMut(Signal), '_> AsMut<R> for Checkpoint<'_, History<R, C, F>, C>[src]

impl<R, C: Command<R>, F: FnMut(Signal), '_> AsRef<R> for Checkpoint<'_, Record<R, C, F>, C>[src]

impl<R, C: Command<R>, F: FnMut(Signal), '_> AsRef<R> for Checkpoint<'_, History<R, C, F>, C>[src]

impl<'a, T, C> From<&'a mut T> for Checkpoint<'a, T, C>[src]

impl<'a, T: Debug, C: Debug> Debug for Checkpoint<'a, T, C>[src]

Auto Trait Implementations

impl<'a, T, C> Send for Checkpoint<'a, T, C> where
    C: Send,
    T: Send

impl<'a, T, C> Sync for Checkpoint<'a, T, C> where
    C: Sync,
    T: Sync

Blanket Implementations

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

impl<T> From for T[src]

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

type Error = Infallible

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

The type returned in the event of a conversion error.