Skip to content

Commit

Permalink
Add ZERO, ONE and MAX associated constants. (#15)
Browse files Browse the repository at this point in the history
* Add ZERO, ONE and MAX associated constants.

Resolves #14.

* Fix hard coded types in cmp test.

* cargo fmt
  • Loading branch information
vi authored Nov 6, 2023
1 parent a4a428f commit 4f2f23d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ macro_rules! nonmax {
pub const fn get(&self) -> $primitive {
self.0.get() ^ $primitive::MAX
}

/// Gets non-max with the value zero (0)
pub const ZERO: $nonmax = unsafe { Self::new_unchecked(0) };

/// Gets non-max with the value one (1)
pub const ONE: $nonmax = unsafe { Self::new_unchecked(1) };

/// Gets non-max with maximum possible value (which is maximum of the underlying primitive minus one)
pub const MAX: $nonmax = unsafe { Self::new_unchecked($primitive::MAX - 1) };
}

impl Default for $nonmax {
Expand Down Expand Up @@ -274,15 +283,25 @@ macro_rules! nonmax {

#[test]
fn cmp() {
let zero = NonMaxU8::new(0).unwrap();
let one = NonMaxU8::new(1).unwrap();
let two = NonMaxU8::new(2).unwrap();
let zero = $nonmax::new(0).unwrap();
let one = $nonmax::new(1).unwrap();
let two = $nonmax::new(2).unwrap();
assert!(zero < one);
assert!(one < two);
assert!(two > one);
assert!(one > zero);
}

#[test]
fn constants() {
let zero = $nonmax::ZERO;
let one = $nonmax::ONE;
let max = $nonmax::MAX;
assert_eq!(zero.get(), 0);
assert_eq!(one.get(), 1);
assert_eq!(max.get(), $primitive::MAX - 1);
}

#[test]
#[cfg(feature = "std")] // to_string
fn parse() {
Expand Down

0 comments on commit 4f2f23d

Please sign in to comment.