Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/bits.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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