Skip to content

Commit 09cbcdc

Browse files
committed
Add BTreeMap::try_insert and btree_map::OccupiedError.
1 parent 7f32f62 commit 09cbcdc

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

library/alloc/src/collections/btree/map.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::node::{self, marker, ForceResult::*, Handle, NodeRef, Root};
1414
use super::search::SearchResult::*;
1515

1616
mod entry;
17-
pub use entry::{Entry, OccupiedEntry, VacantEntry};
17+
pub use entry::{Entry, OccupiedEntry, OccupiedError, VacantEntry};
1818
use Entry::*;
1919

2020
/// Minimum number of elements in nodes that are not a root.
@@ -836,6 +836,40 @@ impl<K, V> BTreeMap<K, V> {
836836
}
837837
}
838838

839+
/// Tries to insert a key-value pair into the map, and returns
840+
/// a mutable reference to the value in the entry.
841+
///
842+
/// If the map already had this key present, nothing is updated, and
843+
/// an error containing the occupied entry and the value is returned.
844+
///
845+
/// # Examples
846+
///
847+
/// Basic usage:
848+
///
849+
/// ```
850+
/// #![feature(map_try_insert)]
851+
///
852+
/// use std::collections::BTreeMap;
853+
///
854+
/// let mut map = BTreeMap::new();
855+
/// assert_eq!(map.try_insert(37, "a").unwrap(), &"a");
856+
///
857+
/// let err = map.try_insert(37, "b").unwrap_err();
858+
/// assert_eq!(err.entry.key(), &37);
859+
/// assert_eq!(err.entry.get(), &"a");
860+
/// assert_eq!(err.value, "b");
861+
/// ```
862+
#[unstable(feature = "map_try_insert", issue = "none")]
863+
pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, OccupiedError<'_, K, V>>
864+
where
865+
K: Ord,
866+
{
867+
match self.entry(key) {
868+
Occupied(entry) => Err(OccupiedError { entry, value }),
869+
Vacant(entry) => Ok(entry.insert(value)),
870+
}
871+
}
872+
839873
/// Removes a key from the map, returning the value at the key if the key
840874
/// was previously in the map.
841875
///

library/alloc/src/collections/btree/map/entry.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ impl<K: Debug + Ord, V: Debug> Debug for OccupiedEntry<'_, K, V> {
7171
}
7272
}
7373

74+
/// The error returned by [`try_insert`](BTreeMap::try_insert) when the key already exists.
75+
///
76+
/// Contains the occupied entry, and the value that was not inserted.
77+
#[unstable(feature = "map_try_insert", issue = "none")]
78+
pub struct OccupiedError<'a, K: 'a, V: 'a> {
79+
/// The entry in the map that was already occupied.
80+
pub entry: OccupiedEntry<'a, K, V>,
81+
/// The value which was not inserted, because the entry was already occupied.
82+
pub value: V,
83+
}
84+
85+
#[unstable(feature = "map_try_insert", issue = "none")]
86+
impl<K: Debug + Ord, V: Debug> Debug for OccupiedError<'_, K, V> {
87+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88+
f.debug_struct("OccupiedError")
89+
.field("entry", &self.entry)
90+
.field("value", &self.value)
91+
.finish()
92+
}
93+
}
94+
7495
impl<'a, K: Ord, V> Entry<'a, K, V> {
7596
/// Ensures a value is in the entry by inserting the default if empty, and returns
7697
/// a mutable reference to the value in the entry.

0 commit comments

Comments
 (0)