I'm in a situation where I'd really like to use arithmetic with const generics to allow something like this:
fn foo<const N: usize>(input: [f32; N]) -> [f32; N/4] {
...
}
But this requires the const_generics
and const_evaluatable_checked
features and I don't want to be relying on anything that triggers the incomplete_features
lint. min_const_generics
is in beta though, so I'm happy to use that.
Are there any workarounds on stable Rust where I can take a fixed-length array and return a new array which is 1/4 the length?
I tried adding another layer of indirection using traits and an associated const, but that didn't get me very far.
Example using associated constants
trait MultiplyBy4 {
const VALUE: usize;
type Array;
}
impl<const N: usize> MultiplyBy4 for [f32; N]
{
const VALUE: usize = N*4;
type Array = [f32; Self::VALUE];
}
Fails to compile with
error: generic `Self` types are currently not permitted in anonymous constants
--> src/lib.rs:16:24
|
16 | type Array = [f32; Self::VALUE];
| ^^^^^^^^^^^
|
note: not a concrete type
--> src/lib.rs:13:38
|
13 | impl<const N: usize> MultiplyBy4 for [f32; N]
| ^^^^^^^^
Variations on this seem to fail with similar errors.