[−][src]Struct redo::History
A history of commands.
Unlike Record which maintains a linear undo history, History maintains an undo tree containing every edit made to the receiver. By switching between different branches in the tree, the user can get to any previous state of the receiver.
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 history = History::default(); history.apply(Add('a'))?; history.apply(Add('b'))?; history.apply(Add('c'))?; let abc = history.root(); assert_eq!(history.as_receiver(), "abc"); history.go_to(abc, 1).unwrap()?; assert_eq!(history.as_receiver(), "a"); history.apply(Add('f'))?; history.apply(Add('g'))?; assert_eq!(history.as_receiver(), "afg"); history.go_to(abc, 3).unwrap()?; assert_eq!(history.as_receiver(), "abc"); Ok(()) }
Methods
impl<R, C: Command<R>> History<R, C>
[src]
pub fn new(
receiver: impl Into<R>
) -> History<R, C>
[src]
receiver: impl Into<R>
) -> History<R, C>
Returns a new history.
pub fn builder() -> HistoryBuilder<R, C>
[src]
Returns a builder for a history.
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 history.
pub fn len(&self) -> usize
[src]
Returns the number of commands in the current branch of the history.
pub fn is_empty(&self) -> bool
[src]
Returns true
if the current branch of the history is empty.
pub fn limit(&self) -> usize
[src]
Returns the limit of the history.
pub fn set_limit(&mut self, limit: usize) -> usize
[src]
Sets the limit of the history and returns the new limit.
If this limit is reached it will start popping of commands at the beginning of the history 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> where
C: 'static,
R: 'static,
[src]
&mut self,
f: impl FnMut(Signal) + Send + Sync + 'static
) -> Option<impl FnMut(Signal) + Send + Sync + 'static> where
C: 'static,
R: 'static,
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 history can undo.
pub fn can_redo(&self) -> bool
[src]
Returns true
if the history 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<(), C::Error>>
[src]
Revert the changes done to the receiver since the saved state.
pub fn root(&self) -> usize
[src]
Returns the current branch.
pub fn current(&self) -> usize
[src]
Returns the position of the current command.
pub fn clear(&mut self)
[src]
Removes all commands from the history without undoing them.
pub fn apply(&mut self, command: C) -> Result<(), C::Error>
[src]
Pushes the command to the top of the history and executes its apply
method.
Errors
If an error occur when executing apply
the error is returned
and the state of the history is left unchanged.
pub fn undo(&mut self) -> Option<Result<(), C::Error>>
[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 history is left unchanged.
pub fn redo(&mut self) -> Option<Result<(), C::Error>>
[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 history is left unchanged.
pub fn go_to(
&mut self,
branch: usize,
current: usize
) -> Option<Result<(), C::Error>>
[src]
&mut self,
branch: usize,
current: usize
) -> Option<Result<(), C::Error>>
Repeatedly calls undo
or redo
until the command in branch
at current
is reached.
Errors
If an error occur when executing undo
or redo
the error is returned
and the state of the history is left unchanged.
pub fn time_travel<Tz: TimeZone>(
&mut self,
to: &DateTime<Tz>
) -> Option<Result<(), C::Error>>
[src]
&mut self,
to: &DateTime<Tz>
) -> Option<Result<(), C::Error>>
Go back or forward in time.
pub fn extend(
&mut self,
commands: impl IntoIterator<Item = C>
) -> Result<(), C::Error>
[src]
&mut self,
commands: impl IntoIterator<Item = C>
) -> Result<(), C::Error>
Applies each command in the iterator.
Errors
If an error occur when executing apply
the error is returned
and the remaining commands in the iterator are discarded.
pub fn checkpoint(&mut self) -> Checkpoint<History<R, C>, C>
[src]
Returns a checkpoint.
pub fn queue(&mut self) -> Queue<History<R, C>, 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.
pub fn into_receiver(self) -> R
[src]
Consumes the history, returning the receiver
.
pub fn commands(
&self
) -> impl Iterator<Item = &C>
[src]
&self
) -> impl Iterator<Item = &C>
Returns an iterator over the commands in the current branch.
impl<R, C: Command<R> + ToString> History<R, C>
[src]
pub fn to_undo_string(&self) -> Option<String>
[src]
Returns the string of the command which will be undone in the next call to undo
.
pub fn to_redo_string(&self) -> Option<String>
[src]
Returns the string of the command which will be redone in the next call to redo
.
pub fn display(&self) -> Display<Self>
[src]
Returns a structure for configurable formatting of the record.
Trait Implementations
impl<R, C: Command<R>> AsMut<R> for History<R, C>
[src]
impl<R, C: Command<R>> AsRef<R> for History<R, C>
[src]
impl<R: Default, C: Command<R>> Default for History<R, C>
[src]
impl<R, C: Command<R>> From<R> for History<R, C>
[src]
impl<R, C: Command<R>> From<Record<R, C>> for History<R, C>
[src]
impl<R, C: Command<R>> From<History<R, C>> for Record<R, C>
[src]
impl<R: Debug, C: Debug + Command<R>> Debug for History<R, C>
[src]
impl<R, C: Command<R> + Display> Display for History<R, C>
[src]
impl<R, C: Command<R>> Serialize for History<R, C> where
R: Serialize,
C: Serialize,
[src]
R: Serialize,
C: Serialize,
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de, R, C: Command<R>> Deserialize<'de> for History<R, C> where
R: Deserialize<'de>,
C: Deserialize<'de>,
[src]
R: Deserialize<'de>,
C: Deserialize<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
Auto Trait Implementations
impl<R, C> Send for History<R, C> where
C: Send,
R: Send,
C: Send,
R: Send,
impl<R, C> Sync for History<R, C> where
C: Sync,
R: Sync,
C: Sync,
R: Sync,
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,