Skip to content

impl Debug for Digest#130

Merged
akhilles merged 4 commits intomrhooray:masterfrom
ChocolateLoverRaj:digest-debug
Jan 2, 2026
Merged

impl Debug for Digest#130
akhilles merged 4 commits intomrhooray:masterfrom
ChocolateLoverRaj:digest-debug

Conversation

@ChocolateLoverRaj
Copy link
Contributor

Closes #129

This implementation only shows the value field and not the info about the CRC. We could technically show the CRC config, but I don't think it would be that useful.

src/lib.rs Outdated
impl<'a, W: Width + Debug, I: Implementation> Debug for Digest<'a, W, I> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Digest")
.field("value", &self.value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO, this should be formatted as hex w/ constant char width based on W.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's a good idea, but idk how to make the width based on the max width of W, since there isn't a trait (at least in std), with const BITS: usize. Here is my current implementation:

let width = "0x".len() + (u16::BITS / 8 * 2) as usize;
let value = &self.value;
f.debug_struct("Digest")
    .field("value", &format_args!("{value:#0width$x?}"))
    .finish()

But as you can see this currently hard-codes u16, since W::BITS isn't valid code. One solution might be to add it to the Width trait, and implement it manually for the number types.

Copy link
Collaborator

Choose a reason for hiding this comment

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

impl<'a, W: Width + Debug + LowerHex, I: Implementation> Debug for Digest<'a, W, I> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Digest")
            .field("value", &format_args!("0x{:0w$x}", self.value, w = size_of::<W>() * 2))
            .finish()
    }
}

^ this should work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, I forgot that we can use size_of for this. Also the +2 is needed because "0x" is included in the length. I added a commit which changes to hex.

@akhilles akhilles merged commit 1894aa9 into mrhooray:master Jan 2, 2026
6 checks passed
@ChocolateLoverRaj ChocolateLoverRaj deleted the digest-debug branch January 2, 2026 19:12
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.

Feature request: implement Debug for Digest

2 participants