Specifying generic parameter to belong to a small

2020-04-19 23:35发布

Is it possible with to constrain a generic parameter to be one of the select few types without figuring out what traits precisely define those type? e.g.

impl<T> Data<T> where T == u32 || T == u64 

Sometimes it's tedious to figure out what all traits to add to where to get the types you want, and sometimes one wouldn't want to allow a type even when it makes syntactic sense because of semantics.

2条回答
老娘就宠你
2楼-- · 2020-04-20 00:03

You could use a marker trait for the types you want to support:

trait DataSupported {}

impl DataSupported for u64 {}
impl DataSupported for u32 {}

impl<T> Data<T> where T: DataSupported {}

As Pavel Strakhov mentioned, if you need to use this trait for a few impls and you need other trait bounds, then you can just make those traits as bounds of your marker trait instead, which will keep your impls terse:

trait DataSupported: Num + Debug {}
查看更多
手持菜刀,她持情操
3楼-- · 2020-04-20 00:19

You can use a macro to add implementations based on a list of types:

macro_rules! data_impl {
    ($($t: ty),+) => {
        $(
            impl Data<$t> {
                // methods go here
            }
        )+
    }
}

data_impl!(u32, u64, i32, i64);

It's probably worth adding #[allow(dead_code)] to each method to prevent warnings, since you'll be defining lots of specialised methods that may not be used in your application if you don't use them for every possible type. This is exactly the same thing that the compiler does anyway if you define methods over a parameter T (a process called monomorphisation). The difference is that the macro-generated methods are defined during the compiler's parse step (before linting) while monomorphisation happens later in the pipeline.

查看更多
登录 后发表回答