Skip to content
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

Conversation

keithamus
Copy link
Contributor

PR Type

Feature

PR Checklist

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the nightly rustfmt (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, and update_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 to update, 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 make update_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.

@keithamus keithamus force-pushed the implement-contains-key-update-update-or branch from 1d9b2a6 to f944258 Compare August 20, 2024 23:20
@keithamus keithamus force-pushed the implement-contains-key-update-update-or branch from f944258 to d1639b9 Compare August 20, 2024 23:21
@robjtede robjtede added A-session Project: actix-session B-semver-minor labels Aug 21, 2024
@keithamus
Copy link
Contributor Author

Thoughts on this one?

Comment on lines 219 to 221
key: &str,
updater: F,
default_value: T,
Copy link
Member

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 robjtede enabled auto-merge January 13, 2025 23:35
@robjtede robjtede added this pull request to the merge queue Jan 13, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 13, 2025
@robjtede robjtede added this pull request to the merge queue Jan 13, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 13, 2025
@robjtede robjtede added this pull request to the merge queue Jan 13, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 14, 2025
@robjtede robjtede added this pull request to the merge queue Jan 14, 2025
Merged via the queue into actix:master with commit 265b213 Jan 14, 2025
9 checks passed
@keithamus keithamus deleted the implement-contains-key-update-update-or branch January 14, 2025 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-session Project: actix-session B-semver-minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants