Skip to content

Commit 4e0d349

Browse files
authored
Merge pull request #712 from dtolnay/remove_entry
Add Map::remove_entry
2 parents d3c6400 + 4c3300b commit 4e0d349

5 files changed

Lines changed: 82 additions & 2 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
strategy:
3131
fail-fast: false
3232
matrix:
33-
rust: [beta, stable, 1.36.0, 1.31.0]
33+
rust: [beta, stable, 1.45.0, 1.40.0, 1.36.0, 1.31.0]
3434
os: [ubuntu]
3535
include:
3636
- rust: stable

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ edition = "2018"
1414

1515
[dependencies]
1616
serde = { version = "1.0.100", default-features = false }
17-
indexmap = { version = "1.2", optional = true }
17+
indexmap = { version = "1.5", optional = true }
1818
itoa = { version = "0.4.3", default-features = false }
1919
ryu = "1.0"
2020

‎build.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::env;
2+
use std::process::Command;
3+
use std::str::{self, FromStr};
24

35
fn main() {
46
// Decide ideal limb width for arithmetic in the float parser. Refer to
@@ -12,4 +14,33 @@ fn main() {
1214
println!("cargo:rustc-cfg=limb_width_32");
1315
}
1416
}
17+
18+
let minor = match rustc_minor_version() {
19+
Some(minor) => minor,
20+
None => return,
21+
};
22+
23+
// BTreeMap::get_key_value
24+
// https://blog.rust-lang.org/2019/12/19/Rust-1.40.0.html#additions-to-the-standard-library
25+
if minor < 40 {
26+
println!("cargo:rustc-cfg=no_btreemap_get_key_value");
27+
}
28+
29+
// BTreeMap::remove_entry
30+
// https://blog.rust-lang.org/2020/07/16/Rust-1.45.0.html#library-changes
31+
if minor < 45 {
32+
println!("cargo:rustc-cfg=no_btreemap_remove_entry");
33+
}
34+
}
35+
36+
fn rustc_minor_version() -> Option<u32> {
37+
let rustc = env::var_os("RUSTC")?;
38+
let output = Command::new(rustc).arg("--version").output().ok()?;
39+
let version = str::from_utf8(&output.stdout).ok()?;
40+
let mut pieces = version.split('.');
41+
if pieces.next() != Some("rustc 1") {
42+
return None;
43+
}
44+
let next = pieces.next()?;
45+
u32::from_str(next).ok()
1546
}

‎src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ mod lib {
371371
pub use std::*;
372372
}
373373

374+
pub use self::core::ops::{Bound, RangeBounds};
374375
pub use self::core::cell::{Cell, RefCell};
375376
pub use self::core::clone::{self, Clone};
376377
pub use self::core::convert::{self, From, Into};

‎src/map.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,54 @@ impl Map<String, Value> {
122122
return self.map.remove(key);
123123
}
124124

125+
/// Removes a key from the map, returning the stored key and value if the
126+
/// key was previously in the map.
127+
///
128+
/// The key may be any borrowed form of the map's key type, but the ordering
129+
/// on the borrowed form *must* match the ordering on the key type.
130+
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)>
131+
where
132+
String: Borrow<Q>,
133+
Q: ?Sized + Ord + Eq + Hash,
134+
{
135+
#[cfg(any(feature = "preserve_order", not(no_btreemap_remove_entry)))]
136+
return self.map.remove_entry(key);
137+
#[cfg(all(
138+
not(feature = "preserve_order"),
139+
no_btreemap_remove_entry,
140+
not(no_btreemap_get_key_value),
141+
))]
142+
{
143+
let (key, _value) = self.map.get_key_value(key)?;
144+
let key = key.clone();
145+
let value = self.map.remove::<String>(&key)?;
146+
Some((key, value))
147+
}
148+
#[cfg(all(
149+
not(feature = "preserve_order"),
150+
no_btreemap_remove_entry,
151+
no_btreemap_get_key_value,
152+
))]
153+
{
154+
struct Key<'a, Q: ?Sized>(&'a Q);
155+
156+
impl<'a, Q: ?Sized> RangeBounds<Q> for Key<'a, Q> {
157+
fn start_bound(&self) -> Bound<&Q> {
158+
Bound::Included(self.0)
159+
}
160+
fn end_bound(&self) -> Bound<&Q> {
161+
Bound::Included(self.0)
162+
}
163+
}
164+
165+
let mut range = self.map.range(Key(key));
166+
let (key, _value) = range.next()?;
167+
let key = key.clone();
168+
let value = self.map.remove::<String>(&key)?;
169+
Some((key, value))
170+
}
171+
}
172+
125173
/// Moves all elements from other into Self, leaving other empty.
126174
#[inline]
127175
pub fn append(&mut self, other: &mut Self) {

0 commit comments

Comments
 (0)