A basic Const Generics code pattern

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?

3 Likes

Yes, I think having const generics work with array sizes is one of the most important aspects of this feature, it's just not implemented yet.

3 Likes

They already work with array sizes, but there's no syntax to demand that some derived constant expression or array be well-formed, so this code is rejected.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.