-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
ErrorRef
in #
error formatter
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.
- Loading branch information
Showing
3 changed files
with
153 additions
and
17 deletions.
There are no files selected for viewing
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 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 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