Skip to content
Open
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
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ macro_rules! define_unsigned {
pub const MAX: Self = $name(((1 as $type) << $bits) -1 );
pub const MIN: Self = $name(0);

/// The size of this type
pub const BITS: u32 = $bits;

fn mask(self) -> Self {
$name(self.0 & ( ((1 as $type) << $bits).overflowing_sub(1).0))
}
Expand All @@ -67,6 +70,7 @@ macro_rules! define_signed {
impl $name {
pub const MAX: Self = $name(((1 as $type) << ($bits - 1)) - 1);
pub const MIN: Self = $name(-((1 as $type) << ($bits - 1)));
pub const BITS: u32 = $bits;

fn mask(self) -> Self {
if ( self.0 & (1<<($bits-1)) ) == 0 {
Expand Down Expand Up @@ -739,6 +743,21 @@ mod tests {
assert_eq!(i9::MIN, i9(-256));
}

#[test]
fn bits_value() {
assert_eq!(u1::BITS, 1);
assert_eq!(u3::BITS, 3);
assert_eq!(u62::BITS, 62);
assert_eq!(u108::BITS, 108);
assert_eq!(u127::BITS, 127);

assert_eq!(i1::BITS, 1);
assert_eq!(i3::BITS, 3);
assert_eq!(i62::BITS, 62);
assert_eq!(i108::BITS, 108);
assert_eq!(i127::BITS, 127);
}

#[test]
fn test_wrapping_add() {
assert_eq!(u1::MAX.wrapping_add(u1(1)), u1(0));
Expand Down