]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
85aaf69f | 11 | //! Functionality for ordering and comparison. |
1a4d82fc | 12 | //! |
62682a34 SL |
13 | //! This module defines both `PartialOrd` and `PartialEq` traits which are used |
14 | //! by the compiler to implement comparison operators. Rust programs may | |
15 | //! implement `PartialOrd` to overload the `<`, `<=`, `>`, and `>=` operators, | |
16 | //! and may implement `PartialEq` to overload the `==` and `!=` operators. | |
1a4d82fc | 17 | |
85aaf69f | 18 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
19 | |
20 | use self::Ordering::*; | |
21 | ||
22 | use marker::Sized; | |
23 | use option::Option::{self, Some, None}; | |
24 | ||
62682a34 SL |
25 | /// Trait for equality comparisons which are [partial equivalence |
26 | /// relations](http://en.wikipedia.org/wiki/Partial_equivalence_relation). | |
1a4d82fc | 27 | /// |
62682a34 SL |
28 | /// This trait allows for partial equality, for types that do not have a full |
29 | /// equivalence relation. For example, in floating point numbers `NaN != NaN`, | |
30 | /// so floating point types implement `PartialEq` but not `Eq`. | |
1a4d82fc JJ |
31 | /// |
32 | /// Formally, the equality must be (for all `a`, `b` and `c`): | |
33 | /// | |
34 | /// - symmetric: `a == b` implies `b == a`; and | |
35 | /// - transitive: `a == b` and `b == c` implies `a == c`. | |
36 | /// | |
62682a34 SL |
37 | /// Note that these requirements mean that the trait itself must be implemented |
38 | /// symmetrically and transitively: if `T: PartialEq<U>` and `U: PartialEq<V>` | |
39 | /// then `U: PartialEq<T>` and `T: PartialEq<V>`. | |
1a4d82fc | 40 | /// |
62682a34 SL |
41 | /// PartialEq only requires the `eq` method to be implemented; `ne` is defined |
42 | /// in terms of it by default. Any manual implementation of `ne` *must* respect | |
43 | /// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and | |
44 | /// only if `a != b`. | |
d9579d0f | 45 | #[lang = "eq"] |
85aaf69f | 46 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 47 | pub trait PartialEq<Rhs: ?Sized = Self> { |
62682a34 SL |
48 | /// This method tests for `self` and `other` values to be equal, and is used |
49 | /// by `==`. | |
85aaf69f | 50 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
51 | fn eq(&self, other: &Rhs) -> bool; |
52 | ||
53 | /// This method tests for `!=`. | |
54 | #[inline] | |
85aaf69f | 55 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
56 | fn ne(&self, other: &Rhs) -> bool { !self.eq(other) } |
57 | } | |
58 | ||
59 | /// Trait for equality comparisons which are [equivalence relations]( | |
60 | /// https://en.wikipedia.org/wiki/Equivalence_relation). | |
61 | /// | |
85aaf69f SL |
62 | /// This means, that in addition to `a == b` and `a != b` being strict inverses, the equality must |
63 | /// be (for all `a`, `b` and `c`): | |
1a4d82fc JJ |
64 | /// |
65 | /// - reflexive: `a == a`; | |
66 | /// - symmetric: `a == b` implies `b == a`; and | |
67 | /// - transitive: `a == b` and `b == c` implies `a == c`. | |
85aaf69f SL |
68 | /// |
69 | /// This property cannot be checked by the compiler, and therefore `Eq` implies | |
70 | /// `PartialEq`, and has no extra methods. | |
71 | #[stable(feature = "rust1", since = "1.0.0")] | |
1a4d82fc JJ |
72 | pub trait Eq: PartialEq<Self> { |
73 | // FIXME #13101: this method is used solely by #[deriving] to | |
74 | // assert that every component of a type implements #[deriving] | |
75 | // itself, the current deriving infrastructure means doing this | |
76 | // assertion without using a method on this trait is nearly | |
77 | // impossible. | |
78 | // | |
79 | // This should never be implemented by hand. | |
80 | #[doc(hidden)] | |
81 | #[inline(always)] | |
85aaf69f | 82 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
83 | fn assert_receiver_is_total_eq(&self) {} |
84 | } | |
85 | ||
85aaf69f SL |
86 | /// An `Ordering` is the result of a comparison between two values. |
87 | /// | |
88 | /// # Examples | |
89 | /// | |
90 | /// ``` | |
91 | /// use std::cmp::Ordering; | |
92 | /// | |
93 | /// let result = 1.cmp(&2); | |
94 | /// assert_eq!(Ordering::Less, result); | |
95 | /// | |
96 | /// let result = 1.cmp(&1); | |
97 | /// assert_eq!(Ordering::Equal, result); | |
98 | /// | |
99 | /// let result = 2.cmp(&1); | |
100 | /// assert_eq!(Ordering::Greater, result); | |
101 | /// ``` | |
102 | #[derive(Clone, Copy, PartialEq, Debug)] | |
103 | #[stable(feature = "rust1", since = "1.0.0")] | |
1a4d82fc JJ |
104 | pub enum Ordering { |
105 | /// An ordering where a compared value is less [than another]. | |
85aaf69f SL |
106 | #[stable(feature = "rust1", since = "1.0.0")] |
107 | Less = -1, | |
1a4d82fc | 108 | /// An ordering where a compared value is equal [to another]. |
85aaf69f SL |
109 | #[stable(feature = "rust1", since = "1.0.0")] |
110 | Equal = 0, | |
1a4d82fc | 111 | /// An ordering where a compared value is greater [than another]. |
85aaf69f SL |
112 | #[stable(feature = "rust1", since = "1.0.0")] |
113 | Greater = 1, | |
1a4d82fc JJ |
114 | } |
115 | ||
116 | impl Ordering { | |
85aaf69f | 117 | /// Reverse the `Ordering`. |
1a4d82fc | 118 | /// |
85aaf69f SL |
119 | /// * `Less` becomes `Greater`. |
120 | /// * `Greater` becomes `Less`. | |
121 | /// * `Equal` becomes `Equal`. | |
1a4d82fc | 122 | /// |
85aaf69f | 123 | /// # Examples |
1a4d82fc | 124 | /// |
85aaf69f | 125 | /// Basic behavior: |
1a4d82fc | 126 | /// |
85aaf69f SL |
127 | /// ``` |
128 | /// use std::cmp::Ordering; | |
129 | /// | |
130 | /// assert_eq!(Ordering::Less.reverse(), Ordering::Greater); | |
131 | /// assert_eq!(Ordering::Equal.reverse(), Ordering::Equal); | |
132 | /// assert_eq!(Ordering::Greater.reverse(), Ordering::Less); | |
133 | /// ``` | |
134 | /// | |
135 | /// This method can be used to reverse a comparison: | |
136 | /// | |
137 | /// ``` | |
138 | /// use std::cmp::Ordering; | |
139 | /// | |
140 | /// let mut data: &mut [_] = &mut [2, 10, 5, 8]; | |
1a4d82fc JJ |
141 | /// |
142 | /// // sort the array from largest to smallest. | |
143 | /// data.sort_by(|a, b| a.cmp(b).reverse()); | |
144 | /// | |
85aaf69f | 145 | /// let b: &mut [_] = &mut [10, 8, 5, 2]; |
1a4d82fc JJ |
146 | /// assert!(data == b); |
147 | /// ``` | |
148 | #[inline] | |
85aaf69f | 149 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
150 | pub fn reverse(self) -> Ordering { |
151 | unsafe { | |
152 | // this compiles really nicely (to a single instruction); | |
153 | // an explicit match has a pile of branches and | |
154 | // comparisons. | |
155 | // | |
156 | // NB. it is safe because of the explicit discriminants | |
157 | // given above. | |
158 | ::mem::transmute::<_, Ordering>(-(self as i8)) | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
85aaf69f | 163 | /// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order). |
1a4d82fc JJ |
164 | /// |
165 | /// An order is a total order if it is (for all `a`, `b` and `c`): | |
166 | /// | |
85aaf69f SL |
167 | /// - total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and |
168 | /// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`. | |
c1a9b12d SL |
169 | /// |
170 | /// When this trait is `derive`d, it produces a lexicographic ordering. | |
85aaf69f | 171 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 172 | pub trait Ord: Eq + PartialOrd<Self> { |
85aaf69f SL |
173 | /// This method returns an `Ordering` between `self` and `other`. |
174 | /// | |
175 | /// By convention, `self.cmp(&other)` returns the ordering matching the expression | |
176 | /// `self <operator> other` if true. | |
1a4d82fc | 177 | /// |
85aaf69f | 178 | /// # Examples |
1a4d82fc JJ |
179 | /// |
180 | /// ``` | |
85aaf69f | 181 | /// use std::cmp::Ordering; |
1a4d82fc | 182 | /// |
85aaf69f SL |
183 | /// assert_eq!(5.cmp(&10), Ordering::Less); |
184 | /// assert_eq!(10.cmp(&5), Ordering::Greater); | |
185 | /// assert_eq!(5.cmp(&5), Ordering::Equal); | |
1a4d82fc | 186 | /// ``` |
85aaf69f | 187 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
188 | fn cmp(&self, other: &Self) -> Ordering; |
189 | } | |
190 | ||
85aaf69f | 191 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
192 | impl Eq for Ordering {} |
193 | ||
85aaf69f | 194 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
195 | impl Ord for Ordering { |
196 | #[inline] | |
85aaf69f | 197 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 198 | fn cmp(&self, other: &Ordering) -> Ordering { |
85aaf69f | 199 | (*self as i32).cmp(&(*other as i32)) |
1a4d82fc JJ |
200 | } |
201 | } | |
202 | ||
85aaf69f | 203 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
204 | impl PartialOrd for Ordering { |
205 | #[inline] | |
85aaf69f | 206 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 207 | fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> { |
85aaf69f | 208 | (*self as i32).partial_cmp(&(*other as i32)) |
1a4d82fc JJ |
209 | } |
210 | } | |
211 | ||
212 | /// Trait for values that can be compared for a sort-order. | |
213 | /// | |
214 | /// The comparison must satisfy, for all `a`, `b` and `c`: | |
215 | /// | |
216 | /// - antisymmetry: if `a < b` then `!(a > b)` and vice versa; and | |
85aaf69f | 217 | /// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`. |
1a4d82fc | 218 | /// |
85aaf69f SL |
219 | /// Note that these requirements mean that the trait itself must be implemented symmetrically and |
220 | /// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T: | |
1a4d82fc JJ |
221 | /// PartialOrd<V>`. |
222 | /// | |
85aaf69f SL |
223 | /// PartialOrd only requires implementation of the `partial_cmp` method, with the others generated |
224 | /// from default implementations. | |
1a4d82fc | 225 | /// |
85aaf69f SL |
226 | /// However it remains possible to implement the others separately for types which do not have a |
227 | /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 == | |
228 | /// false` (cf. IEEE 754-2008 section 5.11). | |
d9579d0f | 229 | #[lang = "ord"] |
85aaf69f | 230 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 231 | pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> { |
85aaf69f SL |
232 | /// This method returns an ordering between `self` and `other` values if one exists. |
233 | /// | |
234 | /// # Examples | |
235 | /// | |
236 | /// ``` | |
237 | /// use std::cmp::Ordering; | |
238 | /// | |
239 | /// let result = 1.0.partial_cmp(&2.0); | |
240 | /// assert_eq!(result, Some(Ordering::Less)); | |
241 | /// | |
242 | /// let result = 1.0.partial_cmp(&1.0); | |
243 | /// assert_eq!(result, Some(Ordering::Equal)); | |
244 | /// | |
245 | /// let result = 2.0.partial_cmp(&1.0); | |
246 | /// assert_eq!(result, Some(Ordering::Greater)); | |
247 | /// ``` | |
248 | /// | |
249 | /// When comparison is impossible: | |
250 | /// | |
251 | /// ``` | |
252 | /// let result = std::f64::NAN.partial_cmp(&1.0); | |
253 | /// assert_eq!(result, None); | |
254 | /// ``` | |
255 | #[stable(feature = "rust1", since = "1.0.0")] | |
1a4d82fc JJ |
256 | fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>; |
257 | ||
258 | /// This method tests less than (for `self` and `other`) and is used by the `<` operator. | |
85aaf69f SL |
259 | /// |
260 | /// # Examples | |
261 | /// | |
262 | /// ``` | |
263 | /// use std::cmp::Ordering; | |
264 | /// | |
265 | /// let result = 1.0 < 2.0; | |
266 | /// assert_eq!(result, true); | |
267 | /// | |
268 | /// let result = 2.0 < 1.0; | |
269 | /// assert_eq!(result, false); | |
270 | /// ``` | |
1a4d82fc | 271 | #[inline] |
85aaf69f | 272 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
273 | fn lt(&self, other: &Rhs) -> bool { |
274 | match self.partial_cmp(other) { | |
275 | Some(Less) => true, | |
276 | _ => false, | |
277 | } | |
278 | } | |
279 | ||
85aaf69f SL |
280 | /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=` |
281 | /// operator. | |
282 | /// | |
283 | /// # Examples | |
284 | /// | |
285 | /// ``` | |
286 | /// let result = 1.0 <= 2.0; | |
287 | /// assert_eq!(result, true); | |
288 | /// | |
289 | /// let result = 2.0 <= 2.0; | |
290 | /// assert_eq!(result, true); | |
291 | /// ``` | |
1a4d82fc | 292 | #[inline] |
85aaf69f | 293 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
294 | fn le(&self, other: &Rhs) -> bool { |
295 | match self.partial_cmp(other) { | |
296 | Some(Less) | Some(Equal) => true, | |
297 | _ => false, | |
298 | } | |
299 | } | |
300 | ||
85aaf69f SL |
301 | /// This method tests greater than (for `self` and `other`) and is used by the `>` operator. |
302 | /// | |
303 | /// # Examples | |
304 | /// | |
305 | /// ``` | |
306 | /// let result = 1.0 > 2.0; | |
307 | /// assert_eq!(result, false); | |
308 | /// | |
309 | /// let result = 2.0 > 2.0; | |
310 | /// assert_eq!(result, false); | |
311 | /// ``` | |
1a4d82fc | 312 | #[inline] |
85aaf69f | 313 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
314 | fn gt(&self, other: &Rhs) -> bool { |
315 | match self.partial_cmp(other) { | |
316 | Some(Greater) => true, | |
317 | _ => false, | |
318 | } | |
319 | } | |
320 | ||
85aaf69f SL |
321 | /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` |
322 | /// operator. | |
323 | /// | |
324 | /// # Examples | |
325 | /// | |
326 | /// ``` | |
327 | /// let result = 2.0 >= 1.0; | |
328 | /// assert_eq!(result, true); | |
329 | /// | |
330 | /// let result = 2.0 >= 2.0; | |
331 | /// assert_eq!(result, true); | |
332 | /// ``` | |
1a4d82fc | 333 | #[inline] |
85aaf69f | 334 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
335 | fn ge(&self, other: &Rhs) -> bool { |
336 | match self.partial_cmp(other) { | |
337 | Some(Greater) | Some(Equal) => true, | |
338 | _ => false, | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | /// Compare and return the minimum of two values. | |
85aaf69f | 344 | /// |
c34b1796 AL |
345 | /// Returns the first argument if the comparison determines them to be equal. |
346 | /// | |
85aaf69f SL |
347 | /// # Examples |
348 | /// | |
349 | /// ``` | |
350 | /// use std::cmp; | |
351 | /// | |
352 | /// assert_eq!(1, cmp::min(1, 2)); | |
353 | /// assert_eq!(2, cmp::min(2, 2)); | |
354 | /// ``` | |
1a4d82fc | 355 | #[inline] |
85aaf69f | 356 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 357 | pub fn min<T: Ord>(v1: T, v2: T) -> T { |
c34b1796 | 358 | if v1 <= v2 { v1 } else { v2 } |
1a4d82fc JJ |
359 | } |
360 | ||
361 | /// Compare and return the maximum of two values. | |
85aaf69f | 362 | /// |
c34b1796 AL |
363 | /// Returns the second argument if the comparison determines them to be equal. |
364 | /// | |
85aaf69f SL |
365 | /// # Examples |
366 | /// | |
367 | /// ``` | |
368 | /// use std::cmp; | |
369 | /// | |
370 | /// assert_eq!(2, cmp::max(1, 2)); | |
371 | /// assert_eq!(2, cmp::max(2, 2)); | |
372 | /// ``` | |
1a4d82fc | 373 | #[inline] |
85aaf69f | 374 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 375 | pub fn max<T: Ord>(v1: T, v2: T) -> T { |
c34b1796 | 376 | if v2 >= v1 { v2 } else { v1 } |
1a4d82fc JJ |
377 | } |
378 | ||
379 | /// Compare and return the minimum of two values if there is one. | |
380 | /// | |
381 | /// Returns the first argument if the comparison determines them to be equal. | |
85aaf69f SL |
382 | /// |
383 | /// # Examples | |
384 | /// | |
385 | /// ``` | |
c1a9b12d SL |
386 | /// #![feature(cmp_partial)] |
387 | /// | |
85aaf69f SL |
388 | /// use std::cmp; |
389 | /// | |
390 | /// assert_eq!(Some(1), cmp::partial_min(1, 2)); | |
391 | /// assert_eq!(Some(2), cmp::partial_min(2, 2)); | |
392 | /// ``` | |
393 | /// | |
394 | /// When comparison is impossible: | |
395 | /// | |
396 | /// ``` | |
c1a9b12d SL |
397 | /// #![feature(cmp_partial)] |
398 | /// | |
85aaf69f SL |
399 | /// use std::cmp; |
400 | /// | |
401 | /// let result = cmp::partial_min(std::f64::NAN, 1.0); | |
402 | /// assert_eq!(result, None); | |
403 | /// ``` | |
1a4d82fc | 404 | #[inline] |
62682a34 | 405 | #[unstable(feature = "cmp_partial")] |
c1a9b12d | 406 | #[deprecated(since = "1.3.0", reason = "has not proven itself worthwhile")] |
1a4d82fc JJ |
407 | pub fn partial_min<T: PartialOrd>(v1: T, v2: T) -> Option<T> { |
408 | match v1.partial_cmp(&v2) { | |
409 | Some(Less) | Some(Equal) => Some(v1), | |
410 | Some(Greater) => Some(v2), | |
411 | None => None | |
412 | } | |
413 | } | |
414 | ||
415 | /// Compare and return the maximum of two values if there is one. | |
416 | /// | |
c34b1796 | 417 | /// Returns the second argument if the comparison determines them to be equal. |
85aaf69f SL |
418 | /// |
419 | /// # Examples | |
420 | /// | |
421 | /// ``` | |
c1a9b12d SL |
422 | /// #![feature(cmp_partial)] |
423 | /// | |
85aaf69f SL |
424 | /// use std::cmp; |
425 | /// | |
426 | /// assert_eq!(Some(2), cmp::partial_max(1, 2)); | |
427 | /// assert_eq!(Some(2), cmp::partial_max(2, 2)); | |
428 | /// ``` | |
429 | /// | |
430 | /// When comparison is impossible: | |
431 | /// | |
432 | /// ``` | |
c1a9b12d SL |
433 | /// #![feature(cmp_partial)] |
434 | /// | |
85aaf69f SL |
435 | /// use std::cmp; |
436 | /// | |
437 | /// let result = cmp::partial_max(std::f64::NAN, 1.0); | |
438 | /// assert_eq!(result, None); | |
439 | /// ``` | |
1a4d82fc | 440 | #[inline] |
62682a34 | 441 | #[unstable(feature = "cmp_partial")] |
c1a9b12d | 442 | #[deprecated(since = "1.3.0", reason = "has not proven itself worthwhile")] |
1a4d82fc JJ |
443 | pub fn partial_max<T: PartialOrd>(v1: T, v2: T) -> Option<T> { |
444 | match v1.partial_cmp(&v2) { | |
c34b1796 AL |
445 | Some(Equal) | Some(Less) => Some(v2), |
446 | Some(Greater) => Some(v1), | |
1a4d82fc JJ |
447 | None => None |
448 | } | |
449 | } | |
450 | ||
451 | // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types | |
452 | mod impls { | |
453 | use cmp::{PartialOrd, Ord, PartialEq, Eq, Ordering}; | |
454 | use cmp::Ordering::{Less, Greater, Equal}; | |
455 | use marker::Sized; | |
456 | use option::Option; | |
457 | use option::Option::{Some, None}; | |
458 | ||
459 | macro_rules! partial_eq_impl { | |
460 | ($($t:ty)*) => ($( | |
85aaf69f | 461 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
462 | impl PartialEq for $t { |
463 | #[inline] | |
464 | fn eq(&self, other: &$t) -> bool { (*self) == (*other) } | |
465 | #[inline] | |
466 | fn ne(&self, other: &$t) -> bool { (*self) != (*other) } | |
467 | } | |
468 | )*) | |
469 | } | |
470 | ||
85aaf69f | 471 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
472 | impl PartialEq for () { |
473 | #[inline] | |
474 | fn eq(&self, _other: &()) -> bool { true } | |
475 | #[inline] | |
476 | fn ne(&self, _other: &()) -> bool { false } | |
477 | } | |
478 | ||
479 | partial_eq_impl! { | |
85aaf69f | 480 | bool char usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 |
1a4d82fc JJ |
481 | } |
482 | ||
483 | macro_rules! eq_impl { | |
484 | ($($t:ty)*) => ($( | |
85aaf69f | 485 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
486 | impl Eq for $t {} |
487 | )*) | |
488 | } | |
489 | ||
85aaf69f | 490 | eq_impl! { () bool char usize u8 u16 u32 u64 isize i8 i16 i32 i64 } |
1a4d82fc JJ |
491 | |
492 | macro_rules! partial_ord_impl { | |
493 | ($($t:ty)*) => ($( | |
85aaf69f | 494 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
495 | impl PartialOrd for $t { |
496 | #[inline] | |
497 | fn partial_cmp(&self, other: &$t) -> Option<Ordering> { | |
498 | match (self <= other, self >= other) { | |
499 | (false, false) => None, | |
500 | (false, true) => Some(Greater), | |
501 | (true, false) => Some(Less), | |
502 | (true, true) => Some(Equal), | |
503 | } | |
504 | } | |
505 | #[inline] | |
506 | fn lt(&self, other: &$t) -> bool { (*self) < (*other) } | |
507 | #[inline] | |
508 | fn le(&self, other: &$t) -> bool { (*self) <= (*other) } | |
509 | #[inline] | |
510 | fn ge(&self, other: &$t) -> bool { (*self) >= (*other) } | |
511 | #[inline] | |
512 | fn gt(&self, other: &$t) -> bool { (*self) > (*other) } | |
513 | } | |
514 | )*) | |
515 | } | |
516 | ||
85aaf69f | 517 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
518 | impl PartialOrd for () { |
519 | #[inline] | |
520 | fn partial_cmp(&self, _: &()) -> Option<Ordering> { | |
521 | Some(Equal) | |
522 | } | |
523 | } | |
524 | ||
85aaf69f | 525 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
526 | impl PartialOrd for bool { |
527 | #[inline] | |
528 | fn partial_cmp(&self, other: &bool) -> Option<Ordering> { | |
529 | (*self as u8).partial_cmp(&(*other as u8)) | |
530 | } | |
531 | } | |
532 | ||
85aaf69f | 533 | partial_ord_impl! { char usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 } |
1a4d82fc JJ |
534 | |
535 | macro_rules! ord_impl { | |
536 | ($($t:ty)*) => ($( | |
85aaf69f | 537 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
538 | impl Ord for $t { |
539 | #[inline] | |
540 | fn cmp(&self, other: &$t) -> Ordering { | |
541 | if *self < *other { Less } | |
542 | else if *self > *other { Greater } | |
543 | else { Equal } | |
544 | } | |
545 | } | |
546 | )*) | |
547 | } | |
548 | ||
85aaf69f | 549 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
550 | impl Ord for () { |
551 | #[inline] | |
552 | fn cmp(&self, _other: &()) -> Ordering { Equal } | |
553 | } | |
554 | ||
85aaf69f | 555 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
556 | impl Ord for bool { |
557 | #[inline] | |
558 | fn cmp(&self, other: &bool) -> Ordering { | |
559 | (*self as u8).cmp(&(*other as u8)) | |
560 | } | |
561 | } | |
562 | ||
85aaf69f | 563 | ord_impl! { char usize u8 u16 u32 u64 isize i8 i16 i32 i64 } |
1a4d82fc JJ |
564 | |
565 | // & pointers | |
566 | ||
85aaf69f | 567 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
568 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a A where A: PartialEq<B> { |
569 | #[inline] | |
570 | fn eq(&self, other: & &'b B) -> bool { PartialEq::eq(*self, *other) } | |
571 | #[inline] | |
572 | fn ne(&self, other: & &'b B) -> bool { PartialEq::ne(*self, *other) } | |
573 | } | |
85aaf69f | 574 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
575 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b B> for &'a A where A: PartialOrd<B> { |
576 | #[inline] | |
577 | fn partial_cmp(&self, other: &&'b B) -> Option<Ordering> { | |
578 | PartialOrd::partial_cmp(*self, *other) | |
579 | } | |
580 | #[inline] | |
581 | fn lt(&self, other: & &'b B) -> bool { PartialOrd::lt(*self, *other) } | |
582 | #[inline] | |
583 | fn le(&self, other: & &'b B) -> bool { PartialOrd::le(*self, *other) } | |
584 | #[inline] | |
585 | fn ge(&self, other: & &'b B) -> bool { PartialOrd::ge(*self, *other) } | |
586 | #[inline] | |
587 | fn gt(&self, other: & &'b B) -> bool { PartialOrd::gt(*self, *other) } | |
588 | } | |
85aaf69f | 589 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
590 | impl<'a, A: ?Sized> Ord for &'a A where A: Ord { |
591 | #[inline] | |
592 | fn cmp(&self, other: & &'a A) -> Ordering { Ord::cmp(*self, *other) } | |
593 | } | |
85aaf69f | 594 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
595 | impl<'a, A: ?Sized> Eq for &'a A where A: Eq {} |
596 | ||
597 | // &mut pointers | |
598 | ||
85aaf69f | 599 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
600 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> { |
601 | #[inline] | |
602 | fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) } | |
603 | #[inline] | |
604 | fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) } | |
605 | } | |
85aaf69f | 606 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
607 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b mut B> for &'a mut A where A: PartialOrd<B> { |
608 | #[inline] | |
609 | fn partial_cmp(&self, other: &&'b mut B) -> Option<Ordering> { | |
610 | PartialOrd::partial_cmp(*self, *other) | |
611 | } | |
612 | #[inline] | |
613 | fn lt(&self, other: &&'b mut B) -> bool { PartialOrd::lt(*self, *other) } | |
614 | #[inline] | |
615 | fn le(&self, other: &&'b mut B) -> bool { PartialOrd::le(*self, *other) } | |
616 | #[inline] | |
617 | fn ge(&self, other: &&'b mut B) -> bool { PartialOrd::ge(*self, *other) } | |
618 | #[inline] | |
619 | fn gt(&self, other: &&'b mut B) -> bool { PartialOrd::gt(*self, *other) } | |
620 | } | |
85aaf69f | 621 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
622 | impl<'a, A: ?Sized> Ord for &'a mut A where A: Ord { |
623 | #[inline] | |
624 | fn cmp(&self, other: &&'a mut A) -> Ordering { Ord::cmp(*self, *other) } | |
625 | } | |
85aaf69f | 626 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
627 | impl<'a, A: ?Sized> Eq for &'a mut A where A: Eq {} |
628 | ||
85aaf69f | 629 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
630 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> { |
631 | #[inline] | |
632 | fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) } | |
633 | #[inline] | |
634 | fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) } | |
635 | } | |
636 | ||
85aaf69f | 637 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
638 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> { |
639 | #[inline] | |
640 | fn eq(&self, other: &&'b B) -> bool { PartialEq::eq(*self, *other) } | |
641 | #[inline] | |
642 | fn ne(&self, other: &&'b B) -> bool { PartialEq::ne(*self, *other) } | |
643 | } | |
644 | } |