Recursively descending generic constant

55 views Asked by At

Is there a way to bring this snippet to successful compilation ?

#![feature(generic_const_exprs)]

struct ConsPeaks<const N: usize>
where
    [(); N]: Sized, 
{
    data: [u8; N],
    next: Option<Box<ConsPeaks<{N-1}>>>,
}

As far as I can guess a potential bug here the compiler tries to prevent is that N at some point can reach zero and then overflow on subtraction. Is there a way for me to introduce this additional constraint to the code ?

I also tried this slightly modified code I found on gh, which sets the constraint on N:

struct Arr<const N: usize>
where 
    Assert::<{N > 0}>: IsTrue,
{
    data: [u8; N],
    next: Option<Box<Arr<{N-1}>>>,
}

enum Assert<const COND: bool> {}

trait IsTrue {}

but still, it doesn't catch the {N-1} case.

0

There are 0 answers