0

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.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • The compiler is literally suggesting the solution. – Chayim Friedman Jun 25 '23 at 08:10
  • @Chayim Friedman Ah, I think I forgot to thank you for valuable reply. The compiler is literally NOT suggesting a solution. But I really appreciate you wasted 3 secs of your life to pay attention to my trouble. Anyway, if you know anyone who is capable to respond to my question, drop me a message, Golden Badge Carrier. – user18236575 Jun 27 '23 at 02:40
  • ```help: try adding a `where` bound using this expression: `where [(); {N-1}]:``` – Chayim Friedman Jun 27 '23 at 03:12
  • 1
    @Chayim Friedman I wouldn't post this question without trying whatever the compiler suggests as a solution. ```where [(); {N-1}]``` is what the compiler literally suggests and this doesn't compile. If you succeeded to compile it, please provide your snippet and the toolchain you use. If you didn't try to compile please take your negative vote off, as I suspect, nobody will even look at a question with a negative rating. – user18236575 Jun 27 '23 at 15:53
  • Report a bug. It should work. But as they say, ```the feature `generic_const_exprs` is incomplete```. – Chayim Friedman Jun 27 '23 at 18:24
  • And if I specify `where ConsPeaks<{N - 1}>:,`, [it hangs](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=50fe8ed0cc72f71ee2306aac0c539f2b). Another bug. – Chayim Friedman Jun 27 '23 at 18:26

0 Answers0