-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Implement Error for TryReserveError #69792
Changes from all commits
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 |
---|---|---|
|
@@ -42,6 +42,7 @@ pub use linked_list::LinkedList; | |
pub use vec_deque::VecDeque; | ||
|
||
use crate::alloc::{Layout, LayoutErr}; | ||
use core::fmt::Display; | ||
|
||
/// The error type for `try_reserve` methods. | ||
#[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", | ||
Comment on lines
+89
to
+92
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 do dislike that this isn't formatted consistently, but rustfmt formatted it this way. 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. The cleaner approach would be to move the match out of the write_str call and just assign the string to a variable IMO. 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. It doesn't help with the formatting though, rustfmt transforms this: #[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)
}
} Into this: #[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)
}
} 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 tried to submit code in which both statements used braces, but that makes one of the checks fail. 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 think it's fine now, btw, it's a really minor issue. |
||
}; | ||
fmt.write_str(reason) | ||
} | ||
} | ||
|
||
/// An intermediate trait for specialization of `Extend`. | ||
#[doc(hidden)] | ||
trait SpecExtend<I: IntoIterator> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's possible to return the LayoutErr as cause and source of the TryReserveError when passed here, although it would make the struct bigger.