Struct undo::Checkpoint
source · pub struct Checkpoint<'a, T: 'a, R> { /* 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 {
fn apply(&mut self, s: &mut String) -> Result<(), Box<dyn Error + Send + Sync>> {
s.push(self.0);
Ok(())
}
fn undo(&mut self, s: &mut String) -> Result<(), Box<dyn Error + Send + Sync>> {
self.0 = s.pop().ok_or("`s` is empty")?;
Ok(())
}
}
fn main() -> Result<(), Box<dyn Error>> {
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§
source§impl<'a, R> Checkpoint<'a, Record<R>, R>
impl<'a, R> Checkpoint<'a, Record<R>, R>
sourcepub fn apply(
&mut self,
command: impl Command<R> + 'static
) -> Result<(), Error<R>>where
R: 'static,
pub fn apply(
&mut self,
command: impl Command<R> + 'static
) -> Result<(), Error<R>>where
R: 'static,
Calls the apply
method.
sourcepub fn cancel(self) -> Result<(), Error<R>>
pub fn cancel(self) -> Result<(), Error<R>>
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>, R>
pub fn checkpoint(&mut self) -> Checkpoint<'_, Record<R>, R>
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.
source§impl<'a, R> Checkpoint<'a, History<R>, R>
impl<'a, R> Checkpoint<'a, History<R>, R>
sourcepub fn apply(
&mut self,
command: impl Command<R> + 'static
) -> Result<(), Error<R>>where
R: 'static,
pub fn apply(
&mut self,
command: impl Command<R> + 'static
) -> Result<(), Error<R>>where
R: 'static,
Calls the apply
method.
sourcepub fn go_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<(), Error<R>>>where
R: 'static,
pub fn go_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<(), Error<R>>>where
R: 'static,
Calls the go_to
method.
sourcepub fn cancel(self) -> Result<(), Error<R>>where
R: 'static,
pub fn cancel(self) -> Result<(), Error<R>>where
R: 'static,
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>, R>
pub fn checkpoint(&mut self) -> Checkpoint<'_, History<R>, R>
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.