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

Add ZERO, ONE and MAX associated constants. #15

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Changes from 2 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
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
Loading