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 Map::remove_entry #712

Merged
merged 11 commits into from
Sep 30, 2020
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: [beta, stable, 1.36.0, 1.31.0]
rust: [beta, stable, 1.45.0, 1.40.0, 1.36.0, 1.31.0]
os: [ubuntu]
include:
- rust: stable
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ edition = "2018"

[dependencies]
serde = { version = "1.0.100", default-features = false }
indexmap = { version = "1.2", optional = true }
indexmap = { version = "1.5", optional = true }
itoa = { version = "0.4.3", default-features = false }
ryu = "1.0"

Expand Down
31 changes: 31 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::env;
use std::process::Command;
use std::str::{self, FromStr};

fn main() {
// Decide ideal limb width for arithmetic in the float parser. Refer to
Expand All @@ -12,4 +14,33 @@ fn main() {
println!("cargo:rustc-cfg=limb_width_32");
}
}

let minor = match rustc_minor_version() {
Some(minor) => minor,
None => return,
};

// BTreeMap::get_key_value
// https://blog.rust-lang.org/2019/12/19/Rust-1.40.0.html#additions-to-the-standard-library
if minor < 40 {
println!("cargo:rustc-cfg=no_btreemap_get_key_value");
}

// BTreeMap::remove_entry
// https://blog.rust-lang.org/2020/07/16/Rust-1.45.0.html#library-changes
if minor < 45 {
println!("cargo:rustc-cfg=no_btreemap_remove_entry");
}
}

fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
let next = pieces.next()?;
u32::from_str(next).ok()
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ mod lib {
pub use std::*;
}

pub use self::core::ops::{Bound, RangeBounds};
pub use self::core::cell::{Cell, RefCell};
pub use self::core::clone::{self, Clone};
pub use self::core::convert::{self, From, Into};
Expand Down
48 changes: 48 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,54 @@ impl Map<String, Value> {
return self.map.remove(key);
}

/// Removes a key from the map, returning the stored key and value if the
/// key was previously in the map.
///
/// The key may be any borrowed form of the map's key type, but the ordering
/// on the borrowed form *must* match the ordering on the key type.
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)>
where
String: Borrow<Q>,
Q: ?Sized + Ord + Eq + Hash,
{
#[cfg(any(feature = "preserve_order", not(no_btreemap_remove_entry)))]
return self.map.remove_entry(key);
#[cfg(all(
not(feature = "preserve_order"),
no_btreemap_remove_entry,
not(no_btreemap_get_key_value),
))]
{
let (key, _value) = self.map.get_key_value(key)?;
let key = key.clone();
let value = self.map.remove::<String>(&key)?;
Some((key, value))
}
#[cfg(all(
not(feature = "preserve_order"),
no_btreemap_remove_entry,
no_btreemap_get_key_value,
))]
{
struct Key<'a, Q: ?Sized>(&'a Q);

impl<'a, Q: ?Sized> RangeBounds<Q> for Key<'a, Q> {
fn start_bound(&self) -> Bound<&Q> {
Bound::Included(self.0)
}
fn end_bound(&self) -> Bound<&Q> {
Bound::Included(self.0)
}
}

let mut range = self.map.range(Key(key));
let (key, _value) = range.next()?;
let key = key.clone();
let value = self.map.remove::<String>(&key)?;
Some((key, value))
}
}

/// Moves all elements from other into Self, leaving other empty.
#[inline]
pub fn append(&mut self, other: &mut Self) {
Expand Down