Fix and improve Persist::commit
method
Previously, regardless of whether writing to persistence backend is successful or not, the logic always cleared `self.staged`. This is changed to only clear `self.staged` after successful write. Additionally, the written changeset (if any) is returned, and `PersistBackend::write_changes` will not be called if `self.staged` is empty.
This commit is contained in:
parent
e01d17d59b
commit
96b1075132
@ -41,11 +41,19 @@ where
|
|||||||
|
|
||||||
/// Commit the staged changes to the underlying persistance backend.
|
/// Commit the staged changes to the underlying persistance backend.
|
||||||
///
|
///
|
||||||
|
/// Changes that are committed (if any) are returned.
|
||||||
|
///
|
||||||
|
/// # Error
|
||||||
|
///
|
||||||
/// Returns a backend-defined error if this fails.
|
/// Returns a backend-defined error if this fails.
|
||||||
pub fn commit(&mut self) -> Result<(), B::WriteError> {
|
pub fn commit(&mut self) -> Result<Option<C>, B::WriteError> {
|
||||||
let mut temp = C::default();
|
if self.stage.is_empty() {
|
||||||
core::mem::swap(&mut temp, &mut self.stage);
|
return Ok(None);
|
||||||
self.backend.write_changes(&temp)
|
}
|
||||||
|
self.backend
|
||||||
|
.write_changes(&self.stage)
|
||||||
|
// if written successfully, take and return `self.stage`
|
||||||
|
.map(|_| Some(core::mem::take(&mut self.stage)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user