-
Notifications
You must be signed in to change notification settings - Fork 5
Description
The Edit trait contains methods for all the stages of an edit's lifecycle except for one curious exception, the dropping of an edit. Perhaps it is assumed that there is nothing special to be done when an Edit is dropped, but sometimes there might be. So long as an Edit knows that it might later be undone, it might choose not to fully commit to its action.
For example, consider how a Slab has a key for each item. So long as those keys are being held, they will not be reused, but as soon as the key is removed it can potentially be reused for something else. Now imagine a data structure built using a Slab and using keys to store connections between nodes in the structure. If an Edit were to delete a node in that structure, it might be better to keep the key for that node and not remove it, because this allows the edit to be completely undone, restoring the original key. This might not be possible if the edit had removed the key from the slab and the slab had later reused that key for a different node.
So imagine this hypothetical version of the Edit trait:
pub trait Edit {
type Target;
type Output;
fn edit(&mut self, target: &mut Self::Target) -> Self::Output;
fn undo(&mut self, target: &mut Self::Target) -> Self::Output;
fn redo(&mut self, target: &mut Self::Target) -> Self::Output {
self.edit(target)
}
fn merge(&mut self, other: Self) -> Merged<Self>
where
Self: Sized,
{
Merged::No(other)
}
/// Do whatever is necessary when this edit is about to leave its record/history.
/// Calls to `undo` and `redo` become invalid after this, so clean up any resources
/// in `target` that are associated with this edit.
fn drop(&mut self, target: &mut Self::Target) {
}
}Now, an implementation of Edit might allow resources to linger in the target (such as slab keys) so long as the edit remains in the record and may need those resources for future undo/redo operations, and then when Edit::drop is called the edit can finally clean up the last traces of the edit from the target.
If it is unacceptable to add a method to the Edit trait, then a similar effect might be achieved by adding a Drop event that is emitted whenever an edit is dropped.