I have a type implementing a const-generic trait with different implementations for specific cases. I want to inherit (and extend) the logic for another type and would prefer not to have to manually enumerate all combinations (just two in this example).
trait Hi<const B: bool> {
fn hi();
}
struct X;
struct Y;
impl Hi<true> for X {
fn hi() {
println!("hi")
}
}
impl Hi<false> for X {
fn hi() {
println!("bye")
}
}
impl<const B: bool> Hi<B> for Y {
fn hi() {
<X as Hi<B>>::hi();
}
}
fn main() {
<X as Hi<true>>::hi();
<X as Hi<false>>::hi();
<Y as Hi<true>>::hi();
<Y as Hi<false>>::hi();
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `X: Hi<B>` is not satisfied
--> src/main.rs:22:10
|
22 | <X as Hi<B>>::hi();
| ^ the trait `Hi<B>` is not implemented for `X`
|
= help: the following other types implement trait `Hi<B>`:
`X` implements `Hi<false>`
`X` implements `Hi<true>`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error
I do not think this will be possible right now but it could be. I wanted to ask if someone knows a workaround, and if this is something the compiler is planned to support in the future.