num-traits icon indicating copy to clipboard operation
num-traits copied to clipboard

[feature request] Bits trait

Open kyp44 opened this issue 3 years ago • 1 comments

Most Rust primitives have a BITS associated const (with the number of bits used), but evidently there is no way to use this generically. I have a particular need for this and it's easy enough to implement myself, but it seems like a good candidate for a num-traits trait.

kyp44 avatar Aug 17 '22 01:08 kyp44

Though this is obviously pretty trivial to figure out, here is an example for a subset of primitives.

pub trait Bits {
    fn bits() -> u32;
}
impl Bits for u8 {
    fn bits() -> u32 {
        Self::BITS
    }
}
impl Bits for u16 {
    fn bits() -> u32 {
        Self::BITS
    }
}
impl Bits for u32 {
    fn bits() -> u32 {
        Self::BITS
    }
}
impl Bits for u64 {
    fn bits() -> u32 {
        Self::BITS
    }
}

kyp44 avatar Aug 17 '22 01:08 kyp44