-
Notifications
You must be signed in to change notification settings - Fork 360
Custom impl Into<Diagnostic>
for types that already implement Display
#880
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
Comments
we're open to suggestions. I don't see any other workaround either. |
One idea that comes to mind is making the blanket pub trait IntoDiagnostic {}
impl<T: Display> IntoDiagnostic for T {}
impl<'a, T> From<T> for Diagnostic<'a>
where
T: Display + IntoDiagnostic
{
// existing implementation
} With this approach, Another option could be waiting for negative trait bounds to stabilize so you can do something like this: pub trait ErrorType {
fn error_type(&self) -> String;
}
impl<'a, T> From<T> for Diagnostic<'a>
where
T: Display + ErrorType
{
fn from(value: T) -> Self {
Diagnostic {
error_type: std::borrow::Cow::Borrowed(value.error_type()),
error_message: std::borrow::Cow::Owned(format!("{value}")),
}
}
}
impl<'a, T> From<T> for Diagnostic<'a>
where
T: Display + !ErrorType
{
// existing implementation
} With this approach, you could still lean on the convenience of having |
I think this is the right solution, other extensions to the runtime already follow this principle, like the RequestExt in lambda-http. I'm not concerned about backwards compatibility, since we're not giving any guarantees until the runtime is officially supported, and this is something fairly new. Do you mind opening a PR with your proposal? We'll really appreciate the contribution. |
I've tried your proposal but it doesn't work because it has the same problem since everything that implements Display automatically implements IntoDiagnostic. I went with a different implementation in #897 and I added an example on how to implement Diagnostic based on your snippet. |
Thanks for looking into this @calavera! My bad on the proposal oversight. I thought as long as |
No worries, thanks for bringing this issue up! |
This issue is now closed. Comments on closed issues are hard for our team to see. |
The
Diagnostic
docs say:I'm trying to do just that. However my error container already has a
Display
implementation (viathiserror
). Because of the blanketimpl<'a, T> From<T> for Diagnostic<'a> where T: Display
, I run into a conflicting trait implementations error.The error makes perfect sense, but I also see no way around it without removing the
Display
implementation from my code, which I'd like to keep. I feel like it's not that unusual to want a customInto<Diagnostic>
forDisplay
types. Do I have other options here?As a motivating example, I'm working in an application that uses AWS step functions, and our team wants to distinguish between retryable and non-retryable errors so the state machine can automatically retry failed lambdas when appropriate. For logging purposes, we don't care whether the error is retryable, but we need the information to bubble up to the
errorType
of the lambda error output. Here's a snippet:The text was updated successfully, but these errors were encountered: