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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

keithamus
Copy link

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
Author

Thoughts on this one?

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