genericsrusttraitscompositioninterior-mutability

How can I use internal mutability with generic type in Rust?


I would like to design a struct in Rust that can be constructed with an object that implements the Digest trait, and abstract the behavior of the hash behind a method. Here's a simple example that doesn't compile:

use digest::Digest;

struct Crypto<D: Digest> {
    digest: D,
}

impl<D> Crypto<D>
where
    D: Digest,
{
    pub fn hash(&self, data: &[u8]) -> Vec<u8> {
        self.digest.chain(&data).finalize_reset().to_vec()
    }
}

This fails to compile because self is immutably borrowed in the method signature, so self.digest cannot be immutably borrowed. So it tries to copy it, instead, but since the D generic is not defined to adhere to the Copy trait, it fails.

I'd rather not copy it, anyway. I'd rather have the one instance. Some things I've tried:

I was trying to use generics to get the compile-time benefits of trait bounds, but have to admit that the challenges of internal mutability when composing with objects whose behavior require mutability is thwarting me. Pointers to idiomatic Rust solutions to this design challenge greatly appreciated.

Bonus — how do I avoid the to_vec() copy and just return the array returned by finalize_reset()?


Solution

  • I'd rather not copy it, anyway. I'd rather have the one instance [of self.digest].

    The problem is that self.digest.chain() consumes (takes ownership of) self.digest, and that's a fundamental part of the contract of Digest::chain() which you cannot change. Interior mutability won't help because it's not a mutability issue, it's an object lifetime issue - you cannot use an object after it is moved or dropped.

    Your idea to make digest a function that creates digests should work, though. It will require two generic types, one for the digest type, with a trait bound of Digest, and the other for the factory, with a trait bound of Fn() -> D:

    struct Crypto<F> {
        digest_factory: F,
    }
    
    impl<D, F> Crypto<F>
    where
        D: Digest,
        F: Fn() -> D,
    {
        pub fn hash(&self, data: &[u8]) -> Vec<u8> {
            (self.digest_factory)()
                .chain(&data)
                .finalize()  // use finalize as the object is not reused
                .to_vec()
        }
    }
    

    how do I avoid the to_vec() copy and just return the array returned by finalize_reset()?

    You can have hash() return the same type as finalize(), digest::Output<D>:

    pub fn hash(&self, data: &[u8]) -> digest::Output<D> {
        (self.digest_factory)()
            .chain(&data)
            .finalize()
    }