static-assertions
static-assertions copied to clipboard
Feature request: `assert_type_in`
Assertion that the supplied type must be one of multiple listed types.
One possible (although not so elegant) implementation is like this:
macro_rules! assert_type_in {
($label:ident; $type:ty, $($in:ty),*) => {
mod $label {
$(fn mux(_: $in) {})*
fn _(v: $type) {
mux(v);
}
}
};
}
I came up with this which seems to work fine (playground):
macro_rules! assert_type_in {
($t:ty: $($l:ty),+ $(,)*) => {
{
trait InList {}
$(impl InList for $l {})+
fn in_list<T: ?Sized + InList>() {}
let _ = in_list::<$t>;
}
}
}
fn main() {
assert_type_in!(u8: u8, u16);
}