Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] Bits trait #247

Open
kyp44 opened this issue Aug 17, 2022 · 1 comment
Open

[feature request] Bits trait #247

kyp44 opened this issue Aug 17, 2022 · 1 comment

Comments

@kyp44
Copy link

kyp44 commented Aug 17, 2022

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
Copy link
Author

kyp44 commented Aug 17, 2022

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
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant