Skip to main content

hexga_number/arithmetic/
increment.rs

1use super::*;
2
3/// The `+1` operation
4pub trait Increment: One + Add<Self, Output = Self> + AddAssign<Self> + Copy + Sized
5{
6    /// The increment `x++` operator
7    #[inline(always)]
8    fn increment(&mut self) { *self += Self::ONE; }
9    /// The increment `x++` operator
10    #[inline(always)]
11    fn increment_checked(&mut self) -> Result<(), ()>
12    where
13        Self: OverflowBehavior + MaxValue + PartialEq,
14    {
15        if self.can_increment()
16        {
17            self.increment();
18            Ok(())
19        }
20        else
21        {
22            Err(())
23        }
24    }
25
26    /// Do the current value have a successor.
27    ///
28    /// True if not Self::MAX, except for wrapping type, they always have a successor because they wrap
29    #[inline(always)]
30    fn can_increment(&self) -> bool
31    where
32        Self: OverflowBehavior + MaxValue + PartialEq,
33    {
34        Self::OVERFLOW_BEHAVIOR.is_wrapping() || self.is_not_max()
35    }
36
37    /// Return the successor `self + 1`
38    #[inline(always)]
39    fn successor(self) -> Self { self + Self::ONE }
40    /// Return the successor `self + 1`
41    #[inline(always)]
42    fn successor_checked(&mut self) -> Result<Self, ()>
43    where
44        Self: OverflowBehavior + MaxValue + PartialEq,
45    {
46        if self.have_successor() { Ok(self.successor()) } else { Err(()) }
47    }
48
49    /// Do the current value have a successor.
50    ///
51    /// True if not Self::MAX, except for wrapping type, they always have a successor because they wrap
52    #[inline(always)]
53    fn have_successor(self) -> bool
54    where
55        Self: OverflowBehavior + MaxValue + PartialEq,
56    {
57        self.can_increment()
58    }
59}
60impl<T> Increment for T where T: One + Add<T, Output = T> + AddAssign<Self> + Copy {}
61
62/// The `-1` operation
63pub trait Decrement: One + Sub<Self, Output = Self> + SubAssign<Self> + Copy + Sized
64{
65    /// The decrement `x--` operator
66    #[inline(always)]
67    fn decrement(&mut self) { *self -= Self::ONE; }
68    /// The increment `x--` operator
69    #[inline(always)]
70    fn decrement_checked(&mut self) -> Result<(), ()>
71    where
72        Self: OverflowBehavior + MinValue + PartialEq,
73    {
74        if self.can_decrement()
75        {
76            self.decrement();
77            Ok(())
78        }
79        else
80        {
81            Err(())
82        }
83    }
84
85    /// Do the current value have a predecessor.
86    ///
87    /// True if not Self::MIN, except for wrapping type, they always have a predecessor because they wrap
88    #[inline(always)]
89    fn can_decrement(&self) -> bool
90    where
91        Self: OverflowBehavior + MinValue + PartialEq,
92    {
93        Self::OVERFLOW_BEHAVIOR.is_wrapping() || self.is_not_min()
94    }
95
96    /// Return the predecessor `self - 1`
97    #[inline(always)]
98    fn predecessor(self) -> Self { self - Self::ONE }
99    /// Return the predecessor `self - 1`
100    #[inline(always)]
101    fn predecessor_checked(&mut self) -> Result<Self, ()>
102    where
103        Self: OverflowBehavior + MinValue + PartialEq,
104    {
105        if self.have_predecessor() { Ok(self.predecessor()) } else { Err(()) }
106    }
107
108    /// Do the current value have a predecessor.
109    ///
110    /// True if not Self::MIN, except for wrapping type, they always have a predecessor because they wrap
111    #[inline(always)]
112    fn have_predecessor(self) -> bool
113    where
114        Self: OverflowBehavior + MinValue + PartialEq,
115    {
116        self.can_decrement()
117    }
118}
119impl<T> Decrement for T where T: One + Sub<Self, Output = Self> + SubAssign<Self> + Copy + Sized {}