问题
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:
Changing the method signature to take
mut self
instead. But that moves ownership of the object into the method, after which it cannot be used again.Wrapping the
digest
field in aRefMut
orCell
, in an effort to adopt internal mutability, but I was not able to figure out the right method to then borrow thedigest
mutably without it trying to copy the value. Also, would prefer to keep borrow checks at compile-time if possible.Change the type of
D
to a function that returns an instance of aDigest
, and use it to instantiate a new digest inside thehash()
method. But then, even if I define it asD: Box<dyn Digest>
, the compiler complains thatthe value of the associated type OutputSize (from trait digest::Digest) must be specified
. So that seems challenging, since I want to support different hashing algorithms that will produce hashes of varying sizes.
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()?
回答1:
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 byfinalize_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()
}
回答2:
To add to user4815162342's digest factory answer here's an alternative implementation using interior mutability:
use digest::Digest;
use std::cell::RefCell;
struct Crypto<D: Digest> {
digest: RefCell<D>,
}
impl<D> Crypto<D>
where
D: Digest,
{
pub fn hash(&self, data: &[u8]) -> Vec<u8> {
let mut digest = self.digest.borrow_mut();
digest.update(&data);
digest.finalize_reset().to_vec()
}
}
playground
来源:https://stackoverflow.com/questions/65783417/how-can-i-use-internal-mutability-with-generic-type-in-rust