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

feat: implement Debug for DatabaseComponentError if supported #363

Merged
merged 3 commits into from
Feb 10, 2023

Conversation

Wodann
Copy link
Contributor

@Wodann Wodann commented Feb 8, 2023

No description provided.

@SuperFluffy
Copy link

SuperFluffy commented Feb 9, 2023

When you #[derive(Debug)] for a type Foo<T> with generic type parameters, then the proc macro will implicitly add impl<T: Debug> Debug for Foo<T> for you. This means that the manual implementation of Debug is the same as the derived one. Proof:

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")

Copy link
Member

@rakita rakita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few nits

@Wodann
Copy link
Contributor Author

Wodann commented Feb 9, 2023

@SuperFluffy does that still allow instances of the type that don't implement Debug for T?

@SuperFluffy
Copy link

SuperFluffy commented Feb 9, 2023

@Wodann

@SuperFluffy does that still allow instances of the type that don't implement Debug for T?

Of course! The #[derive(Debug)] is only relevant for the actual impl Trait for Type block, it does not affect the type definition itself. The derive macro literally boils down to what you wrote. Here is the output of cargo expand for my example above:

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 struct Foo<T: Debug>, which would indeed restrict Foo to only contain T: Debug. I wrote some code like that a few years ago and it was awful. :D

@Wodann
Copy link
Contributor Author

Wodann commented Feb 9, 2023

Good to know, thanks!

@Wodann
Copy link
Contributor Author

Wodann commented Feb 9, 2023

Applied all suggestions

@rakita
Copy link
Member

rakita commented Feb 10, 2023

@SuperFluffy nice!

@rakita rakita merged commit 3158ce9 into bluealloy:main Feb 10, 2023
@Wodann Wodann deleted the improvement/debug-component branch February 10, 2023 20:56
@Wodann Wodann mentioned this pull request May 3, 2023
7 tasks
@Wodann Wodann restored the improvement/debug-component branch August 30, 2023 16:23
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.

3 participants