Use blanket impls to add convenience methods to all errors#20
Open
sruggier wants to merge 1 commit intooxidecomputer:mainfrom
Open
Use blanket impls to add convenience methods to all errors#20sruggier wants to merge 1 commit intooxidecomputer:mainfrom
sruggier wants to merge 1 commit intooxidecomputer:mainfrom
Conversation
This defines two traits, each defining a convenience method, `as_inline_error_chain` and `as_array_error_chain` respectively, as a more convenient way to construct `InlineErrorChain` and `ArrayErrorChain` adapters. Finally, it also defines corresponding blanket implementations for each one, so they automatically apply to any value whose type implements `std::error::Error`. This makes it possible to use `err.as_inline_error_chain()` instead of `InlineErrorChain::new(&err)`, and `err.as_array_error_chain()` instead of `ArrayErrorChain::new(&err)`.
Author
|
As discussed in oxidecomputer/omicron#9923. After playing with the idea a bit in a minimal test crate, I decided to go ahead and implement it. I made it return the adapter rather than producing a string directly, so that, true to the crate name, it would also be applicable in calls to slog macros. |
Author
|
To be clear, I don't see this as mutually exclusive with adding an extra string formatting method on the derive macros, I just happen to have tried this first. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This defines two traits, each defining a convenience method,
as_inline_error_chainandas_array_error_chainrespectively, as a more convenient way to constructInlineErrorChainandArrayErrorChainadapters.Finally, it also defines corresponding blanket implementations for each one, so they automatically apply to any value whose type implements
std::error::Error. This makes it possible to useerr.as_inline_error_chain()instead ofInlineErrorChain::new(&err), anderr.as_array_error_chain()instead ofArrayErrorChain::new(&err).