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