How to calculate 2nd const parameter in function with single const generic parameter

I have the following two structs with const generics:

struct IntPrinter<const I: usize> {}

impl<const I: usize> IntPrinter<{ I }> {
    fn print() {
        println!("{}", I);
    }
}

struct FollowingIntPrinter<const I: usize>
{}

impl<const I: usize> FollowingIntPrinter<{ I }>
    where [(); { I + 1 }]:
{
    fn print() {
        IntPrinter::<{ I + 1 }>::print();
    }
}

Calling FollowingIntPrinter::<41>::print(); prints the number 42 as expected.

The problem is that, when I change the type from usize to u8, it doesn't compile anymore. Instead, it complains that usize was expected but u8 was found. Is there any way to fix this?

In my real project, I'd like to use an enum with two values as a type. The caller would specify one value as a const value, and internally, I'd have a second one with the other value. I guess this is too much to ask...?

where [(); { I + 1 } as usize]:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3340d5f3a3c210bc8c4b0bc95e9b2d21

Thanks!

I think I've tried this, but didn't actually execute it because my IDE (CLion) showed an error. But I guess that is to be expected with nightly features.

I've also got a running version when using an enum Colour with two values (While & Black) and using

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=e4a1c3dc362ff1f4be867dfcd557a5f7

What did not work was moving the function opponent into the enum itself.

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.