impl_ops_cmp/cmp.rs
1//! cmp
2//!
3
4/// impl_ops_cmp without Copy derive
5#[macro_export]
6macro_rules! impl_ops_cmp {
7 (impl $imp:ident, $imo:ident, $method:ident for $t:ty, $u:ty, $sgn:expr) => {
8/*
9 /// impl $imp<&$u> for &mut $t
10 /// - expect $imp as PartialEq or Eq
11 impl<'a, 'b> $imp<&'b $u> for &'a mut $t {
12 /// eq &mut $t == &$u
13 #[inline]
14 fn eq(&self, rhs: &&'b $u) -> bool {
15 self.$method(*rhs) == 0
16 }
17 }
18
19 /// impl $imo<&$u> for &mut $t
20 /// - expect $imo as PartialOrd
21 impl<'a, 'b> $imo<&'b $u> for &'a mut $t {
22 /// partial_cmp &mut $t - &$u
23 #[inline]
24 fn partial_cmp(&self, rhs: &&'b $u) -> Option<Ordering> {
25 (self.$method(*rhs) * $sgn).partial_cmp(&0)
26// let c = self.$method(*rhs) * $sgn;
27// if c == 0 { Some(Ordering::Equal) }
28// else if c < 0 { Some(Ordering::Less) }
29// else if c > 0 { Some(Ordering::Greater) }
30// else { None }
31 }
32 }
33*/
34 /// impl $imp<$u> for &mut $t
35 /// - expect $imp as PartialEq or Eq
36 impl<'a> $imp<$u> for &'a mut $t {
37 /// eq &mut $t == $u
38 #[inline]
39 fn eq(&self, rhs: &$u) -> bool {
40 self.$method(rhs) == 0
41 }
42 }
43
44 /// impl $imo<$u> for &mut $t
45 /// - expect $imo as PartialOrd
46 impl<'a> $imo<$u> for &'a mut $t {
47 /// partial_cmp &mut $t - $u
48 #[inline]
49 fn partial_cmp(&self, rhs: &$u) -> Option<Ordering> {
50 (self.$method(rhs) * $sgn).partial_cmp(&0)
51 }
52 }
53
54 /// impl $imp<$u> for $t
55 /// - expect $imp as PartialEq or Eq
56 impl $imp<$u> for $t {
57 /// eq $t == $u
58 #[inline]
59 fn eq(&self, rhs: &$u) -> bool {
60 self.$method(rhs) == 0
61 }
62 }
63
64 /// impl $imo<$u> for $t
65 /// - expect $imo as PartialOrd
66 impl $imo<$u> for $t {
67 /// partial_cmp $t - $u
68 #[inline]
69 fn partial_cmp(&self, rhs: &$u) -> Option<Ordering> {
70 (self.$method(rhs) * $sgn).partial_cmp(&0)
71 }
72 }
73 };
74}
75pub use impl_ops_cmp;
76
77/// impl_ops_cmp_p without Copy derive
78#[macro_export]
79macro_rules! impl_ops_cmp_p {
80 (impl $imp:ident, $imo:ident, $method:ident for $t:ty, $u:ty, $sgn:expr) => {
81 /// impl $imp<$u> for &mut $t
82 /// - expect $imp as PartialEq or Eq
83 impl<'a> $imp<$u> for &'a mut $t {
84 /// eq &mut $t == $u
85 #[inline]
86 fn eq(&self, rhs: &$u) -> bool {
87 // not use self.eq to avoid recursion infinite
88 <$t>::eq(self, rhs)
89 }
90 }
91
92 /// impl $imo<$u> for &mut $t
93 /// - expect $imo as PartialOrd
94 impl<'a> $imo<$u> for &'a mut $t {
95 /// partial_cmp &mut $t - $u
96 #[inline]
97 fn partial_cmp(&self, rhs: &$u) -> Option<Ordering> {
98 // not use self.partial_cmp to avoid recursion infinite
99 <$t>::partial_cmp(self, rhs)
100 }
101 }
102
103 /// impl $imp<$u> for &$t
104 /// - expect $imp as PartialEq or Eq
105 impl<'a> $imp<$u> for &'a $t {
106 /// eq &$t == $u
107 #[inline]
108 fn eq(&self, rhs: &$u) -> bool {
109 // not use self.eq to avoid recursion infinite
110 <$t>::eq(self, rhs)
111 }
112 }
113
114 /// impl $imo<$u> for &$t
115 /// - expect $imo as PartialOrd
116 impl<'a> $imo<$u> for &'a $t {
117 /// partial_cmp &$t - $u
118 #[inline]
119 fn partial_cmp(&self, rhs: &$u) -> Option<Ordering> {
120 // not use self.partial_cmp to avoid recursion infinite
121 <$t>::partial_cmp(self, rhs)
122 }
123 }
124
125 /// impl $imp<$u> for $t
126 /// - expect $imp as PartialEq or Eq
127 impl $imp<$u> for $t {
128 /// eq $t == $u
129 #[inline]
130 fn eq(&self, rhs: &$u) -> bool {
131 self.$method(*rhs) == 0
132 }
133 }
134
135 /// impl $imo<$u> for $t
136 /// - expect $imo as PartialOrd
137 impl $imo<$u> for $t {
138 /// partial_cmp $t - $u
139 #[inline]
140 fn partial_cmp(&self, rhs: &$u) -> Option<Ordering> {
141 (self.$method(*rhs) * $sgn).partial_cmp(&0)
142 }
143 }
144
145 impl_ops_cmp_q!{impl $imp, $imo, for $u, $t}
146 };
147}
148pub use impl_ops_cmp_p;
149
150/// impl_ops_cmp_q without Copy derive
151#[macro_export]
152macro_rules! impl_ops_cmp_q {
153 (impl $imp:ident, $imo:ident, for $t:ty, $u:ty) => {
154 /// impl $imp<&mut $u> for $t
155 /// - expect $imp as PartialEq or Eq
156 impl<'a> $imp<&'a mut $u> for $t {
157 /// eq $t == &mut $u
158 #[inline]
159 fn eq(&self, rhs: &&'a mut $u) -> bool {
160 (*rhs).eq(self)
161 }
162 }
163
164 /// impl $imo<&mut $u> for $t
165 /// - expect $imo as PartialOrd
166 impl<'a> $imo<&'a mut $u> for $t {
167 /// partial_cmp $t - &mut $u
168 #[inline]
169 fn partial_cmp(&self, rhs: &&'a mut $u) -> Option<Ordering> {
170 (*rhs).partial_cmp(self).map(Ordering::reverse)
171 }
172 }
173
174 /// impl $imp<&$u> for $t
175 /// - expect $imp as PartialEq or Eq
176 impl<'a> $imp<&'a $u> for $t {
177 /// eq $t == &$u
178 #[inline]
179 fn eq(&self, rhs: &&'a $u) -> bool {
180 (*rhs).eq(self)
181 }
182 }
183
184 /// impl $imo<&$u> for $t
185 /// - expect $imo as PartialOrd
186 impl<'a> $imo<&'a $u> for $t {
187 /// partial_cmp $t - &$u
188 #[inline]
189 fn partial_cmp(&self, rhs: &&'a $u) -> Option<Ordering> {
190 (*rhs).partial_cmp(self).map(Ordering::reverse)
191 }
192 }
193
194 /// impl $imp<$u> for $t
195 /// - expect $imp as PartialEq or Eq
196 impl $imp<$u> for $t {
197 /// eq $t == $u
198 #[inline]
199 fn eq(&self, rhs: &$u) -> bool {
200 rhs.eq(self)
201 }
202 }
203
204 /// impl $imo<$u> for $t
205 /// - expect $imo as PartialOrd
206 impl $imo<$u> for $t {
207 /// partial_cmp $t - $u
208 #[inline]
209 fn partial_cmp(&self, rhs: &$u) -> Option<Ordering> {
210 rhs.partial_cmp(self).map(Ordering::reverse)
211 }
212 }
213 };
214}
215pub use impl_ops_cmp_q;