-
Notifications
You must be signed in to change notification settings - Fork 95
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
Support ErrorRef
in #
error formatter
#328
Merged
Merged
Conversation
This file contains 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 is brilliant!! I'm not the greatest with traditional macros, so I'm not 100% sure about compatibility either. I will rebase this to be compatible with the last PR. |
Techcable
added a commit
to demurgos/slog
that referenced
this pull request
Jan 3, 2024
The weird borrowing magic is part of the point (PR slog-rs#328)
This commit adds support for error references for the `#` error formatter from the `kv` macro. If the passed error is an owned error value it uses `ErrorValue`; otherwise if it can be dereferenced into an error then it uses `ErrorRef`. The way to detect if the value implements `Error` directly or if it's an error reference is based on auto-deref coercion as described in the following articles: - <https://lukaskalbertodt.github.io/2019/12/05/generalized-autoref-based-specialization.html> - <https://github.com/dtolnay/case-studies/tree/master/autoref-specialization> I believe it to be fully backwards compatible with existing uses of `#`. This provides a short-hand for the `ErrorRef` wrapper in most common cases. However, it does not support temporary lifetime extension. This means that the following example needs to introduce a temporary binding to use the shorthand syntax: ```rust // explicit ref with lifetime extension info!(logger, "not found"; "error" => ErrorRef(&std::io::Error::from(std::io::ErrorKind::NotFound))); // `#` shorthand with temporary binding let error = std::io::Error::from(std::io::ErrorKind::NotFound); info!(logger, "not found"; "error" => #&error); ``` In practice, this use case is not an issue. Such a temporary can instead be passed by value (the reference is useless) through `ErrorValue`. There are [discussions in Zulip](https://rust-lang.zulipchat.com/#narrow/stream/403629-t-lang.2Ftemporary-lifetimes-2024) to support explicit lifetime extensions and support this use case in the future.
Avoids duplicating multiple times across multiple macros. Explicitly suppress unused_import warning (needed with clippy deny warnings)
Clippy gives a warning here
The weird borrowing magic is part of the point (PR slog-rs#328)
Requires `impl Error for Arc<T>`, which only exists on recent versions. Uses rustversion to conditionally disable.
Techcable
force-pushed
the
feature/error-ref-macro
branch
from
January 3, 2024 09:10
4bcbec2
to
467bfda
Compare
Techcable
approved these changes
Jan 3, 2024
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.
This is excellent. Thank you!!
Techcable
added a commit
that referenced
this pull request
Jan 3, 2024
The weird borrowing magic is part of the point (PR #328)
Thank you for reviewing and merging my changes |
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 commit adds support for error references for the
#
error formatter from thekv
macro. If the passed error is an owned error value it usesErrorValue
; otherwise if it can be dereferenced into an error then it usesErrorRef
.The way to detect if the value implements
Error
directly or if it's an error reference is based on auto-deref coercion as described in the following articles:I believe it to be fully backwards compatible with existing uses of
#
. This provides a short-hand for theErrorRef
wrapper in most common cases. However, it does not support temporary lifetime extension.This means that the following example needs to introduce a temporary binding to use the shorthand syntax:
In practice, this use case is not an issue. Such a temporary can instead be passed by value (the reference is useless) through
ErrorValue
. There are discussions in Zulip to support explicit lifetime extensions and support this use case in the future.Make sure to:
This is a follow up to #327 to add support for the macro shorthand
#
. I sent it in its own PR as the logic is more complex and I don't want to block the simple error reference wrapper because of the macro logic. Commit with macro support