4 ms·
I'm really happy that this finally landed. Unfortunately the limitations of the MVP are severe. For example you can't use associated constant as generic argume
by CodesInChaos 6y ago
I'm really happy that this finally landed. Unfortunately the limitations of the MVP are severe.
For example you can't use associated constant as generic arguments, preventing constructions like this:
trait HashFunction {
const OUTPUT_SIZE: usize;
fn hash(input: &[u8]) -> [u8; Self::OUTPUT_SIZE];
}
- rmdashrfstar 6y agoWhat’s the progress on supporting something like this in future versions?
- steveklabnik 6y agohttps://github.com/rust-lang/rust/issues/76560 https://github.com/rust-lang/rust/issues/76560 is the tracking issue for this specific feature.
- tuankiet65 6y agoAre 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).