-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add return val to replace_bucket_with
replace_entry_with
#419
base: master
Are you sure you want to change the base?
Conversation
If a user has a `RawOccupiedEntryMut`, they should be able to use `replace_entry_with` to conditionally remove it. However, if a user wishes to extract data from this entity during the removal process, the current api necessitates the use of `unwrap`: ```rust /// Extract the coolness of an item, possibly destroying it fn extract_coolness<V>(v: V) -> (Option<V>, Coolness) { /// ... } let entry: RawOccupiedEntryMut< _, _, _, _> = unimplemented!(); let mut coolness = None; let entry = entry.replace_entry_with(|_k, v| { let (maybe_v, rcool) = extract_coolness(v); coolness = Some(rcool); maybe_v }); let coolness = coolness.unwrap(); // unwrap, ew // Do some stuff with the coolness it doesn't really matter what inspect_coolness(coolness); ``` This is because while `F` is bounded by `FnOnce`, it is under no obligation to call it, which in this example means there is no guarnatee that `coolness` is initialized. This proposed change introduces a new return value `R` which allows callers to transport this data out of `F`. Notably it also commits the `replace_entry_with` to calling `F`, since there is no other way to generate an `R`. With this, our example becomes: ```rust /// Extract the coolness of an item, possibly destroying it fn extract_coolness<V>(v: V) -> (Option<V>, Coolness) { /// ... } let entry: RawOccupiedEntryMut< _, _, _, _> = unimplemented!(); let (entry, coolness) = entry.replace_entry_with(|_k, v| extract_coolness(v)); // Do some stuff with the coolness it doesn't really matter what inspect_coolness(coolness); ```
Is there any interest in this change? |
☔ The latest upstream changes (presumably #425) made this pull request unmergeable. Please resolve the merge conflicts. |
I spent some time thinking about this change. I don't think it is beneficial because it makes the common case where you don't want to return a value more complicated since you need to extract the new |
It is only a |
Long-term, I am planning on deprecating the whole RawEntry API and replacing it with a |
I would like to deprecate the raw entry API in favor of |
If a user has a
RawOccupiedEntryMut
, they should be able to usereplace_entry_with
to conditionally remove it. However, if a user wishes to extract data from this entity during the removal process, the current api necessitates the use ofunwrap
:This is because while
F
is bounded byFnOnce
, it is under no obligation to call it, which in this example means there is no guarnatee thatcoolness
is initialized. This proposed change introduces a new return valueR
which allows callers to transport this data out ofF
. Notably it also commits thereplace_entry_with
to callingF
, since there is no other way to generate anR
. With this, our example becomes:An alternative to this change is to introduce a new API with a new name instead of modifying the existing one as this is a breaking change.