RustCrypto / traits

Collection of cryptography-related traits
593 stars 193 forks source link

digest: implement `Update` for `Vec<u8>` #1630

Closed baloo closed 4 months ago

baloo commented 4 months ago

In use-cases like https://github.com/RustCrypto/SSH/blob/a6f33709d6a48d82c818a9bdec87fe3a96b027ca/ssh-encoding/src/writer.rs#L19 it is not currently possible to write things like;

impl<D> Writer for D
where
    D: digest::Update,
{
    fn write(&mut self, bytes: &[u8]) -> Result<()> {
        self.update(bytes);
        Ok(())
    }
}

to get a writer implementation for T: Digest.

This isn't currently possible because of the following error:

error[E0119]: conflicting implementations of trait `Writer` for type `Vec<u8>`
   |
21 |   impl Writer for Vec<u8> {
   |   ----------------------- first implementation here
...
44 | / impl<D> Writer for D
45 | | where
46 | |     D: Update,
   | |______________^ conflicting implementation for `Vec<u8>`
   |
   = note: upstream crates may add a new impl of trait `digest::Update` for type `alloc::vec::Vec<u8>` in future versions

Although this is technically incorrect, as Vec isn't a digest, an Update implementation still seems relevant.

newpavlov commented 4 months ago

This looks like too much of a hack to me. I think a better solution will be to introduce a DigestWriter wrapper struct.

baloo commented 4 months ago

that's a fair point.