mirror of
https://github.com/serde-rs/json.git
synced 2025-12-30 05:10:37 +00:00
Merge pull request 822 from deankarn/master
This commit is contained in:
commit
37da27fc77
6
build.rs
6
build.rs
@ -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> {
|
||||
|
||||
13
src/map.rs
13
src/map.rs
@ -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 {
|
||||
|
||||
11
tests/map.rs
11
tests/map.rs
@ -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"]);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user