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
// License: see LICENSE file at root directory of `master` branch

//! # Ops

use std::{
    fmt::Display,
    hash::Hash,
    str::FromStr,
};

/// # Math operations
#[deprecated(note="for internal use only")]
pub trait Ops: Eq + PartialEq + Ord + PartialOrd + Hash + Copy + FromStr + Display {

    /// # Saturating adds one
    fn saturating_add_one(&self) -> Self;

    /// # Saturating subtracts from one
    fn saturating_sub_one(&self) -> Self;

    /// # Min value
    fn minimum() -> Self;

    /// # Max value
    fn maximum() -> Self;

}

macro_rules! impl_ops_for_integers {
    ($($ty: ty,)+) => {
        $(
            #[allow(deprecated)]
            impl Ops for $ty {

                fn saturating_add_one(&self) -> Self {
                    self.saturating_add(1)
                }

                fn saturating_sub_one(&self) -> Self {
                    self.saturating_sub(1)
                }

                fn minimum() -> Self {
                    Self::min_value()
                }

                fn maximum() -> Self {
                    Self::max_value()
                }

            }
        )+
    };
}

impl_ops_for_integers! {
    i8, i16, i32, i64, i128, isize,
    u8, u16, u32, u64, u128, usize,
}

#[test]
#[allow(deprecated)]
fn tests() {
    macro_rules! min_max { ($($ty: ty,)+) => {
        $(
            assert_eq!(<$ty>::minimum(), <$ty>::min_value());
            assert_eq!(<$ty>::maximum(), <$ty>::max_value());
        )+
    }}
    min_max!(
        i8, i16, i32, i64, i128, isize,
        u8, u16, u32, u64, u128, usize,
    );
}