Skip to content

Commit 96b1075

Browse files
committed
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.
1 parent e01d17d commit 96b1075

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

crates/chain/src/persist.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,19 @@ where
4141

4242
/// Commit the staged changes to the underlying persistance backend.
4343
///
44+
/// Changes that are committed (if any) are returned.
45+
///
46+
/// # Error
47+
///
4448
/// Returns a backend-defined error if this fails.
45-
pub fn commit(&mut self) -> Result<(), B::WriteError> {
46-
let mut temp = C::default();
47-
core::mem::swap(&mut temp, &mut self.stage);
48-
self.backend.write_changes(&temp)
49+
pub fn commit(&mut self) -> Result<Option<C>, B::WriteError> {
50+
if self.stage.is_empty() {
51+
return Ok(None);
52+
}
53+
self.backend
54+
.write_changes(&self.stage)
55+
// if written successfully, take and return `self.stage`
56+
.map(|_| Some(core::mem::take(&mut self.stage)))
4957
}
5058
}
5159

0 commit comments

Comments
 (0)