Skip to content

Commit

Permalink
Merge pull request #6 from danlehmann/4-cannot-call-non-const-fn-uint…
Browse files Browse the repository at this point in the history
…u8-2-as-arbitrary_intnumbervalue-after-upgrade-from-121-to-122

Use self instead of &self in value() and widen()
  • Loading branch information
danlehmann authored Dec 16, 2022
2 parents c2b06a5 + 71c4bc3 commit e093f57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<T: Copy, const BITS: usize> UInt<T, BITS> {

/// Returns the type as a fundamental data type
#[inline]
pub const fn value(&self) -> T {
pub const fn value(self) -> T {
self.value
}

Expand Down Expand Up @@ -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<const BITS_RESULT: usize>(
&self,
self,
) -> UInt<$type, BITS_RESULT> {
let _ = CompileTimeAssert::<BITS, BITS_RESULT>::SMALLER_THAN;
// Query MAX of the result to ensure we get a compiler error if the current definition is bogus (e.g. <u8, 9>)
Expand Down
17 changes: 17 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, TryNewError> = u6::try_new(62);
assert_eq!(TEST_CONSTANT3A, Ok(u6::new(62)));
const TEST_CONSTANT3B: Result<u6, TryNewError> = u6::try_new(64);
assert!(TEST_CONSTANT3B.is_err());
}

#[test]
fn create_simple() {
let value7 = u7::new(123);
Expand Down

0 comments on commit e093f57

Please sign in to comment.