Feature Request: Integer Trait

Sometimes we wish to generalise an implementation of a trait over many integer types, and applying them one by one becomes too cumbersome. In the vein of generalising integers, I propose the Int, SignedInt and UnsignedInt traits.

pub trait Int {
    // Associated consts
    pub const MIN: Self;
    pub const MAX: Self;
    pub const BITS: u32;
    // Associated functions and methods
    pub const fn leading_zeros(self) -> u32 {}
    // ...
}

pub trait SignedInt: Int {
    pub fn is_negative(self) -> bool;
    pub fn is_positive(self) -> bool;
    // ...
}

pub trait UnsignedInt: Int {
   // ...
}

impl Int for u8 {
    // ...
}
impl Int for i8 {    
    // ...
}
impl UnsignedInt for u8 {
    // ...
}
impl SignedInt for i8 {
   // ...
}
2 Likes

Coherence makes those traits kinda hard to actually use to implement things, so I'd rather see

10 Likes

I see, thanks for pointing it out!

1 Like