From 5f14c117fd00a6bb8ecdd430a5313210da3d246d Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Mon, 4 Dec 2023 07:29:39 -0800 Subject: [PATCH] CollectionDocument::modify allows returning values Closes #312 --- CHANGELOG.md | 6 +++++ .../bonsaidb-core/src/document/collection.rs | 24 ++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a92fc92aa0..473cff98ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Breaking Changes + +- `CollectionDocument::modify`/`CollectionDocument::modify_async` now take an + additional parameter: the return type of the callback function. This result + from the call that succeeds in updating will be returned in `Ok`. + ### Added - `bonsaidb::client::Error` now implements diff --git a/crates/bonsaidb-core/src/document/collection.rs b/crates/bonsaidb-core/src/document/collection.rs index 5fce420e4e..40efd0a509 100644 --- a/crates/bonsaidb-core/src/document/collection.rs +++ b/crates/bonsaidb-core/src/document/collection.rs @@ -144,6 +144,8 @@ where /// Modifies `self`, automatically retrying the modification if the document /// has been updated on the server. /// + /// The contents of `Ok` will be the last value returned from `modifier`. + /// /// ## Data loss warning /// /// If you've modified `self` before calling this function and a conflict @@ -151,11 +153,11 @@ where /// fetched before retrying the process again. When you use this function, /// you should limit the edits to the value to within the `modifier` /// callback. - pub fn modify( + pub fn modify R + Send + Sync>( &mut self, connection: &Cn, mut modifier: Modifier, - ) -> Result<(), Error> + ) -> Result where C::Contents: Clone, { @@ -176,10 +178,10 @@ where } })?; } - modifier(&mut *self); + let result = modifier(&mut *self); match self.update(connection) { Err(Error::DocumentConflict(..)) => {} - other => return other, + other => return other.map(|()| result), } } } @@ -187,6 +189,8 @@ where /// Modifies `self`, automatically retrying the modification if the document /// has been updated on the server. /// + /// The contents of `Ok` will be the last value returned from `modifier`. + /// /// ## Data loss warning /// /// If you've modified `self` before calling this function and a conflict @@ -194,11 +198,15 @@ where /// fetched before retrying the process again. When you use this function, /// you should limit the edits to the value to within the `modifier` /// callback. - pub async fn modify_async( + pub async fn modify_async< + R, + Cn: AsyncConnection, + Modifier: FnMut(&mut Self) -> R + Send + Sync, + >( &mut self, connection: &Cn, mut modifier: Modifier, - ) -> Result<(), Error> + ) -> Result where C::Contents: Clone, { @@ -218,10 +226,10 @@ where Err(err) => err, })?; } - modifier(&mut *self); + let result = modifier(&mut *self); match self.update_async(connection).await { Err(Error::DocumentConflict(..)) => {} - other => return other, + other => return other.map(|()| result), } } }