Give const as generic for trait

I tried to abstract my problem so it is more easy to see the problem:

// real code: Resource
struct Value {
    v: u32
}

// only exists to contain the number of elements
trait Trait0 {
    const num: usize;
}
  
trait Trait1<const N: usize> {
    const c: [Value; N];
}

// real code: some sort of building
struct Test;

impl Trait0 for Test {
    const num: usize = 2;
}

// real code: Trait1 contains a list of resources that is needed to build a specific building
impl Trait1<{Test::num}> for Test {
    const c: [Value; Test::num] = [ Value{ v: 0 }, Value{ v: 1 } ];
}

// real code: foo is a function that gets the resources needed for a building and does stuff with this information
fn foo<T> ()
// Error: generic parameters may not be used in const operations
where T: Trait0 + Trait1<{ T::num }> {
    for i in T::c {
        println!("{}", i.v);
    }
}

fn main() {
    foo::<Test>();
}

Why I expected it to work

I thought that because we established that T impl. Trait0 that I could therefore access the const num from it.

Question

Is there any way to do what I want to do, that is to get the constant c for any T (that impl. Trait1)?

I also saw that there was a simular error message for this post (Passing trait-associated constant as const-generic parameter) but the solution did not work for me. When trying to add const_generics and const_evaluatable_checked as feature like surgested, I got this error: "feature has been removed".

The solution was to use rust nightly and use the feature: #![feature(generic_const_exprs)]

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.