Skip to content

Commit d21320c

Browse files
authored
Rollup merge of #69792 - LenaWil:try_reserve_error/impl-error, r=sfackler
Implement Error for TryReserveError I noticed that the Error trait wasn't implemented for TryReserveError. (#48043) Not sure if the error messages and code style are 100% correct, it's my first time contributing to the Rust std.
2 parents 39c6405 + 2c90a37 commit d21320c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/liballoc/collections/mod.rs

+18
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,23 @@ 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+
let reason = 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+
fmt.write_str(reason)
95+
}
96+
}
97+
8098
/// An intermediate trait for specialization of `Extend`.
8199
#[doc(hidden)]
82100
trait SpecExtend<I: IntoIterator> {

src/libstd/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,9 @@ 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+
555558
// Copied from `any.rs`.
556559
impl dyn Error + 'static {
557560
/// Returns `true` if the boxed type is the same as `T`

0 commit comments

Comments
 (0)