From 71c4bc33d7f6ceb4534f5f891c24f1d1faab1336 Mon Sep 17 00:00:00 2001 From: Daniel Lehmann Date: Thu, 15 Dec 2022 22:24:48 -0800 Subject: [PATCH] Use self instead of &self in value() and widen() This ensures that value() is taken from the uX instead of Number, which isn't const. --- src/lib.rs | 4 ++-- tests/tests.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index acb8c0e..807e1e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,7 +83,7 @@ impl UInt { /// Returns the type as a fundamental data type #[inline] - pub const fn value(&self) -> T { + pub const fn value(self) -> T { self.value } @@ -248,7 +248,7 @@ macro_rules! uint_impl { /// Returns a UInt with a wider bit depth but with the same base data type pub const fn widen( - &self, + self, ) -> UInt<$type, BITS_RESULT> { let _ = CompileTimeAssert::::SMALLER_THAN; // Query MAX of the result to ensure we get a compiler error if the current definition is bogus (e.g. ) diff --git a/tests/tests.rs b/tests/tests.rs index 4df49a5..77fbcd7 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -4,6 +4,23 @@ use arbitrary_int::*; use num_traits::WrappingAdd; use std::fmt::Debug; +#[test] +fn constants() { + // Make a constant to ensure new().value() works in a const-context + const TEST_CONSTANT: u8 = u7::new(127).value(); + assert_eq!(TEST_CONSTANT, 127u8); + + // Same with widen() + const TEST_CONSTANT2: u7 = u6::new(63).widen(); + assert_eq!(TEST_CONSTANT2, u7::new(63)); + + // Same with widen() + const TEST_CONSTANT3A: Result = u6::try_new(62); + assert_eq!(TEST_CONSTANT3A, Ok(u6::new(62))); + const TEST_CONSTANT3B: Result = u6::try_new(64); + assert!(TEST_CONSTANT3B.is_err()); +} + #[test] fn create_simple() { let value7 = u7::new(123);