Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use self instead of &self in value() and widen() #6

Merged
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
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