Skip to content
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 6 commits into from
Jan 3, 2024

Conversation

demurgos
Copy link
Contributor

@demurgos demurgos commented Dec 14, 2023

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:

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:

// 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 to support explicit lifetime extensions and support this use case in the future.

Make sure to:

  • Add an entry to CHANGELOG.md (if necessary)

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

@Techcable
Copy link
Member

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)
demurgos and others added 5 commits January 2, 2024 21:01
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.
Copy link
Member

@Techcable Techcable left a 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 Techcable merged commit 4cb76e1 into slog-rs:master Jan 3, 2024
35 checks passed
Techcable added a commit that referenced this pull request Jan 3, 2024
The weird borrowing magic is part of the point (PR #328)
@demurgos
Copy link
Contributor Author

demurgos commented Jan 3, 2024

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants