3 ms·
Are you literally me 2 months ago? I was trying to implement the same HashFunction trait as yours and I finally settled with: pub trait HashFunction<const
by tuankiet65 6y ago
Are you literally me 2 months ago? I was trying to implement the same HashFunction trait as yours and I finally settled with:
pub trait HashFunction<const OUTPUT_SIZE: usize> {
fn hash(data: &[u8]) -> [u8; OUTPUT_SIZE];
}
I believe this approach has some shortcomings in my case, but it has been a while and I didn't remember anything.
Also for the Rust crowd, is it possible to do something like this?
pub trait HashFunction<const OUTPUT_SIZE: usize> {
// I want Output to always be [u8; OUTPUT_SIZE], but using associated type means
// whatever implements HashFunction can redefine Output.
type Output;
fn hash(data: &[u8]) -> Output;
}
- CodesInChaos 6y agoYou can add a constraint of Output. With a bit of abuse you can create a type that's impossible to implement outside your crate and implement it only for arrays in your crate (but if you make the output generic, there is no reason to keep the generic OUTPUT_SIZE parameter).