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

Add more removal methods to OccupiedEntry #1179

Merged
merged 2 commits into from
Aug 23, 2024
Merged

Conversation

GREsau
Copy link
Contributor

@GREsau GREsau commented Aug 19, 2024

serde_json::Map has several methods for removing an entry:

  • .remove(key)
  • .swap_remove(key) (with preserve_order)
  • .shift_remove(key) (with preserve_order)
  • .remove_entry(key)
  • .swap_remove_entry(key) (with preserve_order)
  • .shift_remove_entry(key) (with preserve_order)

But poor serde_json::map::OccupiedEntry only has .remove().

This means we can't easily/efficiently get the owned key from an OccupiedEntry - we have to either borrow and clone it, or call .remove_entry(key) on the original map, which will re-hash the key. Neither of these are catastrophic, but it's extra work we can avoid, since the underlying OccupiedEntry for both BTreeMap and IndexMap have a remove_entry method.

Similarly, with preserve_order enabled, we can't easily remove the entry without messing up the order of later entries, since OccupiedEntry::remove() uses swap_remove rather than shift_remove.

Hence, this PR adds the five missing removal methods to serde_json::map::OccupiedEntry

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dtolnay dtolnay merged commit f287a3b into serde-rs:master Aug 23, 2024
16 checks passed
@GREsau GREsau deleted the patch-1 branch August 23, 2024 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants