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

Implement Sha256Digest for Vec<u8> and &Vec<u8>and &String directly #13

Merged
merged 2 commits into from
Jul 13, 2023

Conversation

Julian-Wollersberger
Copy link

Previously, this didn't compile:

let input: Vec<u8> = b"some owned bytes".to_vec();
sha256::digest(input);

and the error message was pretty unhelpful:

error[E0277]: the trait bound `Vec<u8>: Sha256Digest` is not satisfied
  --> examples/knampf.rs:6:20
   |
6  |     sha256::digest(input);
   |     -------------- ^^^^^ the trait `Sha256Digest` is not implemented for `Vec<u8>`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the following other types implement trait `Sha256Digest`:
             &[u8; N]
             &[u8]
             &str
             String
note: required by a bound in `sha256::digest`
  --> /home/level3/Documents/Rust/sha256-rs/src/lib.rs:48:18
   |
48 | pub fn digest<D: Sha256Digest>(input: D) -> String {
   |                  ^^^^^^^^^^^^ required by this bound in `digest`

The solution is to do a reborrow: sha256::digest(&*input);
But getting to that took a while and me and my colleges didn't understand what was happening. Apparently the Deref mechanism that lets you usually use &Vec<u8> in place of &[u8] doesn't work with the Sha256Digest trait.

So in this PR, I implement Sha256Digest for Vec<u8> and &Vec<u8> directly. This makes the first example just work.

The situation is similar with &String, which usually works the same as &str, but not here. So I added an impl for that too.

@baoyachi baoyachi merged commit a24f348 into baoyachi:master Jul 13, 2023
@baoyachi
Copy link
Owner

Thx @Julian-Wollersberger

@baoyachi
Copy link
Owner

New version publised,please use :

sha256 = "1.1.5"

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