From 2442cecc3bda871b70067b74b229000ed133baae Mon Sep 17 00:00:00 2001 From: bbaldino Date: Sun, 25 Feb 2024 21:00:09 -0800 Subject: [PATCH 1/2] add BITS associated constant --- src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 0f76f9e..ac4c282 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,7 @@ macro_rules! define_unsigned { impl $name { pub const MAX: Self = $name(((1 as $type) << $bits) -1 ); pub const MIN: Self = $name(0); + pub const BITS: usize = $bits; fn mask(self) -> Self { $name(self.0 & ( ((1 as $type) << $bits).overflowing_sub(1).0)) @@ -67,6 +68,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: usize = $bits; fn mask(self) -> Self { if ( self.0 & (1<<($bits-1)) ) == 0 { @@ -735,6 +737,13 @@ mod tests { assert_eq!(i9::MIN, i9(-256)); } + #[test] + fn test_bits_values() { + assert_eq!(u1::BITS, 1); + assert_eq!(i7::BITS, 7); + assert_eq!(u127::BITS, 127); + } + #[test] fn test_wrapping_add() { assert_eq!(u1::MAX.wrapping_add(u1(1)), u1(0)); From b74c374c89d13cc552d18ce350ca11ea887a2242 Mon Sep 17 00:00:00 2001 From: bbaldino Date: Fri, 1 Mar 2024 12:55:03 -0800 Subject: [PATCH 2/2] fix BITS to use u32 --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ac4c282..f860a9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ macro_rules! define_unsigned { impl $name { pub const MAX: Self = $name(((1 as $type) << $bits) -1 ); pub const MIN: Self = $name(0); - pub const BITS: usize = $bits; + pub const BITS: u32 = $bits; fn mask(self) -> Self { $name(self.0 & ( ((1 as $type) << $bits).overflowing_sub(1).0)) @@ -68,7 +68,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: usize = $bits; + pub const BITS: u32 = $bits; fn mask(self) -> Self { if ( self.0 & (1<<($bits-1)) ) == 0 {