Suppose I have my own type as a tuple struct
struct MyType(u8);
And would like to implement the From
trait for the other integral types in a generic way, like
impl From<T> for MyType {
fn from(num: T) -> Self {
MyType((num & 0xff) as u8)
}
}
Rust does not allow this because generic types T
do not have the as
operator. How can I constrain T
to the integral types so that as
can be used? something along the lines of
impl From<T: Integral> for MyType {...}