Merge pull request 822 from deankarn/master

This commit is contained in:
David Tolnay 2021-11-12 19:03:43 -08:00
commit 37da27fc77
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 30 additions and 0 deletions

View File

@ -31,6 +31,12 @@ fn main() {
if minor < 45 {
println!("cargo:rustc-cfg=no_btreemap_remove_entry");
}
// BTreeMap::retain
// https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#stabilized-apis
if minor < 53 {
println!("cargo:rustc-cfg=no_btreemap_retain");
}
}
fn rustc_minor_version() -> Option<u32> {

View File

@ -234,6 +234,19 @@ impl Map<String, Value> {
}
}
/// Retains only the elements specified by the predicate.
///
/// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)`
/// returns `false`.
#[cfg(not(no_btreemap_retain))]
#[inline]
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&String, &mut Value) -> bool,
{
self.map.retain(f);
}
/// Gets an iterator over the values of the map.
#[inline]
pub fn values(&self) -> Values {

View File

@ -34,3 +34,14 @@ fn test_append() {
assert_eq!(keys, EXPECTED);
assert!(val.is_empty());
}
#[cfg(not(no_btreemap_retain))]
#[test]
fn test_retain() {
let mut v: Value = from_str(r#"{"b":null,"a":null,"c":null}"#).unwrap();
let val = v.as_object_mut().unwrap();
val.retain(|k, _| k.as_str() != "b");
let keys: Vec<_> = val.keys().collect();
assert_eq!(keys, &["a", "c"]);
}