-
Notifications
You must be signed in to change notification settings - Fork 110
Allow conversion from Report to Error
#1749
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1433377
Allow converting `Report` to `Error`
TimDiekmann d153e8d
Add UI test for `into_report`
TimDiekmann 8475450
Implement `From<Report>` for `Box<dyn Error>` variants
TimDiekmann 70aee1a
Add test case for boxed errors
TimDiekmann 841415d
Update changelog
TimDiekmann 0441439
Merge branch 'main' into td/es/into-error
TimDiekmann 07968da
Fix implementation for no-std, stable-toolchain builds
TimDiekmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| use core::fmt; | ||
| #[cfg(nightly)] | ||
| use core::{ | ||
| any::{Demand, Provider}, | ||
| error::Error, | ||
| }; | ||
| #[cfg(not(nightly))] | ||
| use std::error::Error; | ||
|
|
||
| use crate::Report; | ||
|
|
||
| #[repr(transparent)] | ||
| pub(crate) struct ReportError<C>(Report<C>); | ||
|
|
||
| impl<C> ReportError<C> { | ||
| pub(crate) const fn new(report: Report<C>) -> Self { | ||
| Self(report) | ||
| } | ||
|
|
||
| pub(crate) const fn from_ref(report: &Report<C>) -> &Self { | ||
| // SAFETY: `ReportError` is a `repr(transparent)` wrapper around `Report`. | ||
| unsafe { &*(report as *const Report<C>).cast() } | ||
| } | ||
| } | ||
|
|
||
| impl<C> fmt::Debug for ReportError<C> { | ||
| fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Debug::fmt(&self.0, fmt) | ||
| } | ||
| } | ||
|
|
||
| impl<C> fmt::Display for ReportError<C> { | ||
| fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Display::fmt(&self.0, fmt) | ||
| } | ||
| } | ||
|
|
||
| impl<C> Error for ReportError<C> { | ||
| #[cfg(nightly)] | ||
| fn provide<'a>(&'a self, demand: &mut Demand<'a>) { | ||
| self.0.frames().for_each(|frame| frame.provide(demand)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -464,7 +464,6 @@ | |
| )] | ||
|
|
||
| extern crate alloc; | ||
| extern crate core; | ||
|
Member
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 love how CLion always automatically adds this line... |
||
|
|
||
| pub mod future; | ||
| pub mod iter; | ||
|
|
@@ -476,6 +475,8 @@ mod report; | |
| mod result; | ||
|
|
||
| mod context; | ||
| #[cfg(any(nightly, feature = "std"))] | ||
| mod error; | ||
| #[cfg(any(feature = "std", feature = "hooks"))] | ||
| pub mod fmt; | ||
| #[cfg(not(any(feature = "std", feature = "hooks")))] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use core::fmt; | ||
|
|
||
| use error_stack::{Context, IntoReport, Report}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct RootError; | ||
|
|
||
| impl fmt::Display for RootError { | ||
| fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt.write_str("root error") | ||
| } | ||
| } | ||
|
|
||
| impl Context for RootError {} | ||
|
|
||
| fn main() { | ||
| let result = Err::<(), _>(RootError); | ||
| let _: Result<(), Report<RootError>> = result.into_report(); | ||
|
|
||
| let result = Err::<(), _>(Report::new(RootError)); | ||
| // Not allowed | ||
| let _ = result.into_report(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| error[E0599]: the method `into_report` exists for enum `Result<(), error_stack::Report<RootError>>`, but its trait bounds were not satisfied | ||
| | | ||
| ::: src/report.rs | ||
| | | ||
| | pub struct Report<C> { | ||
| | -------------------- doesn't satisfy `_: From<error_stack::Report<RootError>>` | ||
| | | ||
| ::: $RUST/core/src/result.rs | ||
| | | ||
| | pub enum Result<T, E> { | ||
| | --------------------- doesn't satisfy `_: IntoReport` | ||
| --> tests/ui/into_report.rs:22:20 | ||
| | | ||
| 22 | let _ = result.into_report(); | ||
| | ^^^^^^^^^^^ | ||
| | | ||
| = note: the following trait bounds were not satisfied: | ||
| `error_stack::Report<error_stack::Report<RootError>>: From<error_stack::Report<RootError>>` | ||
| which is required by `Result<(), error_stack::Report<RootError>>: IntoReport` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Out of curiosity, any reason for choosing
for_eachoverfor frame in self.frames()?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.
No specific reason. I used it here as it's only one line vs three lines.