rstm_core/traits/
increment.rs

1/*
2    Appellation: increment <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5
6/// [Decrement] is a trait that provides a common interface for decrementing values; i.e.,
7/// subtracting one from a value.
8pub trait Decrement {
9    fn decrement(self) -> Self;
10}
11
12/// [DecrementAssign] is a trait that provides a common interface for decrementing values in
13/// place.
14pub trait DecrementAssign {
15    fn decrement_assign(&mut self);
16}
17
18/// [Increment] is a trait that provides a common interface for incrementing values; i.e.,
19/// adding one to a value.
20pub trait Increment {
21    fn increment(self) -> Self;
22}
23
24/// [IncrementAssign] is a trait that provides a common interface for incrementing values in
25/// place.
26pub trait IncrementAssign {
27    fn increment_assign(&mut self);
28}
29
30/// [Incremental] is a trait that provides a common interface for incrementing and decrementing values.
31pub trait Incremental: Decrement + Increment + DecrementAssign + IncrementAssign {}
32
33/*
34 ************* Implementations *************
35 */
36use num_traits::One;
37impl<T> Decrement for T
38where
39    T: One + core::ops::Sub<Output = T>,
40{
41    fn decrement(self) -> Self {
42        self - T::one()
43    }
44}
45
46impl<T> DecrementAssign for T
47where
48    T: One + core::ops::SubAssign,
49{
50    fn decrement_assign(&mut self) {
51        *self -= T::one();
52    }
53}
54
55impl<T> Increment for T
56where
57    T: One + core::ops::Add<Output = T>,
58{
59    fn increment(self) -> Self {
60        self + T::one()
61    }
62}
63
64impl<T> IncrementAssign for T
65where
66    T: One + core::ops::AddAssign,
67{
68    fn increment_assign(&mut self) {
69        *self += T::one();
70    }
71}
72
73impl<T> Incremental for T where T: Decrement + Increment + DecrementAssign + IncrementAssign {}