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.
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.
11 //! Functionality for ordering and comparison.
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.
18 //! [`PartialOrd`]: trait.PartialOrd.html
19 //! [`PartialEq`]: trait.PartialEq.html
27 //! // these two lines are equivalent
28 //! assert_eq!(x < y, true);
29 //! assert_eq!(x.lt(&y), true);
31 //! // these two lines are also equivalent
32 //! assert_eq!(x == y, false);
33 //! assert_eq!(x.eq(&y), false);
36 #![stable(feature = "rust1", since = "1.0.0")]
38 use self::Ordering::*;
40 /// Trait for equality comparisons which are [partial equivalence
41 /// relations](http://en.wikipedia.org/wiki/Partial_equivalence_relation).
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`.
47 /// Formally, the equality must be (for all `a`, `b` and `c`):
49 /// - symmetric: `a == b` implies `b == a`; and
50 /// - transitive: `a == b` and `b == c` implies `a == c`.
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>`.
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.
63 /// ## How can I implement `PartialEq`?
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
70 /// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with
71 /// each other. It's easy to accidentally make them disagree by deriving some
72 /// of the traits and manually implementing others.
74 /// An example implementation for a domain in which two books are considered
75 /// the same book if their ISBN matches, even if the formats differ:
78 /// enum BookFormat { Paperback, Hardback, Ebook }
81 /// format: BookFormat,
84 /// impl PartialEq for Book {
85 /// fn eq(&self, other: &Book) -> bool {
86 /// self.isbn == other.isbn
90 /// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
91 /// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
92 /// let b3 = Book { isbn: 10, format: BookFormat::Paperback };
94 /// assert!(b1 == b2);
95 /// assert!(b1 != b3);
104 /// assert_eq!(x == y, false);
105 /// assert_eq!(x.eq(&y), false);
108 #[stable(feature = "rust1", since = "1.0.0")]
111 #[rustc_on_unimplemented(
112 message="can't compare `{Self}` with `{Rhs}`",
113 label="no implementation for `{Self} == {Rhs}`",
115 pub trait PartialEq<Rhs: ?Sized = Self> {
116 /// This method tests for `self` and `other` values to be equal, and is used
119 #[stable(feature = "rust1", since = "1.0.0")]
120 fn eq(&self, other: &Rhs) -> bool;
122 /// This method tests for `!=`.
125 #[stable(feature = "rust1", since = "1.0.0")]
126 fn ne(&self, other: &Rhs) -> bool { !self.eq(other) }
129 /// Trait for equality comparisons which are [equivalence relations](
130 /// https://en.wikipedia.org/wiki/Equivalence_relation).
132 /// This means, that in addition to `a == b` and `a != b` being strict inverses, the equality must
133 /// be (for all `a`, `b` and `c`):
135 /// - reflexive: `a == a`;
136 /// - symmetric: `a == b` implies `b == a`; and
137 /// - transitive: `a == b` and `b == c` implies `a == c`.
139 /// This property cannot be checked by the compiler, and therefore `Eq` implies
140 /// `PartialEq`, and has no extra methods.
144 /// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has
145 /// no extra methods, it is only informing the compiler that this is an
146 /// equivalence relation rather than a partial equivalence relation. Note that
147 /// the `derive` strategy requires all fields are `Eq`, which isn't
150 /// ## How can I implement `Eq`?
152 /// If you cannot use the `derive` strategy, specify that your type implements
153 /// `Eq`, which has no methods:
156 /// enum BookFormat { Paperback, Hardback, Ebook }
159 /// format: BookFormat,
161 /// impl PartialEq for Book {
162 /// fn eq(&self, other: &Book) -> bool {
163 /// self.isbn == other.isbn
166 /// impl Eq for Book {}
170 #[stable(feature = "rust1", since = "1.0.0")]
171 pub trait Eq: PartialEq<Self> {
172 // this method is used solely by #[deriving] to assert
173 // that every component of a type implements #[deriving]
174 // itself, the current deriving infrastructure means doing this
175 // assertion without using a method on this trait is nearly
178 // This should never be implemented by hand.
181 #[stable(feature = "rust1", since = "1.0.0")]
182 fn assert_receiver_is_total_eq(&self) {}
185 // FIXME: this struct is used solely by #[derive] to
186 // assert that every component of a type implements Eq.
188 // This struct should never appear in user code.
190 #[allow(missing_debug_implementations)]
191 #[unstable(feature = "derive_eq",
192 reason = "deriving hack, should not be public",
194 pub struct AssertParamIsEq<T: Eq + ?Sized> { _field: ::marker::PhantomData<T> }
196 /// An `Ordering` is the result of a comparison between two values.
201 /// use std::cmp::Ordering;
203 /// let result = 1.cmp(&2);
204 /// assert_eq!(Ordering::Less, result);
206 /// let result = 1.cmp(&1);
207 /// assert_eq!(Ordering::Equal, result);
209 /// let result = 2.cmp(&1);
210 /// assert_eq!(Ordering::Greater, result);
212 #[derive(Clone, Copy, PartialEq, Debug, Hash)]
213 #[stable(feature = "rust1", since = "1.0.0")]
215 /// An ordering where a compared value is less [than another].
216 #[stable(feature = "rust1", since = "1.0.0")]
218 /// An ordering where a compared value is equal [to another].
219 #[stable(feature = "rust1", since = "1.0.0")]
221 /// An ordering where a compared value is greater [than another].
222 #[stable(feature = "rust1", since = "1.0.0")]
227 /// Reverses the `Ordering`.
229 /// * `Less` becomes `Greater`.
230 /// * `Greater` becomes `Less`.
231 /// * `Equal` becomes `Equal`.
238 /// use std::cmp::Ordering;
240 /// assert_eq!(Ordering::Less.reverse(), Ordering::Greater);
241 /// assert_eq!(Ordering::Equal.reverse(), Ordering::Equal);
242 /// assert_eq!(Ordering::Greater.reverse(), Ordering::Less);
245 /// This method can be used to reverse a comparison:
248 /// let mut data: &mut [_] = &mut [2, 10, 5, 8];
250 /// // sort the array from largest to smallest.
251 /// data.sort_by(|a, b| a.cmp(b).reverse());
253 /// let b: &mut [_] = &mut [10, 8, 5, 2];
254 /// assert!(data == b);
257 #[stable(feature = "rust1", since = "1.0.0")]
258 pub fn reverse(self) -> Ordering {
266 /// Chains two orderings.
268 /// Returns `self` when it's not `Equal`. Otherwise returns `other`.
272 /// use std::cmp::Ordering;
274 /// let result = Ordering::Equal.then(Ordering::Less);
275 /// assert_eq!(result, Ordering::Less);
277 /// let result = Ordering::Less.then(Ordering::Equal);
278 /// assert_eq!(result, Ordering::Less);
280 /// let result = Ordering::Less.then(Ordering::Greater);
281 /// assert_eq!(result, Ordering::Less);
283 /// let result = Ordering::Equal.then(Ordering::Equal);
284 /// assert_eq!(result, Ordering::Equal);
286 /// let x: (i64, i64, i64) = (1, 2, 7);
287 /// let y: (i64, i64, i64) = (1, 5, 3);
288 /// let result = x.0.cmp(&y.0).then(x.1.cmp(&y.1)).then(x.2.cmp(&y.2));
290 /// assert_eq!(result, Ordering::Less);
293 #[stable(feature = "ordering_chaining", since = "1.17.0")]
294 pub fn then(self, other: Ordering) -> Ordering {
301 /// Chains the ordering with the given function.
303 /// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
309 /// use std::cmp::Ordering;
311 /// let result = Ordering::Equal.then_with(|| Ordering::Less);
312 /// assert_eq!(result, Ordering::Less);
314 /// let result = Ordering::Less.then_with(|| Ordering::Equal);
315 /// assert_eq!(result, Ordering::Less);
317 /// let result = Ordering::Less.then_with(|| Ordering::Greater);
318 /// assert_eq!(result, Ordering::Less);
320 /// let result = Ordering::Equal.then_with(|| Ordering::Equal);
321 /// assert_eq!(result, Ordering::Equal);
323 /// let x: (i64, i64, i64) = (1, 2, 7);
324 /// let y: (i64, i64, i64) = (1, 5, 3);
325 /// let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));
327 /// assert_eq!(result, Ordering::Less);
330 #[stable(feature = "ordering_chaining", since = "1.17.0")]
331 pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
339 /// A helper struct for reverse ordering.
341 /// This struct is a helper to be used with functions like `Vec::sort_by_key` and
342 /// can be used to reverse order a part of a key.
347 /// use std::cmp::Reverse;
349 /// let mut v = vec![1, 2, 3, 4, 5, 6];
350 /// v.sort_by_key(|&num| (num > 3, Reverse(num)));
351 /// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
353 #[derive(PartialEq, Eq, Debug, Copy, Clone, Default, Hash)]
354 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
355 pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
357 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
358 impl<T: PartialOrd> PartialOrd for Reverse<T> {
360 fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
361 other.0.partial_cmp(&self.0)
365 fn lt(&self, other: &Self) -> bool { other.0 < self.0 }
367 fn le(&self, other: &Self) -> bool { other.0 <= self.0 }
369 fn ge(&self, other: &Self) -> bool { other.0 >= self.0 }
371 fn gt(&self, other: &Self) -> bool { other.0 > self.0 }
374 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
375 impl<T: Ord> Ord for Reverse<T> {
377 fn cmp(&self, other: &Reverse<T>) -> Ordering {
382 /// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
384 /// An order is a total order if it is (for all `a`, `b` and `c`):
386 /// - total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and
387 /// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
391 /// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
392 /// lexicographic ordering based on the top-to-bottom declaration order of the struct's members.
393 /// When `derive`d on enums, variants are ordered by their top-to-bottom declaration order.
395 /// ## How can I implement `Ord`?
397 /// `Ord` requires that the type also be `PartialOrd` and `Eq` (which requires `PartialEq`).
399 /// Then you must define an implementation for `cmp()`. You may find it useful to use
400 /// `cmp()` on your type's fields.
402 /// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with each other. It's
403 /// easy to accidentally make them disagree by deriving some of the traits and manually
404 /// implementing others.
406 /// Here's an example where you want to sort people by height only, disregarding `id`
410 /// use std::cmp::Ordering;
419 /// impl Ord for Person {
420 /// fn cmp(&self, other: &Person) -> Ordering {
421 /// self.height.cmp(&other.height)
425 /// impl PartialOrd for Person {
426 /// fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
427 /// Some(self.cmp(other))
431 /// impl PartialEq for Person {
432 /// fn eq(&self, other: &Person) -> bool {
433 /// self.height == other.height
442 #[stable(feature = "rust1", since = "1.0.0")]
443 pub trait Ord: Eq + PartialOrd<Self> {
444 /// This method returns an `Ordering` between `self` and `other`.
446 /// By convention, `self.cmp(&other)` returns the ordering matching the expression
447 /// `self <operator> other` if true.
452 /// use std::cmp::Ordering;
454 /// assert_eq!(5.cmp(&10), Ordering::Less);
455 /// assert_eq!(10.cmp(&5), Ordering::Greater);
456 /// assert_eq!(5.cmp(&5), Ordering::Equal);
458 #[stable(feature = "rust1", since = "1.0.0")]
459 fn cmp(&self, other: &Self) -> Ordering;
461 /// Compares and returns the maximum of two values.
463 /// Returns the second argument if the comparison determines them to be equal.
468 /// assert_eq!(2, 1.max(2));
469 /// assert_eq!(2, 2.max(2));
471 #[stable(feature = "ord_max_min", since = "1.21.0")]
473 fn max(self, other: Self) -> Self
475 if other >= self { other } else { self }
478 /// Compares and returns the minimum of two values.
480 /// Returns the first argument if the comparison determines them to be equal.
485 /// assert_eq!(1, 1.min(2));
486 /// assert_eq!(2, 2.min(2));
488 #[stable(feature = "ord_max_min", since = "1.21.0")]
490 fn min(self, other: Self) -> Self
492 if self <= other { self } else { other }
496 #[stable(feature = "rust1", since = "1.0.0")]
497 impl Eq for Ordering {}
499 #[stable(feature = "rust1", since = "1.0.0")]
500 impl Ord for Ordering {
502 fn cmp(&self, other: &Ordering) -> Ordering {
503 (*self as i32).cmp(&(*other as i32))
507 #[stable(feature = "rust1", since = "1.0.0")]
508 impl PartialOrd for Ordering {
510 fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
511 (*self as i32).partial_cmp(&(*other as i32))
515 /// Trait for values that can be compared for a sort-order.
517 /// The comparison must satisfy, for all `a`, `b` and `c`:
519 /// - antisymmetry: if `a < b` then `!(a > b)`, as well as `a > b` implying `!(a < b)`; and
520 /// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
522 /// Note that these requirements mean that the trait itself must be implemented symmetrically and
523 /// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T:
528 /// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
529 /// lexicographic ordering based on the top-to-bottom declaration order of the struct's members.
530 /// When `derive`d on enums, variants are ordered by their top-to-bottom declaration order.
532 /// ## How can I implement `PartialOrd`?
534 /// `PartialOrd` only requires implementation of the `partial_cmp` method, with the others
535 /// generated from default implementations.
537 /// However it remains possible to implement the others separately for types which do not have a
538 /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 ==
539 /// false` (cf. IEEE 754-2008 section 5.11).
541 /// `PartialOrd` requires your type to be `PartialEq`.
543 /// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with each other. It's
544 /// easy to accidentally make them disagree by deriving some of the traits and manually
545 /// implementing others.
547 /// If your type is `Ord`, you can implement `partial_cmp()` by using `cmp()`:
550 /// use std::cmp::Ordering;
559 /// impl PartialOrd for Person {
560 /// fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
561 /// Some(self.cmp(other))
565 /// impl Ord for Person {
566 /// fn cmp(&self, other: &Person) -> Ordering {
567 /// self.height.cmp(&other.height)
571 /// impl PartialEq for Person {
572 /// fn eq(&self, other: &Person) -> bool {
573 /// self.height == other.height
578 /// You may also find it useful to use `partial_cmp()` on your type's fields. Here
579 /// is an example of `Person` types who have a floating-point `height` field that
580 /// is the only field to be used for sorting:
583 /// use std::cmp::Ordering;
591 /// impl PartialOrd for Person {
592 /// fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
593 /// self.height.partial_cmp(&other.height)
597 /// impl PartialEq for Person {
598 /// fn eq(&self, other: &Person) -> bool {
599 /// self.height == other.height
610 /// assert_eq!(x < y, true);
611 /// assert_eq!(x.lt(&y), true);
613 #[lang = "partial_ord"]
614 #[stable(feature = "rust1", since = "1.0.0")]
619 #[rustc_on_unimplemented(
620 message="can't compare `{Self}` with `{Rhs}`",
621 label="no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
623 pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
624 /// This method returns an ordering between `self` and `other` values if one exists.
629 /// use std::cmp::Ordering;
631 /// let result = 1.0.partial_cmp(&2.0);
632 /// assert_eq!(result, Some(Ordering::Less));
634 /// let result = 1.0.partial_cmp(&1.0);
635 /// assert_eq!(result, Some(Ordering::Equal));
637 /// let result = 2.0.partial_cmp(&1.0);
638 /// assert_eq!(result, Some(Ordering::Greater));
641 /// When comparison is impossible:
644 /// let result = std::f64::NAN.partial_cmp(&1.0);
645 /// assert_eq!(result, None);
648 #[stable(feature = "rust1", since = "1.0.0")]
649 fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
651 /// This method tests less than (for `self` and `other`) and is used by the `<` operator.
656 /// let result = 1.0 < 2.0;
657 /// assert_eq!(result, true);
659 /// let result = 2.0 < 1.0;
660 /// assert_eq!(result, false);
664 #[stable(feature = "rust1", since = "1.0.0")]
665 fn lt(&self, other: &Rhs) -> bool {
666 match self.partial_cmp(other) {
672 /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
678 /// let result = 1.0 <= 2.0;
679 /// assert_eq!(result, true);
681 /// let result = 2.0 <= 2.0;
682 /// assert_eq!(result, true);
686 #[stable(feature = "rust1", since = "1.0.0")]
687 fn le(&self, other: &Rhs) -> bool {
688 match self.partial_cmp(other) {
689 Some(Less) | Some(Equal) => true,
694 /// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
699 /// let result = 1.0 > 2.0;
700 /// assert_eq!(result, false);
702 /// let result = 2.0 > 2.0;
703 /// assert_eq!(result, false);
707 #[stable(feature = "rust1", since = "1.0.0")]
708 fn gt(&self, other: &Rhs) -> bool {
709 match self.partial_cmp(other) {
710 Some(Greater) => true,
715 /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
721 /// let result = 2.0 >= 1.0;
722 /// assert_eq!(result, true);
724 /// let result = 2.0 >= 2.0;
725 /// assert_eq!(result, true);
729 #[stable(feature = "rust1", since = "1.0.0")]
730 fn ge(&self, other: &Rhs) -> bool {
731 match self.partial_cmp(other) {
732 Some(Greater) | Some(Equal) => true,
738 /// Compares and returns the minimum of two values.
740 /// Returns the first argument if the comparison determines them to be equal.
742 /// Internally uses an alias to `Ord::min`.
749 /// assert_eq!(1, cmp::min(1, 2));
750 /// assert_eq!(2, cmp::min(2, 2));
753 #[stable(feature = "rust1", since = "1.0.0")]
754 pub fn min<T: Ord>(v1: T, v2: T) -> T {
758 /// Compares and returns the maximum of two values.
760 /// Returns the second argument if the comparison determines them to be equal.
762 /// Internally uses an alias to `Ord::max`.
769 /// assert_eq!(2, cmp::max(1, 2));
770 /// assert_eq!(2, cmp::max(2, 2));
773 #[stable(feature = "rust1", since = "1.0.0")]
774 pub fn max<T: Ord>(v1: T, v2: T) -> T {
778 // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
780 use cmp::Ordering::{self, Less, Greater, Equal};
782 macro_rules! partial_eq_impl {
784 #[stable(feature = "rust1", since = "1.0.0")]
785 impl PartialEq for $t {
787 fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
789 fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
794 #[stable(feature = "rust1", since = "1.0.0")]
795 impl PartialEq for () {
797 fn eq(&self, _other: &()) -> bool { true }
799 fn ne(&self, _other: &()) -> bool { false }
803 bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64
806 macro_rules! eq_impl {
808 #[stable(feature = "rust1", since = "1.0.0")]
813 eq_impl! { () bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
815 macro_rules! partial_ord_impl {
817 #[stable(feature = "rust1", since = "1.0.0")]
818 impl PartialOrd for $t {
820 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
821 match (self <= other, self >= other) {
822 (false, false) => None,
823 (false, true) => Some(Greater),
824 (true, false) => Some(Less),
825 (true, true) => Some(Equal),
829 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
831 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
833 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
835 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
840 #[stable(feature = "rust1", since = "1.0.0")]
841 impl PartialOrd for () {
843 fn partial_cmp(&self, _: &()) -> Option<Ordering> {
848 #[stable(feature = "rust1", since = "1.0.0")]
849 impl PartialOrd for bool {
851 fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
852 (*self as u8).partial_cmp(&(*other as u8))
856 partial_ord_impl! { f32 f64 }
858 macro_rules! ord_impl {
860 #[stable(feature = "rust1", since = "1.0.0")]
861 impl PartialOrd for $t {
863 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
864 Some(self.cmp(other))
867 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
869 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
871 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
873 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
876 #[stable(feature = "rust1", since = "1.0.0")]
879 fn cmp(&self, other: &$t) -> Ordering {
880 if *self == *other { Equal }
881 else if *self < *other { Less }
888 #[stable(feature = "rust1", since = "1.0.0")]
891 fn cmp(&self, _other: &()) -> Ordering { Equal }
894 #[stable(feature = "rust1", since = "1.0.0")]
897 fn cmp(&self, other: &bool) -> Ordering {
898 (*self as u8).cmp(&(*other as u8))
902 ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
904 #[unstable(feature = "never_type", issue = "35121")]
905 impl PartialEq for ! {
906 fn eq(&self, _: &!) -> bool {
911 #[unstable(feature = "never_type", issue = "35121")]
914 #[unstable(feature = "never_type", issue = "35121")]
915 impl PartialOrd for ! {
916 fn partial_cmp(&self, _: &!) -> Option<Ordering> {
921 #[unstable(feature = "never_type", issue = "35121")]
923 fn cmp(&self, _: &!) -> Ordering {
930 #[stable(feature = "rust1", since = "1.0.0")]
931 impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a A where A: PartialEq<B> {
933 fn eq(&self, other: & &'b B) -> bool { PartialEq::eq(*self, *other) }
935 fn ne(&self, other: & &'b B) -> bool { PartialEq::ne(*self, *other) }
937 #[stable(feature = "rust1", since = "1.0.0")]
938 impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b B> for &'a A where A: PartialOrd<B> {
940 fn partial_cmp(&self, other: &&'b B) -> Option<Ordering> {
941 PartialOrd::partial_cmp(*self, *other)
944 fn lt(&self, other: & &'b B) -> bool { PartialOrd::lt(*self, *other) }
946 fn le(&self, other: & &'b B) -> bool { PartialOrd::le(*self, *other) }
948 fn ge(&self, other: & &'b B) -> bool { PartialOrd::ge(*self, *other) }
950 fn gt(&self, other: & &'b B) -> bool { PartialOrd::gt(*self, *other) }
952 #[stable(feature = "rust1", since = "1.0.0")]
953 impl<'a, A: ?Sized> Ord for &'a A where A: Ord {
955 fn cmp(&self, other: & &'a A) -> Ordering { Ord::cmp(*self, *other) }
957 #[stable(feature = "rust1", since = "1.0.0")]
958 impl<'a, A: ?Sized> Eq for &'a A where A: Eq {}
962 #[stable(feature = "rust1", since = "1.0.0")]
963 impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> {
965 fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
967 fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
969 #[stable(feature = "rust1", since = "1.0.0")]
970 impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b mut B> for &'a mut A where A: PartialOrd<B> {
972 fn partial_cmp(&self, other: &&'b mut B) -> Option<Ordering> {
973 PartialOrd::partial_cmp(*self, *other)
976 fn lt(&self, other: &&'b mut B) -> bool { PartialOrd::lt(*self, *other) }
978 fn le(&self, other: &&'b mut B) -> bool { PartialOrd::le(*self, *other) }
980 fn ge(&self, other: &&'b mut B) -> bool { PartialOrd::ge(*self, *other) }
982 fn gt(&self, other: &&'b mut B) -> bool { PartialOrd::gt(*self, *other) }
984 #[stable(feature = "rust1", since = "1.0.0")]
985 impl<'a, A: ?Sized> Ord for &'a mut A where A: Ord {
987 fn cmp(&self, other: &&'a mut A) -> Ordering { Ord::cmp(*self, *other) }
989 #[stable(feature = "rust1", since = "1.0.0")]
990 impl<'a, A: ?Sized> Eq for &'a mut A where A: Eq {}
992 #[stable(feature = "rust1", since = "1.0.0")]
993 impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> {
995 fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
997 fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
1000 #[stable(feature = "rust1", since = "1.0.0")]
1001 impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> {
1003 fn eq(&self, other: &&'b B) -> bool { PartialEq::eq(*self, *other) }
1005 fn ne(&self, other: &&'b B) -> bool { PartialEq::ne(*self, *other) }