serde-rs / json

Strongly typed JSON library for Rust
Apache License 2.0
4.91k stars 560 forks source link

Add more removal methods to `OccupiedEntry` #1179

Closed GREsau closed 3 months ago

GREsau commented 3 months ago

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

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