traits icon indicating copy to clipboard operation
traits copied to clipboard

Code in `digest` README is not valid

Open jwodder opened this issue 2 years ago • 1 comments

The README for the digest crate currently contains the following example:

use blake2::{Blake2b, Digest};

let mut hasher = Blake2b::new();
let data = b"Hello world!";
hasher.input(data);
// `input` can be called repeatedly and is generic over `AsRef<[u8]>`
hasher.input("String data");
// Note that calling `finalize()` consumes hasher
let hash = hasher.finalize();
println!("Result: {:x}", hash);

However, Digest values do not have an input() method; the correct name of the method is update(), and so the code above (after adding a fn main()) will not compile. Even after that's fixed, trying to compile the code still fails with the following error:

error[E0283]: type annotations needed for `CoreWrapper<CtVariableCoreWrapper<Blake2bVarCore, OutSize>>`
 --> src/main.rs:4:22
  |
4 |     let mut hasher = Blake2b::new();
  |         ----------   ^^^^^^^ cannot infer type for type parameter `OutSize`
  |         |
  |         consider giving `hasher` the explicit type `CoreWrapper<CtVariableCoreWrapper<_, OutSize>>`, where the type parameter `OutSize` is specified
  |
  = note: cannot satisfy `_: ArrayLength<u8>`
  = note: required because of the requirements on the impl of `BlockSizeUser` for `CtVariableCoreWrapper<Blake2bVarCore, _>`

For more information about this error, try `rustc --explain E0283`.
error: could not compile `digest-example` due to previous error

jwodder avatar Aug 06 '22 21:08 jwodder

Thanks. We should find a way to test this code, possibly using a dummy crate and #[doc = include_str!(...)]

tarcieri avatar Aug 08 '22 11:08 tarcieri