Struct redo::Checkpoint
source · pub struct Checkpoint<'a, T: 'a, C> { /* private fields */ }
Expand description
A checkpoint wrapper.
Wraps a Record or History and gives it checkpoint functionality.
Examples
#[derive(Debug)]
struct Add(char);
impl Command<String> for Add {
type Error = Box<dyn error::Error>;
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<(), Error<String, Add>> {
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(())
}
Implementations
sourceimpl<'a, R, C: Command<R>> Checkpoint<'a, Record<R, C>, C>
impl<'a, R, C: Command<R>> Checkpoint<'a, Record<R, C>, C>
sourcepub fn go_to(&mut self, cursor: usize) -> Option<Result<(), Error<R, C>>>
pub fn go_to(&mut self, cursor: usize) -> Option<Result<(), Error<R, C>>>
Calls the go_to
method.
sourcepub fn cancel(self) -> Result<(), Error<R, C>>
pub fn cancel(self) -> Result<(), Error<R, C>>
Cancels the changes and consumes the checkpoint.
Errors
If an error occur when canceling the changes, the error is returned together with the command.
sourcepub fn checkpoint(&mut self) -> Checkpoint<'_, Record<R, C>, C>
pub fn checkpoint(&mut self) -> Checkpoint<'_, Record<R, C>, C>
Returns a checkpoint.
sourcepub fn as_receiver(&self) -> &R
pub fn as_receiver(&self) -> &R
Returns a reference to the receiver
.
sourcepub fn as_mut_receiver(&mut self) -> &mut R
pub fn as_mut_receiver(&mut self) -> &mut R
Returns a mutable reference to the receiver
.
This method should only be used when doing changes that should not be able to be undone.
sourceimpl<'a, R, C: Command<R>> Checkpoint<'a, History<R, C>, C>
impl<'a, R, C: Command<R>> Checkpoint<'a, History<R, C>, C>
sourcepub fn go_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<(), Error<R, C>>>
pub fn go_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<(), Error<R, C>>>
Calls the go_to
method.
sourcepub fn cancel(self) -> Result<(), Error<R, C>>
pub fn cancel(self) -> Result<(), Error<R, C>>
Cancels the changes and consumes the checkpoint.
Errors
If an error occur when canceling the changes, the error is returned together with the command.
sourcepub fn checkpoint(&mut self) -> Checkpoint<'_, History<R, C>, C>
pub fn checkpoint(&mut self) -> Checkpoint<'_, History<R, C>, C>
Returns a checkpoint.
sourcepub fn as_receiver(&self) -> &R
pub fn as_receiver(&self) -> &R
Returns a reference to the receiver
.
sourcepub fn as_mut_receiver(&mut self) -> &mut R
pub fn as_mut_receiver(&mut self) -> &mut R
Returns a mutable reference to the receiver
.
This method should only be used when doing changes that should not be able to be undone.