pub struct Record<R> { /* private fields */ }
Expand description
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) -> 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();
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(())
}
Implementations§
source§impl<R> Record<R>
impl<R> Record<R>
sourcepub fn builder() -> RecordBuilder<R>
pub fn builder() -> RecordBuilder<R>
Returns a builder for a record.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more commands.
Panics
Panics if the new capacity overflows usize.
sourcepub fn set_limit(&mut self, limit: usize) -> usize
pub fn set_limit(&mut self, limit: usize) -> usize
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
.
sourcepub fn connect(&mut self, f: impl FnMut(Signal) + Send + Sync + 'static)
pub fn connect(&mut self, f: impl FnMut(Signal) + Send + Sync + 'static)
Sets how the signal should be handled when the state changes.
sourcepub fn set_saved(&mut self, saved: bool)
pub fn set_saved(&mut self, saved: bool)
Marks the receiver as currently being in a saved or unsaved state.
sourcepub fn is_saved(&self) -> bool
pub fn is_saved(&self) -> bool
Returns true
if the receiver is in a saved state, false
otherwise.
sourcepub fn revert(&mut self) -> Option<Result<(), Error<R>>>
pub fn revert(&mut self) -> Option<Result<(), Error<R>>>
Revert the changes done to the receiver since the saved state.
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,
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.
sourcepub fn into_receiver(self) -> R
pub fn into_receiver(self) -> R
Consumes the record, returning the receiver
.