-
Notifications
You must be signed in to change notification settings - Fork 212
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
implement contains_key, update, update_or #459
Merged
robjtede
merged 6 commits into
actix:master
from
keithamus:implement-contains-key-update-update-or
Jan 14, 2025
Merged
implement contains_key, update, update_or #459
robjtede
merged 6 commits into
actix:master
from
keithamus:implement-contains-key-update-update-or
Jan 14, 2025
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keithamus
force-pushed
the
implement-contains-key-update-update-or
branch
from
August 20, 2024 23:20
1d9b2a6
to
f944258
Compare
keithamus
force-pushed
the
implement-contains-key-update-update-or
branch
from
August 20, 2024 23:21
f944258
to
d1639b9
Compare
Thoughts on this one? |
robjtede
reviewed
Jan 13, 2025
actix-session/src/session.rs
Outdated
Comment on lines
219
to
221
key: &str, | ||
updater: F, | ||
default_value: T, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to match std's map_or
i'd like to have default value before the updater parameter
robjtede
approved these changes
Jan 13, 2025
github-merge-queue
bot
removed this pull request from the merge queue due to failed status checks
Jan 13, 2025
github-merge-queue
bot
removed this pull request from the merge queue due to failed status checks
Jan 13, 2025
github-merge-queue
bot
removed this pull request from the merge queue due to failed status checks
Jan 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Type
Feature
PR Checklist
cargo +nightly fmt
).Overview
It's quite common to want to update a value based on its existence in a map, and its current value. The typical case being incrementing counters. Right now this is a bit cumbersome, as you have to do a handle error states for serialize/deserialize and then an insert/update.
This PR adds
contains_key
,update
, andupdate_or
methods.contains_key
checks if a key exists in the map, delegating to the underlying HashMap. This makes it a little easier than using.entries()
.update
takes a key and a closure that takes the current value and sets the returned value. If the key doesn't exist, the closure is not called and the key is not inserted.update_or
is similar toupdate
, but takes a default value to insert if the key doesn't exist, kind of like an "emplace" or "upsert" or equivalent.My core use case is probably
update_or
, but the other two feel like a natural fit in terms of ergonomics, and are kind of required to makeupdate_or
work.I come with an open mind and I'm happy to hear feedback on the API design, or otherwise if you don't think this is a good fit feel free to close the PR.