-
Notifications
You must be signed in to change notification settings - Fork 564
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 retain to Map #822
Add retain to Map #822
Conversation
This adds the `retain` function to the Map type which calls the inner maps, Either indexmap or std BTreeMap's, retain. I'm trying to mutate a Value::Object's Map in-place and could't find a nice way without the retain function.
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.
Thanks!
src/map.rs
Outdated
#[cfg(all(feature = "preserve_order", not(no_btreemap_retain)))] | ||
/// Retains only the elements specified by the predicate. | ||
/// | ||
/// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`. | ||
/// The elements are visited in ascending key order. | ||
#[inline] | ||
pub fn retain<F>(&mut self, f: F) | ||
where | ||
F: FnMut(&String, &mut Value) -> bool, | ||
{ | ||
self.map.retain(f); | ||
} | ||
|
||
#[cfg(all(not(feature = "preserve_order"), not(no_btreemap_retain)))] | ||
/// Retains only the elements specified by the predicate. | ||
/// | ||
/// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`. | ||
/// The elements are visited in ascending key order. | ||
#[inline] | ||
pub fn retain<F>(&mut self, f: F) | ||
where | ||
F: FnMut(&String, &mut Value) -> bool, | ||
{ | ||
self.map.retain(f); | ||
} |
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.
Why are there two identical copies of this function? (Maybe I'm not observant enough to notice the difference — what is the difference between these?)
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.
Thanks for catching my mistake @dtolnay
They were originally different but eventually became the same. Combination of late night coding and distraction of kids. It should be good to go now.
Co-authored-by: David Tolnay <dtolnay@gmail.com>
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.
Looks good!
Published in 1.0.70. |
This adds the
retain
function to the Map type which calls the innermaps, Either
indexmap
or stdBTreeMap
's, retain.I'm trying to mutate a
Value::Object
's Map in-place and could't find anice way without the
retain
function.These new
retain
functions will also only be included when compiling with Rust 1.53.0+ whenretain
was added to the BTreeMap in the std lib https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#stabilized-apis