Specialization in stable

I have a simple specialization use case that I want to translate to any existing mechanism we have in stable Rust to do specialization:

pub trait IsU8 {
    type Status: Boolean;
}

pub trait Boolean {}

pub struct True;
pub struct False;

impl Boolean for True {}
impl Boolean for False {}

impl<T> IsU8 for u8 {
    type Status = True;
}

default impl<T> IsU8 for T {
    type Status = False;
}

More specifically:

  • I have an IsU8 trait.
  • I want the implementation of this trait for u8 to have Status be True.
  • I want the implementation of this trait for all other types to have Status be False.

Are there any techniques in stable Rust to achieve the above?

Not completely (or we would effectively have specialization). You can seal things up in various ways so that all other implementations must have Status = False, but you'd have to give up the blanket implementation on all types.

Can you say more about your actual use case? How you would use the trait if you could define it how you like. (My guess is you want something like T != U, but that's just a guess.)