Arjun
Arjun

Reputation: 3793

define const generics associated with outer const generic

I've situation where I want to have size of inner const generics defined struct to have size twice the outer one.

struct Inner<const DOUBLED_SIZE: usize>;
impl<const DOUBLED_SIZE: usize> Inner<DOUBLED_SIZE> {
    pub fn print(&self) {
        println!("val: {}", DOUBLED_SIZE);
    }
}

struct Outer<const SIZE: usize> {
    pub inner: Inner<SIZE>,
    // want to have size of Inner double of Outer
    // uncomment following line
    //pub inner: Inner<2*SIZE>,
}

fn main() {
    let outer = Outer::<40> { inner: Inner {} };
    outer.inner.print();
}

Is there any way to achieve it?

Upvotes: 0

Views: 65

Answers (1)

cdhowie
cdhowie

Reputation: 168958

This currently requires the generic_const_exprs nightly-only feature flag; it is not yet supported in stable Rust.

Upvotes: 1

Related Questions