]> git.lizzy.rs Git - rust.git/blob - src/libcore/cmp.rs
Auto merge of #35854 - nikomatsakis:incr-comp-cache-hash-35549, r=mw
[rust.git] / src / libcore / cmp.rs
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
11 //! Functionality for ordering and comparison.
12 //!
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.
17 //!
18 //! # Examples
19 //!
20 //! ```
21 //! let x: u32 = 0;
22 //! let y: u32 = 1;
23 //!
24 //! // these two lines are equivalent
25 //! assert_eq!(x < y, true);
26 //! assert_eq!(x.lt(&y), true);
27 //!
28 //! // these two lines are also equivalent
29 //! assert_eq!(x == y, false);
30 //! assert_eq!(x.eq(&y), false);
31 //! ```
32
33 #![stable(feature = "rust1", since = "1.0.0")]
34
35 use self::Ordering::*;
36
37 use marker::Sized;
38 use option::Option::{self, Some};
39
40 /// Trait for equality comparisons which are [partial equivalence
41 /// relations](http://en.wikipedia.org/wiki/Partial_equivalence_relation).
42 ///
43 /// This trait allows for partial equality, for types that do not have a full
44 /// equivalence relation.  For example, in floating point numbers `NaN != NaN`,
45 /// so floating point types implement `PartialEq` but not `Eq`.
46 ///
47 /// Formally, the equality must be (for all `a`, `b` and `c`):
48 ///
49 /// - symmetric: `a == b` implies `b == a`; and
50 /// - transitive: `a == b` and `b == c` implies `a == c`.
51 ///
52 /// Note that these requirements mean that the trait itself must be implemented
53 /// symmetrically and transitively: if `T: PartialEq<U>` and `U: PartialEq<V>`
54 /// then `U: PartialEq<T>` and `T: PartialEq<V>`.
55 ///
56 /// ## Derivable
57 ///
58 /// This trait can be used with `#[derive]`. When `derive`d on structs, two
59 /// instances are equal if all fields are equal, and not equal if any fields
60 /// are not equal. When `derive`d on enums, each variant is equal to itself
61 /// and not equal to the other variants.
62 ///
63 /// ## How can I implement `PartialEq`?
64 ///
65 /// PartialEq only requires the `eq` method to be implemented; `ne` is defined
66 /// in terms of it by default. Any manual implementation of `ne` *must* respect
67 /// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and
68 /// only if `a != b`.
69 ///
70 /// An example implementation for a domain in which two books are considered
71 /// the same book if their ISBN matches, even if the formats differ:
72 ///
73 /// ```
74 /// enum BookFormat { Paperback, Hardback, Ebook }
75 /// struct Book {
76 ///     isbn: i32,
77 ///     format: BookFormat,
78 /// }
79 ///
80 /// impl PartialEq for Book {
81 ///     fn eq(&self, other: &Book) -> bool {
82 ///         self.isbn == other.isbn
83 ///     }
84 /// }
85 ///
86 /// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
87 /// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
88 /// let b3 = Book { isbn: 10, format: BookFormat::Paperback };
89 ///
90 /// assert!(b1 == b2);
91 /// assert!(b1 != b3);
92 /// ```
93 ///
94 /// # Examples
95 ///
96 /// ```
97 /// let x: u32 = 0;
98 /// let y: u32 = 1;
99 ///
100 /// assert_eq!(x == y, false);
101 /// assert_eq!(x.eq(&y), false);
102 /// ```
103 #[lang = "eq"]
104 #[stable(feature = "rust1", since = "1.0.0")]
105 pub trait PartialEq<Rhs: ?Sized = Self> {
106     /// This method tests for `self` and `other` values to be equal, and is used
107     /// by `==`.
108     #[stable(feature = "rust1", since = "1.0.0")]
109     fn eq(&self, other: &Rhs) -> bool;
110
111     /// This method tests for `!=`.
112     #[inline]
113     #[stable(feature = "rust1", since = "1.0.0")]
114     fn ne(&self, other: &Rhs) -> bool { !self.eq(other) }
115 }
116
117 /// Trait for equality comparisons which are [equivalence relations](
118 /// https://en.wikipedia.org/wiki/Equivalence_relation).
119 ///
120 /// This means, that in addition to `a == b` and `a != b` being strict inverses, the equality must
121 /// be (for all `a`, `b` and `c`):
122 ///
123 /// - reflexive: `a == a`;
124 /// - symmetric: `a == b` implies `b == a`; and
125 /// - transitive: `a == b` and `b == c` implies `a == c`.
126 ///
127 /// This property cannot be checked by the compiler, and therefore `Eq` implies
128 /// `PartialEq`, and has no extra methods.
129 ///
130 /// ## Derivable
131 ///
132 /// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has
133 /// no extra methods, it is only informing the compiler that this is an
134 /// equivalence relation rather than a partial equivalence relation. Note that
135 /// the `derive` strategy requires all fields are `PartialEq`, which isn't
136 /// always desired.
137 ///
138 /// ## How can I implement `Eq`?
139 ///
140 /// If you cannot use the `derive` strategy, specify that your type implements
141 /// `Eq`, which has no methods:
142 ///
143 /// ```
144 /// enum BookFormat { Paperback, Hardback, Ebook }
145 /// struct Book {
146 ///     isbn: i32,
147 ///     format: BookFormat,
148 /// }
149 /// impl PartialEq for Book {
150 ///     fn eq(&self, other: &Book) -> bool {
151 ///         self.isbn == other.isbn
152 ///     }
153 /// }
154 /// impl Eq for Book {}
155 /// ```
156 #[stable(feature = "rust1", since = "1.0.0")]
157 pub trait Eq: PartialEq<Self> {
158     // FIXME #13101: this method is used solely by #[deriving] to
159     // assert that every component of a type implements #[deriving]
160     // itself, the current deriving infrastructure means doing this
161     // assertion without using a method on this trait is nearly
162     // impossible.
163     //
164     // This should never be implemented by hand.
165     #[doc(hidden)]
166     #[inline(always)]
167     #[stable(feature = "rust1", since = "1.0.0")]
168     fn assert_receiver_is_total_eq(&self) {}
169 }
170
171 /// An `Ordering` is the result of a comparison between two values.
172 ///
173 /// # Examples
174 ///
175 /// ```
176 /// use std::cmp::Ordering;
177 ///
178 /// let result = 1.cmp(&2);
179 /// assert_eq!(Ordering::Less, result);
180 ///
181 /// let result = 1.cmp(&1);
182 /// assert_eq!(Ordering::Equal, result);
183 ///
184 /// let result = 2.cmp(&1);
185 /// assert_eq!(Ordering::Greater, result);
186 /// ```
187 #[derive(Clone, Copy, PartialEq, Debug, Hash)]
188 #[stable(feature = "rust1", since = "1.0.0")]
189 pub enum Ordering {
190     /// An ordering where a compared value is less [than another].
191     #[stable(feature = "rust1", since = "1.0.0")]
192     Less = -1,
193     /// An ordering where a compared value is equal [to another].
194     #[stable(feature = "rust1", since = "1.0.0")]
195     Equal = 0,
196     /// An ordering where a compared value is greater [than another].
197     #[stable(feature = "rust1", since = "1.0.0")]
198     Greater = 1,
199 }
200
201 impl Ordering {
202     /// Reverse the `Ordering`.
203     ///
204     /// * `Less` becomes `Greater`.
205     /// * `Greater` becomes `Less`.
206     /// * `Equal` becomes `Equal`.
207     ///
208     /// # Examples
209     ///
210     /// Basic behavior:
211     ///
212     /// ```
213     /// use std::cmp::Ordering;
214     ///
215     /// assert_eq!(Ordering::Less.reverse(), Ordering::Greater);
216     /// assert_eq!(Ordering::Equal.reverse(), Ordering::Equal);
217     /// assert_eq!(Ordering::Greater.reverse(), Ordering::Less);
218     /// ```
219     ///
220     /// This method can be used to reverse a comparison:
221     ///
222     /// ```
223     /// let mut data: &mut [_] = &mut [2, 10, 5, 8];
224     ///
225     /// // sort the array from largest to smallest.
226     /// data.sort_by(|a, b| a.cmp(b).reverse());
227     ///
228     /// let b: &mut [_] = &mut [10, 8, 5, 2];
229     /// assert!(data == b);
230     /// ```
231     #[inline]
232     #[stable(feature = "rust1", since = "1.0.0")]
233     pub fn reverse(self) -> Ordering {
234         match self {
235             Less => Greater,
236             Equal => Equal,
237             Greater => Less,
238         }
239     }
240 }
241
242 /// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
243 ///
244 /// An order is a total order if it is (for all `a`, `b` and `c`):
245 ///
246 /// - total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and
247 /// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
248 ///
249 /// ## Derivable
250 ///
251 /// This trait can be used with `#[derive]`. When `derive`d, it will produce a lexicographic
252 /// ordering based on the top-to-bottom declaration order of the struct's members.
253 ///
254 /// ## How can I implement `Ord`?
255 ///
256 /// `Ord` requires that the type also be `PartialOrd` and `Eq` (which requires `PartialEq`).
257 ///
258 /// Then you must define an implementation for `cmp()`. You may find it useful to use
259 /// `cmp()` on your type's fields.
260 ///
261 /// Here's an example where you want to sort people by height only, disregarding `id`
262 /// and `name`:
263 ///
264 /// ```
265 /// use std::cmp::Ordering;
266 ///
267 /// #[derive(Eq)]
268 /// struct Person {
269 ///     id: u32,
270 ///     name: String,
271 ///     height: u32,
272 /// }
273 ///
274 /// impl Ord for Person {
275 ///     fn cmp(&self, other: &Person) -> Ordering {
276 ///         self.height.cmp(&other.height)
277 ///     }
278 /// }
279 ///
280 /// impl PartialOrd for Person {
281 ///     fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
282 ///         Some(self.cmp(other))
283 ///     }
284 /// }
285 ///
286 /// impl PartialEq for Person {
287 ///     fn eq(&self, other: &Person) -> bool {
288 ///         self.height == other.height
289 ///     }
290 /// }
291 /// ```
292 #[stable(feature = "rust1", since = "1.0.0")]
293 pub trait Ord: Eq + PartialOrd<Self> {
294     /// This method returns an `Ordering` between `self` and `other`.
295     ///
296     /// By convention, `self.cmp(&other)` returns the ordering matching the expression
297     /// `self <operator> other` if true.
298     ///
299     /// # Examples
300     ///
301     /// ```
302     /// use std::cmp::Ordering;
303     ///
304     /// assert_eq!(5.cmp(&10), Ordering::Less);
305     /// assert_eq!(10.cmp(&5), Ordering::Greater);
306     /// assert_eq!(5.cmp(&5), Ordering::Equal);
307     /// ```
308     #[stable(feature = "rust1", since = "1.0.0")]
309     fn cmp(&self, other: &Self) -> Ordering;
310 }
311
312 #[stable(feature = "rust1", since = "1.0.0")]
313 impl Eq for Ordering {}
314
315 #[stable(feature = "rust1", since = "1.0.0")]
316 impl Ord for Ordering {
317     #[inline]
318     fn cmp(&self, other: &Ordering) -> Ordering {
319         (*self as i32).cmp(&(*other as i32))
320     }
321 }
322
323 #[stable(feature = "rust1", since = "1.0.0")]
324 impl PartialOrd for Ordering {
325     #[inline]
326     fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
327         (*self as i32).partial_cmp(&(*other as i32))
328     }
329 }
330
331 /// Trait for values that can be compared for a sort-order.
332 ///
333 /// The comparison must satisfy, for all `a`, `b` and `c`:
334 ///
335 /// - antisymmetry: if `a < b` then `!(a > b)`, as well as `a > b` implying `!(a < b)`; and
336 /// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
337 ///
338 /// Note that these requirements mean that the trait itself must be implemented symmetrically and
339 /// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T:
340 /// PartialOrd<V>`.
341 ///
342 /// ## Derivable
343 ///
344 /// This trait can be used with `#[derive]`. When `derive`d, it will produce a lexicographic
345 /// ordering based on the top-to-bottom declaration order of the struct's members.
346 ///
347 /// ## How can I implement `Ord`?
348 ///
349 /// PartialOrd only requires implementation of the `partial_cmp` method, with the others generated
350 /// from default implementations.
351 ///
352 /// However it remains possible to implement the others separately for types which do not have a
353 /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 ==
354 /// false` (cf. IEEE 754-2008 section 5.11).
355 ///
356 /// `PartialOrd` requires your type to be `PartialEq`.
357 ///
358 /// If your type is `Ord`, you can implement `partial_cmp()` by using `cmp()`:
359 ///
360 /// ```
361 /// use std::cmp::Ordering;
362 ///
363 /// #[derive(Eq)]
364 /// struct Person {
365 ///     id: u32,
366 ///     name: String,
367 ///     height: u32,
368 /// }
369 ///
370 /// impl PartialOrd for Person {
371 ///     fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
372 ///         Some(self.cmp(other))
373 ///     }
374 /// }
375 ///
376 /// impl Ord for Person {
377 ///     fn cmp(&self, other: &Person) -> Ordering {
378 ///         self.height.cmp(&other.height)
379 ///     }
380 /// }
381 ///
382 /// impl PartialEq for Person {
383 ///     fn eq(&self, other: &Person) -> bool {
384 ///         self.height == other.height
385 ///     }
386 /// }
387 /// ```
388 ///
389 /// You may also find it useful to use `partial_cmp()` on your type`s fields. Here
390 /// is an example of `Person` types who have a floating-point `height` field that
391 /// is the only field to be used for sorting:
392 ///
393 /// ```
394 /// use std::cmp::Ordering;
395 ///
396 /// struct Person {
397 ///     id: u32,
398 ///     name: String,
399 ///     height: f64,
400 /// }
401 ///
402 /// impl PartialOrd for Person {
403 ///     fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
404 ///         self.height.partial_cmp(&other.height)
405 ///     }
406 /// }
407 ///
408 /// impl PartialEq for Person {
409 ///     fn eq(&self, other: &Person) -> bool {
410 ///         self.height == other.height
411 ///     }
412 /// }
413 /// ```
414 ///
415 /// # Examples
416 ///
417 /// ```
418 /// let x : u32 = 0;
419 /// let y : u32 = 1;
420 ///
421 /// assert_eq!(x < y, true);
422 /// assert_eq!(x.lt(&y), true);
423 /// ```
424 #[lang = "ord"]
425 #[stable(feature = "rust1", since = "1.0.0")]
426 pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
427     /// This method returns an ordering between `self` and `other` values if one exists.
428     ///
429     /// # Examples
430     ///
431     /// ```
432     /// use std::cmp::Ordering;
433     ///
434     /// let result = 1.0.partial_cmp(&2.0);
435     /// assert_eq!(result, Some(Ordering::Less));
436     ///
437     /// let result = 1.0.partial_cmp(&1.0);
438     /// assert_eq!(result, Some(Ordering::Equal));
439     ///
440     /// let result = 2.0.partial_cmp(&1.0);
441     /// assert_eq!(result, Some(Ordering::Greater));
442     /// ```
443     ///
444     /// When comparison is impossible:
445     ///
446     /// ```
447     /// let result = std::f64::NAN.partial_cmp(&1.0);
448     /// assert_eq!(result, None);
449     /// ```
450     #[stable(feature = "rust1", since = "1.0.0")]
451     fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
452
453     /// This method tests less than (for `self` and `other`) and is used by the `<` operator.
454     ///
455     /// # Examples
456     ///
457     /// ```
458     /// let result = 1.0 < 2.0;
459     /// assert_eq!(result, true);
460     ///
461     /// let result = 2.0 < 1.0;
462     /// assert_eq!(result, false);
463     /// ```
464     #[inline]
465     #[stable(feature = "rust1", since = "1.0.0")]
466     fn lt(&self, other: &Rhs) -> bool {
467         match self.partial_cmp(other) {
468             Some(Less) => true,
469             _ => false,
470         }
471     }
472
473     /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
474     /// operator.
475     ///
476     /// # Examples
477     ///
478     /// ```
479     /// let result = 1.0 <= 2.0;
480     /// assert_eq!(result, true);
481     ///
482     /// let result = 2.0 <= 2.0;
483     /// assert_eq!(result, true);
484     /// ```
485     #[inline]
486     #[stable(feature = "rust1", since = "1.0.0")]
487     fn le(&self, other: &Rhs) -> bool {
488         match self.partial_cmp(other) {
489             Some(Less) | Some(Equal) => true,
490             _ => false,
491         }
492     }
493
494     /// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
495     ///
496     /// # Examples
497     ///
498     /// ```
499     /// let result = 1.0 > 2.0;
500     /// assert_eq!(result, false);
501     ///
502     /// let result = 2.0 > 2.0;
503     /// assert_eq!(result, false);
504     /// ```
505     #[inline]
506     #[stable(feature = "rust1", since = "1.0.0")]
507     fn gt(&self, other: &Rhs) -> bool {
508         match self.partial_cmp(other) {
509             Some(Greater) => true,
510             _ => false,
511         }
512     }
513
514     /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
515     /// operator.
516     ///
517     /// # Examples
518     ///
519     /// ```
520     /// let result = 2.0 >= 1.0;
521     /// assert_eq!(result, true);
522     ///
523     /// let result = 2.0 >= 2.0;
524     /// assert_eq!(result, true);
525     /// ```
526     #[inline]
527     #[stable(feature = "rust1", since = "1.0.0")]
528     fn ge(&self, other: &Rhs) -> bool {
529         match self.partial_cmp(other) {
530             Some(Greater) | Some(Equal) => true,
531             _ => false,
532         }
533     }
534 }
535
536 /// Compare and return the minimum of two values.
537 ///
538 /// Returns the first argument if the comparison determines them to be equal.
539 ///
540 /// # Examples
541 ///
542 /// ```
543 /// use std::cmp;
544 ///
545 /// assert_eq!(1, cmp::min(1, 2));
546 /// assert_eq!(2, cmp::min(2, 2));
547 /// ```
548 #[inline]
549 #[stable(feature = "rust1", since = "1.0.0")]
550 pub fn min<T: Ord>(v1: T, v2: T) -> T {
551     if v1 <= v2 { v1 } else { v2 }
552 }
553
554 /// Compare and return the maximum of two values.
555 ///
556 /// Returns the second argument if the comparison determines them to be equal.
557 ///
558 /// # Examples
559 ///
560 /// ```
561 /// use std::cmp;
562 ///
563 /// assert_eq!(2, cmp::max(1, 2));
564 /// assert_eq!(2, cmp::max(2, 2));
565 /// ```
566 #[inline]
567 #[stable(feature = "rust1", since = "1.0.0")]
568 pub fn max<T: Ord>(v1: T, v2: T) -> T {
569     if v2 >= v1 { v2 } else { v1 }
570 }
571
572 // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
573 mod impls {
574     use cmp::{PartialOrd, Ord, PartialEq, Eq, Ordering};
575     use cmp::Ordering::{Less, Greater, Equal};
576     use marker::Sized;
577     use option::Option;
578     use option::Option::{Some, None};
579
580     macro_rules! partial_eq_impl {
581         ($($t:ty)*) => ($(
582             #[stable(feature = "rust1", since = "1.0.0")]
583             impl PartialEq for $t {
584                 #[inline]
585                 fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
586                 #[inline]
587                 fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
588             }
589         )*)
590     }
591
592     #[stable(feature = "rust1", since = "1.0.0")]
593     impl PartialEq for () {
594         #[inline]
595         fn eq(&self, _other: &()) -> bool { true }
596         #[inline]
597         fn ne(&self, _other: &()) -> bool { false }
598     }
599
600     partial_eq_impl! {
601         bool char usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64
602     }
603
604     macro_rules! eq_impl {
605         ($($t:ty)*) => ($(
606             #[stable(feature = "rust1", since = "1.0.0")]
607             impl Eq for $t {}
608         )*)
609     }
610
611     eq_impl! { () bool char usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
612
613     macro_rules! partial_ord_impl {
614         ($($t:ty)*) => ($(
615             #[stable(feature = "rust1", since = "1.0.0")]
616             impl PartialOrd for $t {
617                 #[inline]
618                 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
619                     match (self <= other, self >= other) {
620                         (false, false) => None,
621                         (false, true) => Some(Greater),
622                         (true, false) => Some(Less),
623                         (true, true) => Some(Equal),
624                     }
625                 }
626                 #[inline]
627                 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
628                 #[inline]
629                 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
630                 #[inline]
631                 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
632                 #[inline]
633                 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
634             }
635         )*)
636     }
637
638     #[stable(feature = "rust1", since = "1.0.0")]
639     impl PartialOrd for () {
640         #[inline]
641         fn partial_cmp(&self, _: &()) -> Option<Ordering> {
642             Some(Equal)
643         }
644     }
645
646     #[stable(feature = "rust1", since = "1.0.0")]
647     impl PartialOrd for bool {
648         #[inline]
649         fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
650             (*self as u8).partial_cmp(&(*other as u8))
651         }
652     }
653
654     partial_ord_impl! { f32 f64 }
655
656     macro_rules! ord_impl {
657         ($($t:ty)*) => ($(
658             #[stable(feature = "rust1", since = "1.0.0")]
659             impl PartialOrd for $t {
660                 #[inline]
661                 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
662                     Some(self.cmp(other))
663                 }
664                 #[inline]
665                 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
666                 #[inline]
667                 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
668                 #[inline]
669                 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
670                 #[inline]
671                 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
672             }
673
674             #[stable(feature = "rust1", since = "1.0.0")]
675             impl Ord for $t {
676                 #[inline]
677                 fn cmp(&self, other: &$t) -> Ordering {
678                     if *self == *other { Equal }
679                     else if *self < *other { Less }
680                     else { Greater }
681                 }
682             }
683         )*)
684     }
685
686     #[stable(feature = "rust1", since = "1.0.0")]
687     impl Ord for () {
688         #[inline]
689         fn cmp(&self, _other: &()) -> Ordering { Equal }
690     }
691
692     #[stable(feature = "rust1", since = "1.0.0")]
693     impl Ord for bool {
694         #[inline]
695         fn cmp(&self, other: &bool) -> Ordering {
696             (*self as u8).cmp(&(*other as u8))
697         }
698     }
699
700     ord_impl! { char usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
701
702     #[unstable(feature = "never_type", issue = "35121")]
703     impl PartialEq for ! {
704         fn eq(&self, _: &!) -> bool {
705             *self
706         }
707     }
708
709     #[unstable(feature = "never_type", issue = "35121")]
710     impl Eq for ! {}
711
712     #[unstable(feature = "never_type", issue = "35121")]
713     impl PartialOrd for ! {
714         fn partial_cmp(&self, _: &!) -> Option<Ordering> {
715             *self
716         }
717     }
718
719     #[unstable(feature = "never_type", issue = "35121")]
720     impl Ord for ! {
721         fn cmp(&self, _: &!) -> Ordering {
722             *self
723         }
724     }
725
726     // & pointers
727
728     #[stable(feature = "rust1", since = "1.0.0")]
729     impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a A where A: PartialEq<B> {
730         #[inline]
731         fn eq(&self, other: & &'b B) -> bool { PartialEq::eq(*self, *other) }
732         #[inline]
733         fn ne(&self, other: & &'b B) -> bool { PartialEq::ne(*self, *other) }
734     }
735     #[stable(feature = "rust1", since = "1.0.0")]
736     impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b B> for &'a A where A: PartialOrd<B> {
737         #[inline]
738         fn partial_cmp(&self, other: &&'b B) -> Option<Ordering> {
739             PartialOrd::partial_cmp(*self, *other)
740         }
741         #[inline]
742         fn lt(&self, other: & &'b B) -> bool { PartialOrd::lt(*self, *other) }
743         #[inline]
744         fn le(&self, other: & &'b B) -> bool { PartialOrd::le(*self, *other) }
745         #[inline]
746         fn ge(&self, other: & &'b B) -> bool { PartialOrd::ge(*self, *other) }
747         #[inline]
748         fn gt(&self, other: & &'b B) -> bool { PartialOrd::gt(*self, *other) }
749     }
750     #[stable(feature = "rust1", since = "1.0.0")]
751     impl<'a, A: ?Sized> Ord for &'a A where A: Ord {
752         #[inline]
753         fn cmp(&self, other: & &'a A) -> Ordering { Ord::cmp(*self, *other) }
754     }
755     #[stable(feature = "rust1", since = "1.0.0")]
756     impl<'a, A: ?Sized> Eq for &'a A where A: Eq {}
757
758     // &mut pointers
759
760     #[stable(feature = "rust1", since = "1.0.0")]
761     impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> {
762         #[inline]
763         fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
764         #[inline]
765         fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
766     }
767     #[stable(feature = "rust1", since = "1.0.0")]
768     impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b mut B> for &'a mut A where A: PartialOrd<B> {
769         #[inline]
770         fn partial_cmp(&self, other: &&'b mut B) -> Option<Ordering> {
771             PartialOrd::partial_cmp(*self, *other)
772         }
773         #[inline]
774         fn lt(&self, other: &&'b mut B) -> bool { PartialOrd::lt(*self, *other) }
775         #[inline]
776         fn le(&self, other: &&'b mut B) -> bool { PartialOrd::le(*self, *other) }
777         #[inline]
778         fn ge(&self, other: &&'b mut B) -> bool { PartialOrd::ge(*self, *other) }
779         #[inline]
780         fn gt(&self, other: &&'b mut B) -> bool { PartialOrd::gt(*self, *other) }
781     }
782     #[stable(feature = "rust1", since = "1.0.0")]
783     impl<'a, A: ?Sized> Ord for &'a mut A where A: Ord {
784         #[inline]
785         fn cmp(&self, other: &&'a mut A) -> Ordering { Ord::cmp(*self, *other) }
786     }
787     #[stable(feature = "rust1", since = "1.0.0")]
788     impl<'a, A: ?Sized> Eq for &'a mut A where A: Eq {}
789
790     #[stable(feature = "rust1", since = "1.0.0")]
791     impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> {
792         #[inline]
793         fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
794         #[inline]
795         fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
796     }
797
798     #[stable(feature = "rust1", since = "1.0.0")]
799     impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> {
800         #[inline]
801         fn eq(&self, other: &&'b B) -> bool { PartialEq::eq(*self, *other) }
802         #[inline]
803         fn ne(&self, other: &&'b B) -> bool { PartialEq::ne(*self, *other) }
804     }
805 }