-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add {BTreeMap,HashMap}::try_insert #82764
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
Changes from 4 commits
09cbcdc
f6fe24a
69d95e2
d85d82a
da01455
1aedb4c
eddd4f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,41 @@ impl<K: Debug + Ord, V: Debug> Debug for OccupiedEntry<'_, K, V> { | |
} | ||
} | ||
|
||
/// The error returned by [`try_insert`](BTreeMap::try_insert) when the key already exists. | ||
/// | ||
/// Contains the occupied entry, and the value that was not inserted. | ||
#[unstable(feature = "map_try_insert", issue = "none")] | ||
pub struct OccupiedError<'a, K: 'a, V: 'a> { | ||
/// The entry in the map that was already occupied. | ||
pub entry: OccupiedEntry<'a, K, V>, | ||
/// The value which was not inserted, because the entry was already occupied. | ||
pub value: V, | ||
Comment on lines
+79
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made these fields public instead of adding accessors. This is somewhat uncommon in Accessor functions here would get somewhat messy, because taking ownership of one or both of the fields should be possible. The alternative is |
||
} | ||
|
||
#[unstable(feature = "map_try_insert", issue = "none")] | ||
impl<K: Debug + Ord, V: Debug> Debug for OccupiedError<'_, K, V> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("OccupiedError") | ||
.field("key", self.entry.key()) | ||
.field("old_value", self.entry.get()) | ||
.field("new_value", &self.value) | ||
.finish() | ||
} | ||
} | ||
|
||
#[unstable(feature = "map_try_insert", issue = "none")] | ||
impl<'a, K: Debug + Ord, V: Debug> fmt::Display for OccupiedError<'a, K, V> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"failed to insert {:?}, key {:?} already exists with value {:?}", | ||
self.value, | ||
self.entry.key(), | ||
self.entry.get(), | ||
) | ||
} | ||
} | ||
|
||
impl<'a, K: Ord, V> Entry<'a, K, V> { | ||
/// Ensures a value is in the entry by inserting the default if empty, and returns | ||
/// a mutable reference to the value in the entry. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -470,6 +470,24 @@ impl Error for char::DecodeUtf16Error { | |
} | ||
} | ||
|
||
#[unstable(feature = "map_try_insert", issue = "none")] | ||
impl<'a, K: Debug + Ord, V: Debug> Error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added Error (and Display) implementations for OccupiedError for completeness, since it's an error. However, since it has non-static lifetimes, the Error implementation is not very useful in most cases. |
||
for crate::collections::btree_map::OccupiedError<'a, K, V> | ||
{ | ||
#[allow(deprecated)] | ||
fn description(&self) -> &str { | ||
"key already exists" | ||
} | ||
} | ||
|
||
#[unstable(feature = "map_try_insert", issue = "none")] | ||
impl<'a, K: Debug, V: Debug> Error for crate::collections::hash_map::OccupiedError<'a, K, V> { | ||
#[allow(deprecated)] | ||
fn description(&self) -> &str { | ||
"key already exists" | ||
} | ||
} | ||
|
||
#[stable(feature = "box_error", since = "1.8.0")] | ||
impl<T: Error> Error for Box<T> { | ||
#[allow(deprecated, deprecated_in_future)] | ||
|
Uh oh!
There was an error while loading. Please reload this page.