I’m trying to write a function in Rust that should accept a generic parameter T, but I want to enforce T to be one of the unsigned integer types (u8, u16, u32, or u64). Additionally, I want to restrict T to support certain bitwise operations such as AND (&) and OR (|), as well as bitwise assignment operators (&= and |=).
Here’s what I’ve tried so far:
fn test<T>(mut x: T)
where
T: std::fmt::Display
+ BitAnd<u8, Output = T>
+ BitOr<u8, Output = T>
+ BitAndAssign<u8>
+ BitOrAssign<u8>
+ std::cmp::PartialOrd<u8>,
{
if x >= 2 {
x &= 4;
}
println!("{}", x);
}
However, this implementation only works when T is u8. When I try to include other types like u16, u32, or u64, it fails to compile.
What would be the correct way to enforce T to be one of these specific unsigned integer types and support the required bitwise operations?
Thank you for your help!