From 418753f01eef29b1266cc3aefa512f90060a9797 Mon Sep 17 00:00:00 2001 From: codingp110 Date: Mon, 21 Jul 2025 21:57:06 +0530 Subject: [PATCH] refactor!: `Box` changeset in `StoreErrorWithDump` As mentioned in the corresponding issue, `Box`ing helps reduce memory overhead while passing the enum around and also to limit the size of enums with `StoreErrorWithDump` as a variant. Breaking: The enum `StoreErrorWithDump` has been changed. --- crates/file_store/src/store.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/crates/file_store/src/store.rs b/crates/file_store/src/store.rs index 1bea6c83e..7e1867926 100644 --- a/crates/file_store/src/store.rs +++ b/crates/file_store/src/store.rs @@ -118,7 +118,7 @@ where /// let mut new_store = /// Store::create(&MAGIC_BYTES, &new_file_path).expect("must create new file"); /// if let Some(aggregated_changeset) = changeset { - /// new_store.append(&aggregated_changeset)?; + /// new_store.append(aggregated_changeset.as_ref())?; /// } /// // The following will overwrite the original file. You will loose the corrupted /// // portion of the original file forever. @@ -152,7 +152,7 @@ where f.read_exact(&mut magic_buf)?; if magic_buf != magic { return Err(StoreErrorWithDump { - changeset: Option::::None, + changeset: Option::>::None, error: StoreError::InvalidMagicBytes { got: magic_buf, expected: magic.to_vec(), @@ -194,7 +194,7 @@ where Ok(aggregated_changeset) } Err(iter_error) => Err(StoreErrorWithDump { - changeset: aggregated_changeset, + changeset: aggregated_changeset.map(Box::new), error: iter_error, }), }, @@ -220,7 +220,7 @@ where Self::create(magic, file_path) .map(|store| (store, Option::::None)) .map_err(|err: StoreError| StoreErrorWithDump { - changeset: Option::::None, + changeset: Option::>::None, error: err, }) } @@ -257,7 +257,7 @@ where #[derive(Debug)] pub struct StoreErrorWithDump { /// The partially-aggregated changeset. - pub changeset: Option, + pub changeset: Option>, /// The [`StoreError`] pub error: StoreError, @@ -266,7 +266,7 @@ pub struct StoreErrorWithDump { impl From for StoreErrorWithDump { fn from(value: io::Error) -> Self { Self { - changeset: Option::::None, + changeset: Option::>::None, error: StoreError::Io(value), } } @@ -371,7 +371,7 @@ mod test { changeset, error: StoreError::Bincode(_), }) => { - assert_eq!(changeset, Some(test_changesets)) + assert_eq!(changeset, Some(Box::new(test_changesets))) } unexpected_res => panic!("unexpected result: {unexpected_res:?}"), } @@ -399,7 +399,7 @@ mod test { changeset, error: StoreError::Bincode(_), }) => { - assert_eq!(changeset, Some(test_changesets)) + assert_eq!(changeset, Some(Box::new(test_changesets))) } unexpected_res => panic!("unexpected result: {unexpected_res:?}"), } @@ -500,10 +500,14 @@ mod test { .expect_err("should fail to aggregate"); assert_eq!( err.changeset, - changesets.iter().cloned().reduce(|mut acc, cs| { - Merge::merge(&mut acc, cs); - acc - }), + changesets + .iter() + .cloned() + .reduce(|mut acc, cs| { + Merge::merge(&mut acc, cs); + acc + }) + .map(Box::new), "should recover all changesets that are written in full", ); // Remove file and start again