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.
This commit is contained in:
commit
d21320cbd9
2 changed files with 21 additions and 0 deletions
|
@ -42,6 +42,7 @@ pub use linked_list::LinkedList;
|
||||||
pub use vec_deque::VecDeque;
|
pub use vec_deque::VecDeque;
|
||||||
|
|
||||||
use crate::alloc::{Layout, LayoutErr};
|
use crate::alloc::{Layout, LayoutErr};
|
||||||
|
use core::fmt::Display;
|
||||||
|
|
||||||
/// The error type for `try_reserve` methods.
|
/// The error type for `try_reserve` methods.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
|
@ -77,6 +78,23 @@ impl From<LayoutErr> for TryReserveError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
|
||||||
|
impl Display for TryReserveError {
|
||||||
|
fn fmt(
|
||||||
|
&self,
|
||||||
|
fmt: &mut core::fmt::Formatter<'_>,
|
||||||
|
) -> core::result::Result<(), core::fmt::Error> {
|
||||||
|
fmt.write_str("memory allocation failed")?;
|
||||||
|
let reason = match &self {
|
||||||
|
TryReserveError::CapacityOverflow => {
|
||||||
|
" because the computed capacity exceeded the collection's maximum"
|
||||||
|
}
|
||||||
|
TryReserveError::AllocError { .. } => " because the memory allocator returned a error",
|
||||||
|
};
|
||||||
|
fmt.write_str(reason)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An intermediate trait for specialization of `Extend`.
|
/// An intermediate trait for specialization of `Extend`.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
trait SpecExtend<I: IntoIterator> {
|
trait SpecExtend<I: IntoIterator> {
|
||||||
|
|
|
@ -552,6 +552,9 @@ impl Error for char::ParseCharError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
|
||||||
|
impl Error for alloc::collections::TryReserveError {}
|
||||||
|
|
||||||
// Copied from `any.rs`.
|
// Copied from `any.rs`.
|
||||||
impl dyn Error + 'static {
|
impl dyn Error + 'static {
|
||||||
/// Returns `true` if the boxed type is the same as `T`
|
/// Returns `true` if the boxed type is the same as `T`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue