Only accept primitive types in a Rust Generic

generics, rust

Solution

I believe the closest thing you can get is `Primitive` trait which is implemented for in-built numeric types. It is a combination of several other numerical traits, which, in the end, allows for bit-fiddling with the values. You will also probably need to add `BitAnd`/`BitOr`/etc. traits, because `Primitive` only does not seem to allow these operations:

fn iter_bits<T: Primitive+BitAnd<T, T>+BitOr<T, T>>(x: T) { /* whatever */ }

Problem

Is there a way I can have a Rust Generic only accept primitive types? I want to later iterate over the bits in the value, and I understand that that's only possible with primitive types. ``` struct MyStruct<T> { my_property: T // my_property HAS to be a primitive type } ```

Original source

Related problems