Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: assert_type_in #15

Open
chankyin opened this issue Jul 24, 2019 · 1 comment
Open

Feature request: assert_type_in #15

chankyin opened this issue Jul 24, 2019 · 1 comment

Comments

@chankyin
Copy link

chankyin commented Jul 24, 2019

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);
            }
        }
    };
}
@nvzqz
Copy link
Owner

nvzqz commented Aug 11, 2019

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants