Skip to content

Commit b900de0

Browse files
committed
Implement Error for TryReserveError
1 parent 4a1b69d commit b900de0

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/liballoc/collections/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub use linked_list::LinkedList;
4242
pub use vec_deque::VecDeque;
4343

4444
use crate::alloc::{Layout, LayoutErr};
45+
use core::fmt::Display;
4546

4647
/// The error type for `try_reserve` methods.
4748
#[derive(Clone, PartialEq, Eq, Debug)]
@@ -77,6 +78,22 @@ impl From<LayoutErr> for TryReserveError {
7778
}
7879
}
7980

81+
#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
82+
impl Display for TryReserveError {
83+
fn fmt(
84+
&self,
85+
fmt: &mut core::fmt::Formatter<'_>,
86+
) -> core::result::Result<(), core::fmt::Error> {
87+
fmt.write_str("memory allocation failed")?;
88+
fmt.write_str(match &self {
89+
TryReserveError::CapacityOverflow => {
90+
" because the computed capacity exceeded the collection's maximum"
91+
}
92+
TryReserveError::AllocError { .. } => " because the memory allocator returned a error",
93+
})
94+
}
95+
}
96+
8097
/// An intermediate trait for specialization of `Extend`.
8198
#[doc(hidden)]
8299
trait SpecExtend<I: IntoIterator> {

src/libstd/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,13 @@ impl Error for char::ParseCharError {
552552
}
553553
}
554554

555+
#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
556+
impl Error for alloc::collections::TryReserveError {
557+
fn description(&self) -> &str {
558+
"memory allocation failed"
559+
}
560+
}
561+
555562
// Copied from `any.rs`.
556563
impl dyn Error + 'static {
557564
/// Returns `true` if the boxed type is the same as `T`

0 commit comments

Comments
 (0)