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 retain to Map #822

Merged
merged 9 commits into from Nov 13, 2021
Merged

Add retain to Map #822

merged 9 commits into from Nov 13, 2021

Conversation

deankarn
Copy link
Contributor

@deankarn deankarn commented Nov 12, 2021

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.

These new retain functions will also only be included when compiling with Rust 1.53.0+ when retain was added to the BTreeMap in the std lib https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#stabilized-apis

Dean Karn added 6 commits November 11, 2021 20:24
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.
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!

build.rs Outdated Show resolved Hide resolved
src/map.rs Outdated
Comment on lines 237 to 261
#[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);
}
Copy link
Member

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?)

Copy link
Contributor Author

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.

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.

Looks good!

@dtolnay dtolnay merged commit 37da27f into serde-rs:master Nov 13, 2021
@dtolnay
Copy link
Member

dtolnay commented Nov 13, 2021

Published in 1.0.70.

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.

None yet

2 participants