Is there any way to restrict a generic type to one of several types?

For references into an array usually you’d just use a usize rather than different integer types.

However, to do what you are after you can create a new trait, implement that trait for u16, u32 and u64 and then restrict T to your new trait.

pub trait MyNewTrait {}

impl MyNewTrait for u16 {}
impl MyNewTrait for u32 {}
impl MyNewTrait for u64 {}

struct Foo<T: MyNewTrait> { ... }

You may then also add methods onto MyNewTrait and the impls to encapsulate the logic specific to u16, u32 and u64.

Leave a Comment