Skip to content

Commit d85d82a

Browse files
committed
Implement Error for OccupiedError.
1 parent 69d95e2 commit d85d82a

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ impl<K: Debug + Ord, V: Debug> Debug for OccupiedError<'_, K, V> {
9393
}
9494
}
9595

96+
#[unstable(feature = "map_try_insert", issue = "none")]
97+
impl<'a, K: Debug + Ord, V: Debug> fmt::Display for OccupiedError<'a, K, V> {
98+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99+
write!(
100+
f,
101+
"failed to insert {:?}, key {:?} already exists with value {:?}",
102+
self.value,
103+
self.entry.key(),
104+
self.entry.get(),
105+
)
106+
}
107+
}
108+
96109
impl<'a, K: Ord, V> Entry<'a, K, V> {
97110
/// Ensures a value is in the entry by inserting the default if empty, and returns
98111
/// a mutable reference to the value in the entry.

library/std/src/collections/hash/map.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,19 @@ impl<K: Debug, V: Debug> Debug for OccupiedError<'_, K, V> {
19071907
}
19081908
}
19091909

1910+
#[unstable(feature = "map_try_insert", issue = "none")]
1911+
impl<'a, K: Debug, V: Debug> fmt::Display for OccupiedError<'a, K, V> {
1912+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1913+
write!(
1914+
f,
1915+
"failed to insert {:?}, key {:?} already exists with value {:?}",
1916+
self.value,
1917+
self.entry.key(),
1918+
self.entry.get(),
1919+
)
1920+
}
1921+
}
1922+
19101923
#[stable(feature = "rust1", since = "1.0.0")]
19111924
impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> {
19121925
type Item = (&'a K, &'a V);

library/std/src/error.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,24 @@ impl Error for char::DecodeUtf16Error {
470470
}
471471
}
472472

473+
#[unstable(feature = "map_try_insert", issue = "none")]
474+
impl<'a, K: Debug + Ord, V: Debug> Error
475+
for crate::collections::btree_map::OccupiedError<'a, K, V>
476+
{
477+
#[allow(deprecated)]
478+
fn description(&self) -> &str {
479+
"key already exists"
480+
}
481+
}
482+
483+
#[unstable(feature = "map_try_insert", issue = "none")]
484+
impl<'a, K: Debug, V: Debug> Error for crate::collections::hash_map::OccupiedError<'a, K, V> {
485+
#[allow(deprecated)]
486+
fn description(&self) -> &str {
487+
"key already exists"
488+
}
489+
}
490+
473491
#[stable(feature = "box_error", since = "1.8.0")]
474492
impl<T: Error> Error for Box<T> {
475493
#[allow(deprecated, deprecated_in_future)]

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
#![feature(linkage)]
282282
#![feature(llvm_asm)]
283283
#![feature(log_syntax)]
284+
#![feature(map_try_insert)]
284285
#![feature(maybe_uninit_extra)]
285286
#![feature(maybe_uninit_ref)]
286287
#![feature(maybe_uninit_slice)]

0 commit comments

Comments
 (0)