nvzqz/static-assertions

Assert supertrait

rodrimati1992 opened this issue · 1 comments

I figured out a way to assert that a trait is a supertrait of another,don't know how useful this is though.

macro_rules! assert_supertrait{
    ( $subtrait:path : $supertrait:path )=>{
        const _:()={
            trait __Impl_Implication:$supertrait{}
            
            impl<__This> __Impl_Implication for __This
            where
                __This:$subtrait
            {}
        };
    }
}

assert_supertrait!{ Copy : Clone }
assert_supertrait!{ Eq : PartialEq }
nvzqz commented

This is awesome, thanks! I don't think I would have come up with it on my own.

I can see this being an all variant:

macro_rules! assert_supertrait {
    ($subtrait:path: $($supertrait:path),+ $(,)?) => {
        const _: () = {
            $({
                #[allow(non_camel_case_types)]
                trait __Impl_Implication: $supertrait {}

                impl<T: $subtrait> __Impl_Implication for T {}
            })+
        };
    };
}

And then we can have another any version down the line, where the test passes if any trait is a parent.