Make update interface slightly more usable

This commit is contained in:
Francesco Magliocca 2020-11-09 21:06:05 +01:00
parent 4b1dd4caed
commit 114ea2309e
1 changed files with 10 additions and 4 deletions

View File

@ -45,7 +45,7 @@ impl VersionedCanvas {
}
}
pub fn update(&mut self, update_fn: impl FnOnce(&Canvas) -> Canvas) {
pub fn update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
// This is a linear history,
// so we first check if there are newer versions, if so
// this means we are in the past, so a change in the past destroys the future
@ -53,9 +53,15 @@ impl VersionedCanvas {
if self.has_newer_versions() {
self.versions = self.versions.take(self.curr_version + 1);
}
let new_version = update_fn(self.get());
self.versions.push_back(new_version);
self.curr_version = self.curr_version + 1;
let mut new_version = self.get().clone();
update_fn(&mut new_version);
// Only push new version if there has been an actual change in the vector
if !new_version.elements.ptr_eq(&self.get().elements) {
self.versions.push_back(new_version);
self.curr_version = self.curr_version + 1;
}
}
// Do inplace update, which will be irreversible