Skip to main content

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    fn one() -> Self {
20        Self
21    }
22}
23
24impl Default for One {
25    fn default() -> Self {
26        One
27    }
28}
29
30impl Mul<One> for One {
31    type Output = Self;
32
33    #[inline]
34    fn mul(self, _rhs: One) -> Self::Output {
35        Self
36    }
37}
38
39impl NonZero for One {
40    #[inline]
41    fn is_not_zero(&self) -> bool {
42        true
43    }
44}
45
46impl fmt::Debug for One {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        fmt::Display::fmt(&self, f)
49    }
50}
51
52impl fmt::Display for One {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.write_str("1")
55    }
56}
57
58macro_rules! define_ops {
59    ($primitive:ident) => {
60        impl From<One> for $primitive {
61            fn from(_: One) -> Self {
62                1
63            }
64        }
65
66        impl From<&One> for $primitive {
67            fn from(_: &One) -> Self {
68                1
69            }
70        }
71
72        impl Add<One> for $primitive {
73            type Output = Self;
74
75            fn add(self, _: One) -> Self::Output {
76                self + 1
77            }
78        }
79
80        impl Add<&One> for $primitive {
81            type Output = Self;
82
83            fn add(self, _: &One) -> Self::Output {
84                self + 1
85            }
86        }
87
88        impl AddAssign<&One> for $primitive {
89            fn add_assign(&mut self, _: &One) {
90                *self += 1;
91            }
92        }
93
94        impl Mul<One> for $primitive {
95            type Output = Self;
96
97            fn mul(self, _: One) -> Self::Output {
98                self
99            }
100        }
101
102        impl Mul<&One> for $primitive {
103            type Output = Self;
104
105            fn mul(self, _: &One) -> Self::Output {
106                self
107            }
108        }
109
110        impl Mul<&One> for &$primitive {
111            type Output = $primitive;
112
113            fn mul(self, _: &One) -> Self::Output {
114                *self
115            }
116        }
117
118        impl Div<One> for $primitive {
119            type Output = Self;
120
121            fn div(self, _: One) -> Self::Output {
122                self
123            }
124        }
125
126        impl Div<&One> for $primitive {
127            type Output = Self;
128
129            fn div(self, _: &One) -> Self::Output {
130                self
131            }
132        }
133    }
134}
135
136define_ops!(i8);
137define_ops!(i16);
138define_ops!(i32);
139define_ops!(i64);
140define_ops!(i128);
141define_ops!(u8);
142define_ops!(u16);
143define_ops!(u32);
144define_ops!(u64);
145define_ops!(u128);
146
147#[cfg(test)]
148mod test {
149    use crate::One;
150
151    #[test]
152    fn test_integer() {
153        assert_eq!(1 + One, 2);
154        assert_eq!(-1 + One, 0);
155        assert_eq!(33 / One, 33);
156        assert_eq!(-33 / &One, -33);
157        assert_eq!(894 * One, 894);
158        assert_eq!(-894 * &One, -894);
159        assert_eq!(0_u8 * &One, 0);
160    }
161}