Const bool generic doesn't understand exhaustiveness implementation

⚓ Rust    📅 2025-06-25    👤 surdeus    👁️ 7      

surdeus

Warning

This post was published 48 days ago. The information described in this article may have changed.
trait MyTrait {
    fn conditional_method(&self) -> u64;
}

trait MyStructConditionalHelper<const COND: bool> {
    fn conditional_method(&self) -> u64;
}
struct MyStruct<const COND: bool>;

impl<const COND: bool> MyStructConditionalHelper<true> for MyStruct<COND> {
    fn conditional_method(&self) -> u64 {
        42
    }
}

impl<const COND: bool> MyStructConditionalHelper<false> for MyStruct<COND> {
    fn conditional_method(&self) -> u64 {
        0
    }
}

impl<const COND: bool> MyTrait for MyStruct<COND> {
    fn conditional_method(&self) -> u64 {
        <Self as MyStructConditionalHelper<COND>>::conditional_method(self)
    }
}

Seems like rustc doesn't understand that there are only two possible values for bool. By implementing both Trait<true> and Trait<false> on a struct I can not generically cast the struct, Struct<COND: bool>, to Trait<COND: bool>. This caught me by surprise. Does someone know if there is plan to support this?

1 post - 1 participant

Read full topic

🏷️ rust_feed