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