integer_result/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
//! A Rust library for converting the primitive and non-zero integer types to a `Result` through comparison.
#![no_std]
/// `use` this trait to access `ok_less`, `ok_equal`, and `ok_greater` for all primitive and non-zero integer types.
pub trait Ext
where
Self: Sized + Eq + Ord,
{
/// Returns an `Ok` holding the integer if less than the other integer `val`.
/// Otherwise, returns and `Err` holding the integer.
///
/// # Examples
/// ```
/// use integer_result::Ext;
///
/// assert_eq!(1.ok_less(2), Ok(1));
/// assert_eq!(1.ok_less(0), Err(1));
/// ```
#[inline]
fn ok_less(self, val: Self) -> Result<Self, Self> {
if self < val {
Ok(self)
} else {
Err(self)
}
}
/// Returns an `Ok` holding the integer if equal to the other integer `val`.
/// Otherwise, returns an `Err` holding the integer.
///
/// # Examples
/// ```
/// use integer_result::Ext;
///
/// assert_eq!(1.ok_equal(1), Ok(1));
/// assert_eq!(1.ok_equal(0), Err(1));
/// ```
#[inline]
fn ok_equal(self, val: Self) -> Result<Self, Self> {
if self == val {
Ok(self)
} else {
Err(self)
}
}
/// Returns an `Ok` holding the integer if greater than the other integer `val`.
/// Otherwise, returns and `Err` holding the integer.
///
/// # Examples
/// ```
/// use integer_result::Ext;
///
/// assert_eq!(1.ok_greater(-1), Ok(1));
/// assert_eq!(1.ok_greater(1), Err(1));
/// ```
#[inline]
fn ok_greater(self, val: Self) -> Result<Self, Self> {
if self > val {
Ok(self)
} else {
Err(self)
}
}
}
impl Ext for i8 {}
impl Ext for i16 {}
impl Ext for i32 {}
impl Ext for i64 {}
impl Ext for i128 {}
impl Ext for isize {}
impl Ext for u8 {}
impl Ext for u16 {}
impl Ext for u32 {}
impl Ext for u64 {}
impl Ext for u128 {}
impl Ext for usize {}
mod non_zero_impl {
use core::num::*;
use crate::Ext;
impl Ext for NonZeroI8 {}
impl Ext for NonZeroI16 {}
impl Ext for NonZeroI32 {}
impl Ext for NonZeroI64 {}
impl Ext for NonZeroI128 {}
impl Ext for NonZeroIsize {}
impl Ext for NonZeroU8 {}
impl Ext for NonZeroU16 {}
impl Ext for NonZeroU32 {}
impl Ext for NonZeroU64 {}
impl Ext for NonZeroU128 {}
impl Ext for NonZeroUsize {}
}
#[cfg(feature="non_zero_impl")]
pub use non_zero_impl::*;