Sometimes I'd like to write some Const Generics code like:
struct Foo<const N: usize> {
data: [u8; N / 2 + 1],
}
impl<const N: usize> Foo<N> {
fn new() -> Self {
Self { data: Default::default() }
}
}
fn main() {
let _bar = Foo::<10>::new();
}
Currently that's not possible, so I sometimes write it like this:
#![feature(const_generics)]
#![allow(incomplete_features, dead_code)]
const fn compute_foo_len(n: usize) -> usize { n / 2 + 1 }
struct FooInternal<const N: usize> {
data: [u8; N],
}
impl<const N: usize> FooInternal<N> {
fn new(m: usize) -> Self {
assert_eq!(N, compute_foo_len(m));
// Uses m here for something.
Self { data: [0; N] }
}
}
type Foo<const M: usize> = FooInternal<{ compute_foo_len(M) }>;
fn main() {
const N: usize = 10;
let _bar = Foo::<N>::new(N);
}
Is the first version of the code supposed to be allowed in future?