-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Conversation
@@ -77,6 +78,22 @@ impl From<LayoutErr> for TryReserveError { | |||
} |
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.
TryReserveError::CapacityOverflow => { | ||
" because the computed capacity exceeded the collection's maximum" | ||
} | ||
TryReserveError::AllocError { .. } => " because the memory allocator returned a error", |
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.
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 comment
The 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 comment
The 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 comment
The 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 comment
The 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.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@bors r+ rollup |
📌 Commit 2c90a37 has been approved by |
Rollup of 10 pull requests Successful merges: - #68899 (Add Display and Error impls for proc_macro::LexError) - #69011 (Document unsafe blocks in core::fmt) - #69674 (Rename DefKind::Method and TraitItemKind::Method ) - #69705 (Toolstate: remove redundant beta-week check.) - #69722 (Tweak output for invalid negative impl AST errors) - #69747 (Rename rustc guide) - #69792 (Implement Error for TryReserveError) - #69830 (miri: ICE on invalid terminators) - #69921 (rustdoc: remove unused import) - #69945 (update outdated comment) Failed merges: r? @ghost
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.