Skip to content

feat: add digest-traits feature to implement digest:Update for all hashers#405

Merged
vmx merged 3 commits intomultiformats:masterfrom
hanabi1224:digest-traits
Apr 7, 2026
Merged

feat: add digest-traits feature to implement digest:Update for all hashers#405
vmx merged 3 commits intomultiformats:masterfrom
hanabi1224:digest-traits

Conversation

@hanabi1224
Copy link
Copy Markdown
Contributor

This PR

  • add digest-traits feature to implement digest::Update for all hashers
  • bump dep versions

digest::Update is required by digest_io::IoWrapper which is useful for streaming input to a hasher.

e.g.

           // `digest-io` compatibility
           #[cfg(feature = "digest-traits")]
           {
                let mut hasher = digest_io::IoWrapper(<$alg>::default());
                let mut reader = std::io::Cursor::new($data);
                std::io::copy(&mut reader, &mut hasher).unwrap();
                assert_eq!(
                    $code.wrap(hasher.0.finalize()).unwrap().to_bytes(),
                    expected,
                    "{:?} encodes correctly (from hasher)", stringify!($alg)
                );
           }

@hanabi1224 hanabi1224 marked this pull request as ready for review April 6, 2026 14:59
Copy link
Copy Markdown
Member

@vmx vmx left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, the changes look good. I'd like to have the upgrade to digest 0.11 in a separate commit, so I just went ahead and did it myself at #406 (a review would be welcome, we are stretched thin on reviewers).

In regards to your change. I don't think we need to feature gate this. Making digest a non-optional shouldn't causes any problems.

Do I understand correctly that the digest-io test is testing that digest::Update is actually implemented? If yes, please add a comment that makes that clearer (at a first look was wondering why there are no tests for it).

@hanabi1224
Copy link
Copy Markdown
Contributor Author

@vmx Thanks for your quick feedback! If we don't feature-gate this, should I go a step further to implement digest::Update for T: multihash_derive::Hasher in multihash_derive crate? That would make the implementation automatic for new hashers, but it introduces 1 more required dependency to the multihash_derive crate. What do you think?

@vmx
Copy link
Copy Markdown
Member

vmx commented Apr 7, 2026

@hanabi1224 Good point. Implementing it directly in multhash_derive makes sense. New dependencies indeed always need a second thought, but as digest only contains traits it's not introducing much bloat.

@hanabi1224
Copy link
Copy Markdown
Contributor Author

@vmx PR updated.
(BTW, it turns out to be impossible to simply impl <T:Hasher> digest::Update for T in multihash-derive because of rustc --explain E0210)

$(
let expected = hex::decode($expect).unwrap();
// Mutlihash enum member, Multihash code, input, Multihash as hex
{$( $alg:ty, $code:expr, $data:expr, $expect:expr; )*} => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(The original 3-space indent seems to be inconsistent with the rest of the crate)

@hanabi1224 hanabi1224 requested a review from vmx April 7, 2026 11:47
Copy link
Copy Markdown
Member

@vmx vmx left a comment

Choose a reason for hiding this comment

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

Thanks for trying different things.

Also your own comment on the whitespace was really useful. Overall, great work!

@vmx vmx merged commit 08f8410 into multiformats:master Apr 7, 2026
11 checks passed
@vmx
Copy link
Copy Markdown
Member

vmx commented Apr 7, 2026

@hanabi1224 Do you plan any other changes atm? I'm asking as I would release a multihash-codetable v0.2. But if there are further changes soon, I'd wait for that.

@hanabi1224
Copy link
Copy Markdown
Contributor Author

Hey @vmx Thanks for the quick review! I don't have more changes to propose atm.

@hanabi1224 hanabi1224 deleted the digest-traits branch April 7, 2026 19:01
@vmx
Copy link
Copy Markdown
Member

vmx commented Apr 7, 2026

The 0.2 release is out!

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