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

Add traits for BITS #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/bits.rs
@@ -0,0 +1,23 @@
use core::{i128, i16, i32, i64, i8, isize};
use core::{u128, u16, u32, u64, u8, usize};

/// Numbers which have a specified number of bits.
pub trait Bits {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you called this trait Precision instead? Then it could also have bytes()

/// Returns the size of the number in bits.
fn bits() -> u32;
}

macro_rules! bits_impl {
( $($x:ident),* ) => (
$(
impl Bits for $x {
fn bits() -> u32 {
$x::BITS
}
}
)*

);
}

bits_impl!(i128, i16, i32, i64, i8, isize, u128, u16, u32, u64, u8, usize);
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -32,6 +32,7 @@ pub use crate::bounds::Bounded;
pub use crate::float::Float;
pub use crate::float::FloatConst;
// pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`.
pub use crate::bits::Bits;
pub use crate::cast::{cast, AsPrimitive, FromPrimitive, NumCast, ToPrimitive};
pub use crate::identities::{one, zero, One, Zero};
pub use crate::int::PrimInt;
Expand All @@ -51,6 +52,7 @@ pub use crate::sign::{abs, abs_sub, signum, Signed, Unsigned};
#[macro_use]
mod macros;

pub mod bits;
pub mod bounds;
pub mod cast;
pub mod float;
Expand Down