relp_num/
one.rs

1//! # One
2use std::fmt;
3use std::ops::{Add, AddAssign, Div, };
4use std::ops::Mul;
5
6use crate::NonZero;
7
8/// A type representing the value `1`.
9///
10/// Can be used when a type from the `MatrixProvider` can only have the value `1`, such as with some
11/// certain network problems, where the cost of a path might always equal `1`.
12///
13/// This type is zero-sized.
14#[derive(Ord, PartialOrd, Eq, PartialEq, Clone)]
15pub struct One;
16
17impl num_traits::One for One {
18    #[inline]
19    #[must_use]
20    fn one() -> Self {
21        Self
22    }
23}
24
25impl Default for One {
26    fn default() -> Self {
27        One
28    }
29}
30
31impl Mul<One> for One {
32    type Output = Self;
33
34    #[inline]
35    #[must_use]
36    fn mul(self, _rhs: One) -> Self::Output {
37        Self
38    }
39}
40
41impl NonZero for One {
42    #[inline]
43    #[must_use]
44    fn is_not_zero(&self) -> bool {
45        true
46    }
47}
48
49impl fmt::Debug for One {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        fmt::Display::fmt(&self, f)
52    }
53}
54
55impl fmt::Display for One {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.write_str("1")
58    }
59}
60
61macro_rules! define_ops {
62    ($primitive:ident) => {
63        impl From<One> for $primitive {
64            fn from(_: One) -> Self {
65                1
66            }
67        }
68
69        impl From<&One> for $primitive {
70            fn from(_: &One) -> Self {
71                1
72            }
73        }
74
75        impl Add<One> for $primitive {
76            type Output = Self;
77
78            fn add(self, _: One) -> Self::Output {
79                self + 1
80            }
81        }
82
83        impl Add<&One> for $primitive {
84            type Output = Self;
85
86            fn add(self, _: &One) -> Self::Output {
87                self + 1
88            }
89        }
90
91        impl AddAssign<&One> for $primitive {
92            fn add_assign(&mut self, _: &One) {
93                *self += 1;
94            }
95        }
96
97        impl Mul<One> for $primitive {
98            type Output = Self;
99
100            fn mul(self, _: One) -> Self::Output {
101                self
102            }
103        }
104
105        impl Mul<&One> for $primitive {
106            type Output = Self;
107
108            fn mul(self, _: &One) -> Self::Output {
109                self
110            }
111        }
112
113        impl Mul<&One> for &$primitive {
114            type Output = $primitive;
115
116            fn mul(self, _: &One) -> Self::Output {
117                *self
118            }
119        }
120
121        impl Div<One> for $primitive {
122            type Output = Self;
123
124            fn div(self, _: One) -> Self::Output {
125                self
126            }
127        }
128
129        impl Div<&One> for $primitive {
130            type Output = Self;
131
132            fn div(self, _: &One) -> Self::Output {
133                self
134            }
135        }
136    }
137}
138
139define_ops!(i8);
140define_ops!(i16);
141define_ops!(i32);
142define_ops!(i64);
143define_ops!(i128);
144define_ops!(u8);
145define_ops!(u16);
146define_ops!(u32);
147define_ops!(u64);
148define_ops!(u128);
149
150#[cfg(test)]
151mod test {
152    use crate::One;
153
154    #[test]
155    fn test_integer() {
156        assert_eq!(1 + One, 2);
157        assert_eq!(-1 + One, 0);
158        assert_eq!(33 / One, 33);
159        assert_eq!(-33 / &One, -33);
160        assert_eq!(894 * One, 894);
161        assert_eq!(-894 * &One, -894);
162        assert_eq!(0_u8 * &One, 0);
163    }
164}