khonsulabs / bonsaidb

A developer-friendly document database that grows with you, written in Rust
https://bonsaidb.io/
Apache License 2.0
1.03k stars 37 forks source link

Allow `modify_async` to return a value. #312

Closed ModProg closed 11 months ago

ModProg commented 11 months ago

I'm using CollectionDocument::modify_async to modify a HashSet::remove and want to pass the result of that back up to the caller.

I'd like to be able to do

let was_removed: bool = document.modify_async(db, move |data| {
  data.set.remove(&value)
}).await?;
ModProg commented 11 months ago

Same would be nice for #103

ecton commented 11 months ago

The initial idea is good and easy, will be pushed shortly.

Same would be nice for #103

Unfortunately, this isn't the same case. With this API, the update function may never be called if the insert function is used instead. Since the insert function already has a return type, we'd have to make the actual result be wrapped in an option, and I'm not sure I like that very much.

ModProg commented 11 months ago

yeah, makes sense, but my usage actually is the upsert, but I can just keep my workaround of trying to insert the default value first.

I'd be fine with having the Option though, it'd be the same as calling insert on, e.g., a HashMap.

ecton commented 11 months ago

it'd be the same as calling insert on, e.g., a HashMap

The NamedCollection::entry API is inspired by HashMap::entry(), which operates the way it does based on how HashMap's and_modify works.