-
Notifications
You must be signed in to change notification settings - Fork 644
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
feat: implement Debug for DatabaseComponentError if supported #363
Conversation
When you use std::fmt::Debug;
#[derive(Debug)]
enum Foo<T, U> {
State(T),
BlockHash(U),
}
enum Bar<T, U> {
State(T),
BlockHash(U),
}
impl<T: Debug, U: Debug> Debug for Bar<T, U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::State(arg0) => f.debug_tuple("State").field(arg0).finish(),
Self::BlockHash(arg0) => f.debug_tuple("BlockHash").field(arg0).finish(),
}
}
}
fn main() {
let foo_state: Foo<&str, &str> = Foo::State("Hello");
let foo_blockhash: Foo<&str, &str> = Foo::BlockHash("World");
let bar_state: Bar<&str, &str> = Bar::State("Hello");
let bar_blockhash: Bar<&str, &str> = Bar::BlockHash("World");
println!("{foo_state:?}");
println!("{foo_blockhash:?}");
println!("{bar_state:?}");
println!("{bar_blockhash:?}");
assert_eq!(format!("{foo_state:?}"), format!("{bar_state:?}"));
assert_eq!(format!("{foo_blockhash:?}"), format!("{bar_blockhash:?}"));
} Output: ❯ cargo run
Compiling check_debug v0.1.0 (/home/janis/dev/scratchpad/check_debug)
Finished dev [unoptimized + debuginfo] target(s) in 0.16s
Running `target/debug/check_debug`
State("Hello")
BlockHash("World")
State("Hello")
BlockHash("World") |
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.
few nits
@SuperFluffy does that still allow instances of the type that don't implement Debug for T? |
Of course! The enum Foo<T, U> {
State(T),
BlockHash(U),
}
#[automatically_derived]
impl<T: ::core::fmt::Debug, U: ::core::fmt::Debug> ::core::fmt::Debug for Foo<T, U> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Foo::State(__self_0) => {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "State", &__self_0)
}
Foo::BlockHash(__self_0) => {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "BlockHash", &__self_0)
}
}
}
} You are probably thinking of something like |
Good to know, thanks! |
Co-authored-by: rakita <[email protected]>
Applied all suggestions |
@SuperFluffy nice! |
No description provided.