1
//! Utilities for comparing and ordering values.
2
//!
3
//! This module contains various tools for comparing and ordering values. In
4
//! summary:
5
//!
6
//! * [`PartialEq<Rhs>`] overloads the `==` and `!=` operators. In cases where
7
//!   `Rhs` (the right hand side's type) is `Self`, this trait corresponds to a
8
//!   partial equivalence relation.
9
//! * [`Eq`] indicates that the overloaded `==` operator corresponds to an
10
//!   equivalence relation.
11
//! * [`Ord`] and [`PartialOrd`] are traits that allow you to define total and
12
//!   partial orderings between values, respectively. Implementing them overloads
13
//!   the `<`, `<=`, `>`, and `>=` operators.
14
//! * [`Ordering`] is an enum returned by the main functions of [`Ord`] and
15
//!   [`PartialOrd`], and describes an ordering of two values (less, equal, or
16
//!   greater).
17
//! * [`Reverse`] is a struct that allows you to easily reverse an ordering.
18
//! * [`max`] and [`min`] are functions that build off of [`Ord`] and allow you
19
//!   to find the maximum or minimum of two values.
20
//!
21
//! For more details, see the respective documentation of each item in the list.
22
//!
23
//! [`max`]: Ord::max
24
//! [`min`]: Ord::min
25

            
26
#![stable(feature = "rust1", since = "1.0.0")]
27

            
28
#[cfg(not(feature = "ferrocene_certified"))]
29
mod bytewise;
30
#[cfg(not(feature = "ferrocene_certified"))]
31
pub(crate) use bytewise::BytewiseEq;
32

            
33
use self::Ordering::*;
34
use crate::marker::PointeeSized;
35
use crate::ops::ControlFlow;
36

            
37
/// Trait for comparisons using the equality operator.
38
///
39
/// Implementing this trait for types provides the `==` and `!=` operators for
40
/// those types.
41
///
42
/// `x.eq(y)` can also be written `x == y`, and `x.ne(y)` can be written `x != y`.
43
/// We use the easier-to-read infix notation in the remainder of this documentation.
44
///
45
/// This trait allows for comparisons using the equality operator, for types
46
/// that do not have a full equivalence relation. For example, in floating point
47
/// numbers `NaN != NaN`, so floating point types implement `PartialEq` but not
48
/// [`trait@Eq`]. Formally speaking, when `Rhs == Self`, this trait corresponds
49
/// to a [partial equivalence relation].
50
///
51
/// [partial equivalence relation]: https://en.wikipedia.org/wiki/Partial_equivalence_relation
52
///
53
/// Implementations must ensure that `eq` and `ne` are consistent with each other:
54
///
55
/// - `a != b` if and only if `!(a == b)`.
56
///
57
/// The default implementation of `ne` provides this consistency and is almost
58
/// always sufficient. It should not be overridden without very good reason.
59
///
60
/// If [`PartialOrd`] or [`Ord`] are also implemented for `Self` and `Rhs`, their methods must also
61
/// be consistent with `PartialEq` (see the documentation of those traits for the exact
62
/// requirements). It's easy to accidentally make them disagree by deriving some of the traits and
63
/// manually implementing others.
64
///
65
/// The equality relation `==` must satisfy the following conditions
66
/// (for all `a`, `b`, `c` of type `A`, `B`, `C`):
67
///
68
/// - **Symmetry**: if `A: PartialEq<B>` and `B: PartialEq<A>`, then **`a == b`
69
///   implies `b == a`**; and
70
///
71
/// - **Transitivity**: if `A: PartialEq<B>` and `B: PartialEq<C>` and `A:
72
///   PartialEq<C>`, then **`a == b` and `b == c` implies `a == c`**.
73
///   This must also work for longer chains, such as when `A: PartialEq<B>`, `B: PartialEq<C>`,
74
///   `C: PartialEq<D>`, and `A: PartialEq<D>` all exist.
75
///
76
/// Note that the `B: PartialEq<A>` (symmetric) and `A: PartialEq<C>`
77
/// (transitive) impls are not forced to exist, but these requirements apply
78
/// whenever they do exist.
79
///
80
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
81
/// specified, but users of the trait must ensure that such logic errors do *not* result in
82
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
83
/// methods.
84
///
85
/// ## Cross-crate considerations
86
///
87
/// Upholding the requirements stated above can become tricky when one crate implements `PartialEq`
88
/// for a type of another crate (i.e., to allow comparing one of its own types with a type from the
89
/// standard library). The recommendation is to never implement this trait for a foreign type. In
90
/// other words, such a crate should do `impl PartialEq<ForeignType> for LocalType`, but it should
91
/// *not* do `impl PartialEq<LocalType> for ForeignType`.
92
///
93
/// This avoids the problem of transitive chains that criss-cross crate boundaries: for all local
94
/// types `T`, you may assume that no other crate will add `impl`s that allow comparing `T == U`. In
95
/// other words, if other crates add `impl`s that allow building longer transitive chains `U1 == ...
96
/// == T == V1 == ...`, then all the types that appear to the right of `T` must be types that the
97
/// crate defining `T` already knows about. This rules out transitive chains where downstream crates
98
/// can add new `impl`s that "stitch together" comparisons of foreign types in ways that violate
99
/// transitivity.
100
///
101
/// Not having such foreign `impl`s also avoids forward compatibility issues where one crate adding
102
/// more `PartialEq` implementations can cause build failures in downstream crates.
103
///
104
/// ## Derivable
105
///
106
/// This trait can be used with `#[derive]`. When `derive`d on structs, two
107
/// instances are equal if all fields are equal, and not equal if any fields
108
/// are not equal. When `derive`d on enums, two instances are equal if they
109
/// are the same variant and all fields are equal.
110
///
111
/// ## How can I implement `PartialEq`?
112
///
113
/// An example implementation for a domain in which two books are considered
114
/// the same book if their ISBN matches, even if the formats differ:
115
///
116
/// ```
117
/// enum BookFormat {
118
///     Paperback,
119
///     Hardback,
120
///     Ebook,
121
/// }
122
///
123
/// struct Book {
124
///     isbn: i32,
125
///     format: BookFormat,
126
/// }
127
///
128
/// impl PartialEq for Book {
129
///     fn eq(&self, other: &Self) -> bool {
130
///         self.isbn == other.isbn
131
///     }
132
/// }
133
///
134
/// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
135
/// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
136
/// let b3 = Book { isbn: 10, format: BookFormat::Paperback };
137
///
138
/// assert!(b1 == b2);
139
/// assert!(b1 != b3);
140
/// ```
141
///
142
/// ## How can I compare two different types?
143
///
144
/// The type you can compare with is controlled by `PartialEq`'s type parameter.
145
/// For example, let's tweak our previous code a bit:
146
///
147
/// ```
148
/// // The derive implements <BookFormat> == <BookFormat> comparisons
149
/// #[derive(PartialEq)]
150
/// enum BookFormat {
151
///     Paperback,
152
///     Hardback,
153
///     Ebook,
154
/// }
155
///
156
/// struct Book {
157
///     isbn: i32,
158
///     format: BookFormat,
159
/// }
160
///
161
/// // Implement <Book> == <BookFormat> comparisons
162
/// impl PartialEq<BookFormat> for Book {
163
///     fn eq(&self, other: &BookFormat) -> bool {
164
///         self.format == *other
165
///     }
166
/// }
167
///
168
/// // Implement <BookFormat> == <Book> comparisons
169
/// impl PartialEq<Book> for BookFormat {
170
///     fn eq(&self, other: &Book) -> bool {
171
///         *self == other.format
172
///     }
173
/// }
174
///
175
/// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
176
///
177
/// assert!(b1 == BookFormat::Paperback);
178
/// assert!(BookFormat::Ebook != b1);
179
/// ```
180
///
181
/// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`,
182
/// we allow `BookFormat`s to be compared with `Book`s.
183
///
184
/// A comparison like the one above, which ignores some fields of the struct,
185
/// can be dangerous. It can easily lead to an unintended violation of the
186
/// requirements for a partial equivalence relation. For example, if we kept
187
/// the above implementation of `PartialEq<Book>` for `BookFormat` and added an
188
/// implementation of `PartialEq<Book>` for `Book` (either via a `#[derive]` or
189
/// via the manual implementation from the first example) then the result would
190
/// violate transitivity:
191
///
192
/// ```should_panic
193
/// #[derive(PartialEq)]
194
/// enum BookFormat {
195
///     Paperback,
196
///     Hardback,
197
///     Ebook,
198
/// }
199
///
200
/// #[derive(PartialEq)]
201
/// struct Book {
202
///     isbn: i32,
203
///     format: BookFormat,
204
/// }
205
///
206
/// impl PartialEq<BookFormat> for Book {
207
///     fn eq(&self, other: &BookFormat) -> bool {
208
///         self.format == *other
209
///     }
210
/// }
211
///
212
/// impl PartialEq<Book> for BookFormat {
213
///     fn eq(&self, other: &Book) -> bool {
214
///         *self == other.format
215
///     }
216
/// }
217
///
218
/// fn main() {
219
///     let b1 = Book { isbn: 1, format: BookFormat::Paperback };
220
///     let b2 = Book { isbn: 2, format: BookFormat::Paperback };
221
///
222
///     assert!(b1 == BookFormat::Paperback);
223
///     assert!(BookFormat::Paperback == b2);
224
///
225
///     // The following should hold by transitivity but doesn't.
226
///     assert!(b1 == b2); // <-- PANICS
227
/// }
228
/// ```
229
///
230
/// # Examples
231
///
232
/// ```
233
/// let x: u32 = 0;
234
/// let y: u32 = 1;
235
///
236
/// assert_eq!(x == y, false);
237
/// assert_eq!(x.eq(&y), false);
238
/// ```
239
///
240
/// [`eq`]: PartialEq::eq
241
/// [`ne`]: PartialEq::ne
242
#[lang = "eq"]
243
#[stable(feature = "rust1", since = "1.0.0")]
244
#[doc(alias = "==")]
245
#[doc(alias = "!=")]
246
#[rustc_on_unimplemented(
247
    message = "can't compare `{Self}` with `{Rhs}`",
248
    label = "no implementation for `{Self} == {Rhs}`",
249
    append_const_msg
250
)]
251
#[rustc_diagnostic_item = "PartialEq"]
252
#[const_trait]
253
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
254
pub trait PartialEq<Rhs: PointeeSized = Self>: PointeeSized {
255
    /// Tests for `self` and `other` values to be equal, and is used by `==`.
256
    #[must_use]
257
    #[stable(feature = "rust1", since = "1.0.0")]
258
    #[rustc_diagnostic_item = "cmp_partialeq_eq"]
259
    fn eq(&self, other: &Rhs) -> bool;
260

            
261
    /// Tests for `!=`. The default implementation is almost always sufficient,
262
    /// and should not be overridden without very good reason.
263
    #[inline]
264
    #[must_use]
265
    #[stable(feature = "rust1", since = "1.0.0")]
266
    #[rustc_diagnostic_item = "cmp_partialeq_ne"]
267
14677
    fn ne(&self, other: &Rhs) -> bool {
268
14677
        !self.eq(other)
269
14677
    }
270
}
271

            
272
/// Derive macro generating an impl of the trait [`PartialEq`].
273
/// The behavior of this macro is described in detail [here](PartialEq#derivable).
274
#[rustc_builtin_macro]
275
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
276
#[allow_internal_unstable(core_intrinsics, structural_match)]
277
pub macro PartialEq($item:item) {
278
    /* compiler built-in */
279
}
280

            
281
/// Trait for comparisons corresponding to [equivalence relations](
282
/// https://en.wikipedia.org/wiki/Equivalence_relation).
283
///
284
/// The primary difference to [`PartialEq`] is the additional requirement for reflexivity. A type
285
/// that implements [`PartialEq`] guarantees that for all `a`, `b` and `c`:
286
///
287
/// - symmetric: `a == b` implies `b == a` and `a != b` implies `!(a == b)`
288
/// - transitive: `a == b` and `b == c` implies `a == c`
289
///
290
/// `Eq`, which builds on top of [`PartialEq`] also implies:
291
///
292
/// - reflexive: `a == a`
293
///
294
/// This property cannot be checked by the compiler, and therefore `Eq` is a trait without methods.
295
///
296
/// Violating this property is a logic error. The behavior resulting from a logic error is not
297
/// specified, but users of the trait must ensure that such logic errors do *not* result in
298
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
299
/// methods.
300
///
301
/// Floating point types such as [`f32`] and [`f64`] implement only [`PartialEq`] but *not* `Eq`
302
/// because `NaN` != `NaN`.
303
///
304
/// ## Derivable
305
///
306
/// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has no extra methods, it
307
/// is only informing the compiler that this is an equivalence relation rather than a partial
308
/// equivalence relation. Note that the `derive` strategy requires all fields are `Eq`, which isn't
309
/// always desired.
310
///
311
/// ## How can I implement `Eq`?
312
///
313
/// If you cannot use the `derive` strategy, specify that your type implements `Eq`, which has no
314
/// extra methods:
315
///
316
/// ```
317
/// enum BookFormat {
318
///     Paperback,
319
///     Hardback,
320
///     Ebook,
321
/// }
322
///
323
/// struct Book {
324
///     isbn: i32,
325
///     format: BookFormat,
326
/// }
327
///
328
/// impl PartialEq for Book {
329
///     fn eq(&self, other: &Self) -> bool {
330
///         self.isbn == other.isbn
331
///     }
332
/// }
333
///
334
/// impl Eq for Book {}
335
/// ```
336
#[doc(alias = "==")]
337
#[doc(alias = "!=")]
338
#[stable(feature = "rust1", since = "1.0.0")]
339
#[rustc_diagnostic_item = "Eq"]
340
pub trait Eq: PartialEq<Self> + PointeeSized {
341
    // this method is used solely by `impl Eq or #[derive(Eq)]` to assert that every component of a
342
    // type implements `Eq` itself. The current deriving infrastructure means doing this assertion
343
    // without using a method on this trait is nearly impossible.
344
    //
345
    // This should never be implemented by hand.
346
    #[doc(hidden)]
347
    #[coverage(off)]
348
    #[inline]
349
    #[stable(feature = "rust1", since = "1.0.0")]
350
    fn assert_receiver_is_total_eq(&self) {}
351
}
352

            
353
/// Derive macro generating an impl of the trait [`Eq`].
354
#[rustc_builtin_macro]
355
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
356
#[allow_internal_unstable(core_intrinsics, derive_eq, structural_match)]
357
#[allow_internal_unstable(coverage_attribute)]
358
pub macro Eq($item:item) {
359
    /* compiler built-in */
360
}
361

            
362
// FIXME: this struct is used solely by #[derive] to
363
// assert that every component of a type implements Eq.
364
//
365
// This struct should never appear in user code.
366
#[doc(hidden)]
367
#[allow(missing_debug_implementations)]
368
#[unstable(feature = "derive_eq", reason = "deriving hack, should not be public", issue = "none")]
369
#[cfg(not(feature = "ferrocene_certified"))]
370
pub struct AssertParamIsEq<T: Eq + PointeeSized> {
371
    _field: crate::marker::PhantomData<T>,
372
}
373

            
374
/// An `Ordering` is the result of a comparison between two values.
375
///
376
/// # Examples
377
///
378
/// ```
379
/// use std::cmp::Ordering;
380
///
381
/// assert_eq!(1.cmp(&2), Ordering::Less);
382
///
383
/// assert_eq!(1.cmp(&1), Ordering::Equal);
384
///
385
/// assert_eq!(2.cmp(&1), Ordering::Greater);
386
/// ```
387
#[cfg_attr(
388
    not(feature = "ferrocene_certified"),
389
    derive(Clone, Copy, Eq, PartialOrd, Ord, Debug, Hash)
390
)]
391
#[cfg_attr(not(feature = "ferrocene_certified"), derive_const(PartialEq))]
392
#[stable(feature = "rust1", since = "1.0.0")]
393
// This is a lang item only so that `BinOp::Cmp` in MIR can return it.
394
// It has no special behavior, but does require that the three variants
395
// `Less`/`Equal`/`Greater` remain `-1_i8`/`0_i8`/`+1_i8` respectively.
396
#[lang = "Ordering"]
397
#[repr(i8)]
398
pub enum Ordering {
399
    /// An ordering where a compared value is less than another.
400
    #[stable(feature = "rust1", since = "1.0.0")]
401
    Less = -1,
402
    /// An ordering where a compared value is equal to another.
403
    #[stable(feature = "rust1", since = "1.0.0")]
404
    Equal = 0,
405
    /// An ordering where a compared value is greater than another.
406
    #[stable(feature = "rust1", since = "1.0.0")]
407
    Greater = 1,
408
}
409

            
410
impl Ordering {
411
    #[inline]
412
1552877
    const fn as_raw(self) -> i8 {
413
        // FIXME(const-hack): just use `PartialOrd` against `Equal` once that's const
414
1552877
        crate::intrinsics::discriminant_value(&self)
415
1552877
    }
416

            
417
    /// Returns `true` if the ordering is the `Equal` variant.
418
    ///
419
    /// # Examples
420
    ///
421
    /// ```
422
    /// use std::cmp::Ordering;
423
    ///
424
    /// assert_eq!(Ordering::Less.is_eq(), false);
425
    /// assert_eq!(Ordering::Equal.is_eq(), true);
426
    /// assert_eq!(Ordering::Greater.is_eq(), false);
427
    /// ```
428
    #[inline]
429
    #[must_use]
430
    #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
431
    #[stable(feature = "ordering_helpers", since = "1.53.0")]
432
    pub const fn is_eq(self) -> bool {
433
        // All the `is_*` methods are implemented as comparisons against zero
434
        // to follow how clang's libcxx implements their equivalents in
435
        // <https://github.com/llvm/llvm-project/blob/60486292b79885b7800b082754153202bef5b1f0/libcxx/include/__compare/is_eq.h#L23-L28>
436

            
437
        self.as_raw() == 0
438
    }
439

            
440
    /// Returns `true` if the ordering is not the `Equal` variant.
441
    ///
442
    /// # Examples
443
    ///
444
    /// ```
445
    /// use std::cmp::Ordering;
446
    ///
447
    /// assert_eq!(Ordering::Less.is_ne(), true);
448
    /// assert_eq!(Ordering::Equal.is_ne(), false);
449
    /// assert_eq!(Ordering::Greater.is_ne(), true);
450
    /// ```
451
    #[inline]
452
    #[must_use]
453
    #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
454
    #[stable(feature = "ordering_helpers", since = "1.53.0")]
455
    pub const fn is_ne(self) -> bool {
456
        self.as_raw() != 0
457
    }
458

            
459
    /// Returns `true` if the ordering is the `Less` variant.
460
    ///
461
    /// # Examples
462
    ///
463
    /// ```
464
    /// use std::cmp::Ordering;
465
    ///
466
    /// assert_eq!(Ordering::Less.is_lt(), true);
467
    /// assert_eq!(Ordering::Equal.is_lt(), false);
468
    /// assert_eq!(Ordering::Greater.is_lt(), false);
469
    /// ```
470
    #[inline]
471
    #[must_use]
472
    #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
473
    #[stable(feature = "ordering_helpers", since = "1.53.0")]
474
477568
    pub const fn is_lt(self) -> bool {
475
477568
        self.as_raw() < 0
476
477568
    }
477

            
478
    /// Returns `true` if the ordering is the `Greater` variant.
479
    ///
480
    /// # Examples
481
    ///
482
    /// ```
483
    /// use std::cmp::Ordering;
484
    ///
485
    /// assert_eq!(Ordering::Less.is_gt(), false);
486
    /// assert_eq!(Ordering::Equal.is_gt(), false);
487
    /// assert_eq!(Ordering::Greater.is_gt(), true);
488
    /// ```
489
    #[inline]
490
    #[must_use]
491
    #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
492
    #[stable(feature = "ordering_helpers", since = "1.53.0")]
493
89675
    pub const fn is_gt(self) -> bool {
494
89675
        self.as_raw() > 0
495
89675
    }
496

            
497
    /// Returns `true` if the ordering is either the `Less` or `Equal` variant.
498
    ///
499
    /// # Examples
500
    ///
501
    /// ```
502
    /// use std::cmp::Ordering;
503
    ///
504
    /// assert_eq!(Ordering::Less.is_le(), true);
505
    /// assert_eq!(Ordering::Equal.is_le(), true);
506
    /// assert_eq!(Ordering::Greater.is_le(), false);
507
    /// ```
508
    #[inline]
509
    #[must_use]
510
    #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
511
    #[stable(feature = "ordering_helpers", since = "1.53.0")]
512
70078
    pub const fn is_le(self) -> bool {
513
70078
        self.as_raw() <= 0
514
70078
    }
515

            
516
    /// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
517
    ///
518
    /// # Examples
519
    ///
520
    /// ```
521
    /// use std::cmp::Ordering;
522
    ///
523
    /// assert_eq!(Ordering::Less.is_ge(), false);
524
    /// assert_eq!(Ordering::Equal.is_ge(), true);
525
    /// assert_eq!(Ordering::Greater.is_ge(), true);
526
    /// ```
527
    #[inline]
528
    #[must_use]
529
    #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
530
    #[stable(feature = "ordering_helpers", since = "1.53.0")]
531
915556
    pub const fn is_ge(self) -> bool {
532
915556
        self.as_raw() >= 0
533
915556
    }
534

            
535
    /// Reverses the `Ordering`.
536
    ///
537
    /// * `Less` becomes `Greater`.
538
    /// * `Greater` becomes `Less`.
539
    /// * `Equal` becomes `Equal`.
540
    ///
541
    /// # Examples
542
    ///
543
    /// Basic behavior:
544
    ///
545
    /// ```
546
    /// use std::cmp::Ordering;
547
    ///
548
    /// assert_eq!(Ordering::Less.reverse(), Ordering::Greater);
549
    /// assert_eq!(Ordering::Equal.reverse(), Ordering::Equal);
550
    /// assert_eq!(Ordering::Greater.reverse(), Ordering::Less);
551
    /// ```
552
    ///
553
    /// This method can be used to reverse a comparison:
554
    ///
555
    /// ```
556
    /// let data: &mut [_] = &mut [2, 10, 5, 8];
557
    ///
558
    /// // sort the array from largest to smallest.
559
    /// data.sort_by(|a, b| a.cmp(b).reverse());
560
    ///
561
    /// let b: &mut [_] = &mut [10, 8, 5, 2];
562
    /// assert!(data == b);
563
    /// ```
564
    #[inline]
565
    #[must_use]
566
    #[rustc_const_stable(feature = "const_ordering", since = "1.48.0")]
567
    #[stable(feature = "rust1", since = "1.0.0")]
568
    pub const fn reverse(self) -> Ordering {
569
        match self {
570
            Less => Greater,
571
            Equal => Equal,
572
            Greater => Less,
573
        }
574
    }
575

            
576
    /// Chains two orderings.
577
    ///
578
    /// Returns `self` when it's not `Equal`. Otherwise returns `other`.
579
    ///
580
    /// # Examples
581
    ///
582
    /// ```
583
    /// use std::cmp::Ordering;
584
    ///
585
    /// let result = Ordering::Equal.then(Ordering::Less);
586
    /// assert_eq!(result, Ordering::Less);
587
    ///
588
    /// let result = Ordering::Less.then(Ordering::Equal);
589
    /// assert_eq!(result, Ordering::Less);
590
    ///
591
    /// let result = Ordering::Less.then(Ordering::Greater);
592
    /// assert_eq!(result, Ordering::Less);
593
    ///
594
    /// let result = Ordering::Equal.then(Ordering::Equal);
595
    /// assert_eq!(result, Ordering::Equal);
596
    ///
597
    /// let x: (i64, i64, i64) = (1, 2, 7);
598
    /// let y: (i64, i64, i64) = (1, 5, 3);
599
    /// let result = x.0.cmp(&y.0).then(x.1.cmp(&y.1)).then(x.2.cmp(&y.2));
600
    ///
601
    /// assert_eq!(result, Ordering::Less);
602
    /// ```
603
    #[inline]
604
    #[must_use]
605
    #[rustc_const_stable(feature = "const_ordering", since = "1.48.0")]
606
    #[stable(feature = "ordering_chaining", since = "1.17.0")]
607
    pub const fn then(self, other: Ordering) -> Ordering {
608
        match self {
609
            Equal => other,
610
            _ => self,
611
        }
612
    }
613

            
614
    /// Chains the ordering with the given function.
615
    ///
616
    /// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
617
    /// the result.
618
    ///
619
    /// # Examples
620
    ///
621
    /// ```
622
    /// use std::cmp::Ordering;
623
    ///
624
    /// let result = Ordering::Equal.then_with(|| Ordering::Less);
625
    /// assert_eq!(result, Ordering::Less);
626
    ///
627
    /// let result = Ordering::Less.then_with(|| Ordering::Equal);
628
    /// assert_eq!(result, Ordering::Less);
629
    ///
630
    /// let result = Ordering::Less.then_with(|| Ordering::Greater);
631
    /// assert_eq!(result, Ordering::Less);
632
    ///
633
    /// let result = Ordering::Equal.then_with(|| Ordering::Equal);
634
    /// assert_eq!(result, Ordering::Equal);
635
    ///
636
    /// let x: (i64, i64, i64) = (1, 2, 7);
637
    /// let y: (i64, i64, i64) = (1, 5, 3);
638
    /// let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));
639
    ///
640
    /// assert_eq!(result, Ordering::Less);
641
    /// ```
642
    #[inline]
643
    #[must_use]
644
    #[stable(feature = "ordering_chaining", since = "1.17.0")]
645
    #[cfg(not(feature = "ferrocene_certified"))]
646
    pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
647
        match self {
648
            Equal => f(),
649
            _ => self,
650
        }
651
    }
652
}
653

            
654
/// A helper struct for reverse ordering.
655
///
656
/// This struct is a helper to be used with functions like [`Vec::sort_by_key`] and
657
/// can be used to reverse order a part of a key.
658
///
659
/// [`Vec::sort_by_key`]: ../../std/vec/struct.Vec.html#method.sort_by_key
660
///
661
/// # Examples
662
///
663
/// ```
664
/// use std::cmp::Reverse;
665
///
666
/// let mut v = vec![1, 2, 3, 4, 5, 6];
667
/// v.sort_by_key(|&num| (num > 3, Reverse(num)));
668
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
669
/// ```
670
#[derive(PartialEq, Eq, Debug, Copy, Default, Hash)]
671
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
672
#[repr(transparent)]
673
#[cfg(not(feature = "ferrocene_certified"))]
674
pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
675

            
676
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
677
#[cfg(not(feature = "ferrocene_certified"))]
678
impl<T: PartialOrd> PartialOrd for Reverse<T> {
679
    #[inline]
680
    fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
681
        other.0.partial_cmp(&self.0)
682
    }
683

            
684
    #[inline]
685
    fn lt(&self, other: &Self) -> bool {
686
        other.0 < self.0
687
    }
688
    #[inline]
689
    fn le(&self, other: &Self) -> bool {
690
        other.0 <= self.0
691
    }
692
    #[inline]
693
    fn gt(&self, other: &Self) -> bool {
694
        other.0 > self.0
695
    }
696
    #[inline]
697
    fn ge(&self, other: &Self) -> bool {
698
        other.0 >= self.0
699
    }
700
}
701

            
702
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
703
#[cfg(not(feature = "ferrocene_certified"))]
704
impl<T: Ord> Ord for Reverse<T> {
705
    #[inline]
706
    fn cmp(&self, other: &Reverse<T>) -> Ordering {
707
        other.0.cmp(&self.0)
708
    }
709
}
710

            
711
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
712
#[cfg(not(feature = "ferrocene_certified"))]
713
impl<T: Clone> Clone for Reverse<T> {
714
    #[inline]
715
    fn clone(&self) -> Reverse<T> {
716
        Reverse(self.0.clone())
717
    }
718

            
719
    #[inline]
720
    fn clone_from(&mut self, source: &Self) {
721
        self.0.clone_from(&source.0)
722
    }
723
}
724

            
725
/// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
726
///
727
/// Implementations must be consistent with the [`PartialOrd`] implementation, and ensure `max`,
728
/// `min`, and `clamp` are consistent with `cmp`:
729
///
730
/// - `partial_cmp(a, b) == Some(cmp(a, b))`.
731
/// - `max(a, b) == max_by(a, b, cmp)` (ensured by the default implementation).
732
/// - `min(a, b) == min_by(a, b, cmp)` (ensured by the default implementation).
733
/// - For `a.clamp(min, max)`, see the [method docs](#method.clamp) (ensured by the default
734
///   implementation).
735
///
736
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
737
/// specified, but users of the trait must ensure that such logic errors do *not* result in
738
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
739
/// methods.
740
///
741
/// ## Corollaries
742
///
743
/// From the above and the requirements of `PartialOrd`, it follows that for all `a`, `b` and `c`:
744
///
745
/// - exactly one of `a < b`, `a == b` or `a > b` is true; and
746
/// - `<` is transitive: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and
747
///   `>`.
748
///
749
/// Mathematically speaking, the `<` operator defines a strict [weak order]. In cases where `==`
750
/// conforms to mathematical equality, it also defines a strict [total order].
751
///
752
/// [weak order]: https://en.wikipedia.org/wiki/Weak_ordering
753
/// [total order]: https://en.wikipedia.org/wiki/Total_order
754
///
755
/// ## Derivable
756
///
757
/// This trait can be used with `#[derive]`.
758
///
759
/// When `derive`d on structs, it will produce a
760
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering based on the
761
/// top-to-bottom declaration order of the struct's members.
762
///
763
/// When `derive`d on enums, variants are ordered primarily by their discriminants. Secondarily,
764
/// they are ordered by their fields. By default, the discriminant is smallest for variants at the
765
/// top, and largest for variants at the bottom. Here's an example:
766
///
767
/// ```
768
/// #[derive(PartialEq, Eq, PartialOrd, Ord)]
769
/// enum E {
770
///     Top,
771
///     Bottom,
772
/// }
773
///
774
/// assert!(E::Top < E::Bottom);
775
/// ```
776
///
777
/// However, manually setting the discriminants can override this default behavior:
778
///
779
/// ```
780
/// #[derive(PartialEq, Eq, PartialOrd, Ord)]
781
/// enum E {
782
///     Top = 2,
783
///     Bottom = 1,
784
/// }
785
///
786
/// assert!(E::Bottom < E::Top);
787
/// ```
788
///
789
/// ## Lexicographical comparison
790
///
791
/// Lexicographical comparison is an operation with the following properties:
792
///  - Two sequences are compared element by element.
793
///  - The first mismatching element defines which sequence is lexicographically less or greater
794
///    than the other.
795
///  - If one sequence is a prefix of another, the shorter sequence is lexicographically less than
796
///    the other.
797
///  - If two sequences have equivalent elements and are of the same length, then the sequences are
798
///    lexicographically equal.
799
///  - An empty sequence is lexicographically less than any non-empty sequence.
800
///  - Two empty sequences are lexicographically equal.
801
///
802
/// ## How can I implement `Ord`?
803
///
804
/// `Ord` requires that the type also be [`PartialOrd`], [`PartialEq`], and [`Eq`].
805
///
806
/// Because `Ord` implies a stronger ordering relationship than [`PartialOrd`], and both `Ord` and
807
/// [`PartialOrd`] must agree, you must choose how to implement `Ord` **first**. You can choose to
808
/// derive it, or implement it manually. If you derive it, you should derive all four traits. If you
809
/// implement it manually, you should manually implement all four traits, based on the
810
/// implementation of `Ord`.
811
///
812
/// Here's an example where you want to define the `Character` comparison by `health` and
813
/// `experience` only, disregarding the field `mana`:
814
///
815
/// ```
816
/// use std::cmp::Ordering;
817
///
818
/// struct Character {
819
///     health: u32,
820
///     experience: u32,
821
///     mana: f32,
822
/// }
823
///
824
/// impl Ord for Character {
825
///     fn cmp(&self, other: &Self) -> Ordering {
826
///         self.experience
827
///             .cmp(&other.experience)
828
///             .then(self.health.cmp(&other.health))
829
///     }
830
/// }
831
///
832
/// impl PartialOrd for Character {
833
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
834
///         Some(self.cmp(other))
835
///     }
836
/// }
837
///
838
/// impl PartialEq for Character {
839
///     fn eq(&self, other: &Self) -> bool {
840
///         self.health == other.health && self.experience == other.experience
841
///     }
842
/// }
843
///
844
/// impl Eq for Character {}
845
/// ```
846
///
847
/// If all you need is to `slice::sort` a type by a field value, it can be simpler to use
848
/// `slice::sort_by_key`.
849
///
850
/// ## Examples of incorrect `Ord` implementations
851
///
852
/// ```
853
/// use std::cmp::Ordering;
854
///
855
/// #[derive(Debug)]
856
/// struct Character {
857
///     health: f32,
858
/// }
859
///
860
/// impl Ord for Character {
861
///     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
862
///         if self.health < other.health {
863
///             Ordering::Less
864
///         } else if self.health > other.health {
865
///             Ordering::Greater
866
///         } else {
867
///             Ordering::Equal
868
///         }
869
///     }
870
/// }
871
///
872
/// impl PartialOrd for Character {
873
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
874
///         Some(self.cmp(other))
875
///     }
876
/// }
877
///
878
/// impl PartialEq for Character {
879
///     fn eq(&self, other: &Self) -> bool {
880
///         self.health == other.health
881
///     }
882
/// }
883
///
884
/// impl Eq for Character {}
885
///
886
/// let a = Character { health: 4.5 };
887
/// let b = Character { health: f32::NAN };
888
///
889
/// // Mistake: floating-point values do not form a total order and using the built-in comparison
890
/// // operands to implement `Ord` irregardless of that reality does not change it. Use
891
/// // `f32::total_cmp` if you need a total order for floating-point values.
892
///
893
/// // Reflexivity requirement of `Ord` is not given.
894
/// assert!(a == a);
895
/// assert!(b != b);
896
///
897
/// // Antisymmetry requirement of `Ord` is not given. Only one of a < c and c < a is allowed to be
898
/// // true, not both or neither.
899
/// assert_eq!((a < b) as u8 + (b < a) as u8, 0);
900
/// ```
901
///
902
/// ```
903
/// use std::cmp::Ordering;
904
///
905
/// #[derive(Debug)]
906
/// struct Character {
907
///     health: u32,
908
///     experience: u32,
909
/// }
910
///
911
/// impl PartialOrd for Character {
912
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
913
///         Some(self.cmp(other))
914
///     }
915
/// }
916
///
917
/// impl Ord for Character {
918
///     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
919
///         if self.health < 50 {
920
///             self.health.cmp(&other.health)
921
///         } else {
922
///             self.experience.cmp(&other.experience)
923
///         }
924
///     }
925
/// }
926
///
927
/// // For performance reasons implementing `PartialEq` this way is not the idiomatic way, but it
928
/// // ensures consistent behavior between `PartialEq`, `PartialOrd` and `Ord` in this example.
929
/// impl PartialEq for Character {
930
///     fn eq(&self, other: &Self) -> bool {
931
///         self.cmp(other) == Ordering::Equal
932
///     }
933
/// }
934
///
935
/// impl Eq for Character {}
936
///
937
/// let a = Character {
938
///     health: 3,
939
///     experience: 5,
940
/// };
941
/// let b = Character {
942
///     health: 10,
943
///     experience: 77,
944
/// };
945
/// let c = Character {
946
///     health: 143,
947
///     experience: 2,
948
/// };
949
///
950
/// // Mistake: The implementation of `Ord` compares different fields depending on the value of
951
/// // `self.health`, the resulting order is not total.
952
///
953
/// // Transitivity requirement of `Ord` is not given. If a is smaller than b and b is smaller than
954
/// // c, by transitive property a must also be smaller than c.
955
/// assert!(a < b && b < c && c < a);
956
///
957
/// // Antisymmetry requirement of `Ord` is not given. Only one of a < c and c < a is allowed to be
958
/// // true, not both or neither.
959
/// assert_eq!((a < c) as u8 + (c < a) as u8, 2);
960
/// ```
961
///
962
/// The documentation of [`PartialOrd`] contains further examples, for example it's wrong for
963
/// [`PartialOrd`] and [`PartialEq`] to disagree.
964
///
965
/// [`cmp`]: Ord::cmp
966
#[doc(alias = "<")]
967
#[doc(alias = ">")]
968
#[doc(alias = "<=")]
969
#[doc(alias = ">=")]
970
#[stable(feature = "rust1", since = "1.0.0")]
971
#[rustc_diagnostic_item = "Ord"]
972
pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
973
    /// This method returns an [`Ordering`] between `self` and `other`.
974
    ///
975
    /// By convention, `self.cmp(&other)` returns the ordering matching the expression
976
    /// `self <operator> other` if true.
977
    ///
978
    /// # Examples
979
    ///
980
    /// ```
981
    /// use std::cmp::Ordering;
982
    ///
983
    /// assert_eq!(5.cmp(&10), Ordering::Less);
984
    /// assert_eq!(10.cmp(&5), Ordering::Greater);
985
    /// assert_eq!(5.cmp(&5), Ordering::Equal);
986
    /// ```
987
    #[must_use]
988
    #[stable(feature = "rust1", since = "1.0.0")]
989
    #[rustc_diagnostic_item = "ord_cmp_method"]
990
    fn cmp(&self, other: &Self) -> Ordering;
991

            
992
    /// Compares and returns the maximum of two values.
993
    ///
994
    /// Returns the second argument if the comparison determines them to be equal.
995
    ///
996
    /// # Examples
997
    ///
998
    /// ```
999
    /// assert_eq!(1.max(2), 2);
    /// assert_eq!(2.max(2), 2);
    /// ```
    /// ```
    /// use std::cmp::Ordering;
    ///
    /// #[derive(Eq)]
    /// struct Equal(&'static str);
    ///
    /// impl PartialEq for Equal {
    ///     fn eq(&self, other: &Self) -> bool { true }
    /// }
    /// impl PartialOrd for Equal {
    ///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
    /// }
    /// impl Ord for Equal {
    ///     fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
    /// }
    ///
    /// assert_eq!(Equal("self").max(Equal("other")).0, "other");
    /// ```
    #[stable(feature = "ord_max_min", since = "1.21.0")]
    #[inline]
    #[must_use]
    #[rustc_diagnostic_item = "cmp_ord_max"]
600
    fn max(self, other: Self) -> Self
600
    where
600
        Self: Sized,
    {
600
        if other < self { self } else { other }
600
    }
    /// Compares and returns the minimum of two values.
    ///
    /// Returns the first argument if the comparison determines them to be equal.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(1.min(2), 1);
    /// assert_eq!(2.min(2), 2);
    /// ```
    /// ```
    /// use std::cmp::Ordering;
    ///
    /// #[derive(Eq)]
    /// struct Equal(&'static str);
    ///
    /// impl PartialEq for Equal {
    ///     fn eq(&self, other: &Self) -> bool { true }
    /// }
    /// impl PartialOrd for Equal {
    ///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
    /// }
    /// impl Ord for Equal {
    ///     fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
    /// }
    ///
    /// assert_eq!(Equal("self").min(Equal("other")).0, "self");
    /// ```
    #[stable(feature = "ord_max_min", since = "1.21.0")]
    #[inline]
    #[must_use]
    #[rustc_diagnostic_item = "cmp_ord_min"]
1332
    fn min(self, other: Self) -> Self
1332
    where
1332
        Self: Sized,
    {
1332
        if other < self { other } else { self }
1332
    }
    /// Restrict a value to a certain interval.
    ///
    /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
    /// less than `min`. Otherwise this returns `self`.
    ///
    /// # Panics
    ///
    /// Panics if `min > max`.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!((-3).clamp(-2, 1), -2);
    /// assert_eq!(0.clamp(-2, 1), 0);
    /// assert_eq!(2.clamp(-2, 1), 1);
    /// ```
    #[must_use]
    #[inline]
    #[stable(feature = "clamp", since = "1.50.0")]
    fn clamp(self, min: Self, max: Self) -> Self
    where
        Self: Sized,
    {
        assert!(min <= max);
        if self < min {
            min
        } else if self > max {
            max
        } else {
            self
        }
    }
}
/// Derive macro generating an impl of the trait [`Ord`].
/// The behavior of this macro is described in detail [here](Ord#derivable).
#[rustc_builtin_macro]
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow_internal_unstable(core_intrinsics)]
#[cfg(not(feature = "ferrocene_certified"))]
pub macro Ord($item:item) {
    /* compiler built-in */
}
/// Trait for types that form a [partial order](https://en.wikipedia.org/wiki/Partial_order).
///
/// The `lt`, `le`, `gt`, and `ge` methods of this trait can be called using the `<`, `<=`, `>`, and
/// `>=` operators, respectively.
///
/// This trait should **only** contain the comparison logic for a type **if one plans on only
/// implementing `PartialOrd` but not [`Ord`]**. Otherwise the comparison logic should be in [`Ord`]
/// and this trait implemented with `Some(self.cmp(other))`.
///
/// The methods of this trait must be consistent with each other and with those of [`PartialEq`].
/// The following conditions must hold:
///
/// 1. `a == b` if and only if `partial_cmp(a, b) == Some(Equal)`.
/// 2. `a < b` if and only if `partial_cmp(a, b) == Some(Less)`
/// 3. `a > b` if and only if `partial_cmp(a, b) == Some(Greater)`
/// 4. `a <= b` if and only if `a < b || a == b`
/// 5. `a >= b` if and only if `a > b || a == b`
/// 6. `a != b` if and only if `!(a == b)`.
///
/// Conditions 2–5 above are ensured by the default implementation. Condition 6 is already ensured
/// by [`PartialEq`].
///
/// If [`Ord`] is also implemented for `Self` and `Rhs`, it must also be consistent with
/// `partial_cmp` (see the documentation of that trait for the exact requirements). It's easy to
/// accidentally make them disagree by deriving some of the traits and manually implementing others.
///
/// The comparison relations must satisfy the following conditions (for all `a`, `b`, `c` of type
/// `A`, `B`, `C`):
///
/// - **Transitivity**: if `A: PartialOrd<B>` and `B: PartialOrd<C>` and `A: PartialOrd<C>`, then `a
///   < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`. This must also
///   work for longer chains, such as when `A: PartialOrd<B>`, `B: PartialOrd<C>`, `C:
///   PartialOrd<D>`, and `A: PartialOrd<D>` all exist.
/// - **Duality**: if `A: PartialOrd<B>` and `B: PartialOrd<A>`, then `a < b` if and only if `b >
///   a`.
///
/// Note that the `B: PartialOrd<A>` (dual) and `A: PartialOrd<C>` (transitive) impls are not forced
/// to exist, but these requirements apply whenever they do exist.
///
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
/// specified, but users of the trait must ensure that such logic errors do *not* result in
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
/// methods.
///
/// ## Cross-crate considerations
///
/// Upholding the requirements stated above can become tricky when one crate implements `PartialOrd`
/// for a type of another crate (i.e., to allow comparing one of its own types with a type from the
/// standard library). The recommendation is to never implement this trait for a foreign type. In
/// other words, such a crate should do `impl PartialOrd<ForeignType> for LocalType`, but it should
/// *not* do `impl PartialOrd<LocalType> for ForeignType`.
///
/// This avoids the problem of transitive chains that criss-cross crate boundaries: for all local
/// types `T`, you may assume that no other crate will add `impl`s that allow comparing `T < U`. In
/// other words, if other crates add `impl`s that allow building longer transitive chains `U1 < ...
/// < T < V1 < ...`, then all the types that appear to the right of `T` must be types that the crate
/// defining `T` already knows about. This rules out transitive chains where downstream crates can
/// add new `impl`s that "stitch together" comparisons of foreign types in ways that violate
/// transitivity.
///
/// Not having such foreign `impl`s also avoids forward compatibility issues where one crate adding
/// more `PartialOrd` implementations can cause build failures in downstream crates.
///
/// ## Corollaries
///
/// The following corollaries follow from the above requirements:
///
/// - irreflexivity of `<` and `>`: `!(a < a)`, `!(a > a)`
/// - transitivity of `>`: if `a > b` and `b > c` then `a > c`
/// - duality of `partial_cmp`: `partial_cmp(a, b) == partial_cmp(b, a).map(Ordering::reverse)`
///
/// ## Strict and non-strict partial orders
///
/// The `<` and `>` operators behave according to a *strict* partial order. However, `<=` and `>=`
/// do **not** behave according to a *non-strict* partial order. That is because mathematically, a
/// non-strict partial order would require reflexivity, i.e. `a <= a` would need to be true for
/// every `a`. This isn't always the case for types that implement `PartialOrd`, for example:
///
/// ```
/// let a = f64::sqrt(-1.0);
/// assert_eq!(a <= a, false);
/// ```
///
/// ## Derivable
///
/// This trait can be used with `#[derive]`.
///
/// When `derive`d on structs, it will produce a
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering based on the
/// top-to-bottom declaration order of the struct's members.
///
/// When `derive`d on enums, variants are primarily ordered by their discriminants. Secondarily,
/// they are ordered by their fields. By default, the discriminant is smallest for variants at the
/// top, and largest for variants at the bottom. Here's an example:
///
/// ```
/// #[derive(PartialEq, PartialOrd)]
/// enum E {
///     Top,
///     Bottom,
/// }
///
/// assert!(E::Top < E::Bottom);
/// ```
///
/// However, manually setting the discriminants can override this default behavior:
///
/// ```
/// #[derive(PartialEq, PartialOrd)]
/// enum E {
///     Top = 2,
///     Bottom = 1,
/// }
///
/// assert!(E::Bottom < E::Top);
/// ```
///
/// ## How can I implement `PartialOrd`?
///
/// `PartialOrd` only requires implementation of the [`partial_cmp`] method, with the others
/// generated from default implementations.
///
/// However it remains possible to implement the others separately for types which do not have a
/// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 == false`
/// (cf. IEEE 754-2008 section 5.11).
///
/// `PartialOrd` requires your type to be [`PartialEq`].
///
/// If your type is [`Ord`], you can implement [`partial_cmp`] by using [`cmp`]:
///
/// ```
/// use std::cmp::Ordering;
///
/// struct Person {
///     id: u32,
///     name: String,
///     height: u32,
/// }
///
/// impl PartialOrd for Person {
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
///         Some(self.cmp(other))
///     }
/// }
///
/// impl Ord for Person {
///     fn cmp(&self, other: &Self) -> Ordering {
///         self.height.cmp(&other.height)
///     }
/// }
///
/// impl PartialEq for Person {
///     fn eq(&self, other: &Self) -> bool {
///         self.height == other.height
///     }
/// }
///
/// impl Eq for Person {}
/// ```
///
/// You may also find it useful to use [`partial_cmp`] on your type's fields. Here is an example of
/// `Person` types who have a floating-point `height` field that is the only field to be used for
/// sorting:
///
/// ```
/// use std::cmp::Ordering;
///
/// struct Person {
///     id: u32,
///     name: String,
///     height: f64,
/// }
///
/// impl PartialOrd for Person {
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
///         self.height.partial_cmp(&other.height)
///     }
/// }
///
/// impl PartialEq for Person {
///     fn eq(&self, other: &Self) -> bool {
///         self.height == other.height
///     }
/// }
/// ```
///
/// ## Examples of incorrect `PartialOrd` implementations
///
/// ```
/// use std::cmp::Ordering;
///
/// #[derive(PartialEq, Debug)]
/// struct Character {
///     health: u32,
///     experience: u32,
/// }
///
/// impl PartialOrd for Character {
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
///         Some(self.health.cmp(&other.health))
///     }
/// }
///
/// let a = Character {
///     health: 10,
///     experience: 5,
/// };
/// let b = Character {
///     health: 10,
///     experience: 77,
/// };
///
/// // Mistake: `PartialEq` and `PartialOrd` disagree with each other.
///
/// assert_eq!(a.partial_cmp(&b).unwrap(), Ordering::Equal); // a == b according to `PartialOrd`.
/// assert_ne!(a, b); // a != b according to `PartialEq`.
/// ```
///
/// # Examples
///
/// ```
/// let x: u32 = 0;
/// let y: u32 = 1;
///
/// assert_eq!(x < y, true);
/// assert_eq!(x.lt(&y), true);
/// ```
///
/// [`partial_cmp`]: PartialOrd::partial_cmp
/// [`cmp`]: Ord::cmp
#[lang = "partial_ord"]
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(alias = ">")]
#[doc(alias = "<")]
#[doc(alias = "<=")]
#[doc(alias = ">=")]
#[rustc_on_unimplemented(
    message = "can't compare `{Self}` with `{Rhs}`",
    label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
    append_const_msg
)]
#[rustc_diagnostic_item = "PartialOrd"]
#[allow(multiple_supertrait_upcastable)] // FIXME(sized_hierarchy): remove this
pub trait PartialOrd<Rhs: PointeeSized = Self>: PartialEq<Rhs> + PointeeSized {
    /// This method returns an ordering between `self` and `other` values if one exists.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::cmp::Ordering;
    ///
    /// let result = 1.0.partial_cmp(&2.0);
    /// assert_eq!(result, Some(Ordering::Less));
    ///
    /// let result = 1.0.partial_cmp(&1.0);
    /// assert_eq!(result, Some(Ordering::Equal));
    ///
    /// let result = 2.0.partial_cmp(&1.0);
    /// assert_eq!(result, Some(Ordering::Greater));
    /// ```
    ///
    /// When comparison is impossible:
    ///
    /// ```
    /// let result = f64::NAN.partial_cmp(&1.0);
    /// assert_eq!(result, None);
    /// ```
    #[must_use]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_diagnostic_item = "cmp_partialord_cmp"]
    fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
    /// Tests less than (for `self` and `other`) and is used by the `<` operator.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(1.0 < 1.0, false);
    /// assert_eq!(1.0 < 2.0, true);
    /// assert_eq!(2.0 < 1.0, false);
    /// ```
    #[inline]
    #[must_use]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_diagnostic_item = "cmp_partialord_lt"]
4142
    fn lt(&self, other: &Rhs) -> bool {
4142
        self.partial_cmp(other).is_some_and(Ordering::is_lt)
4142
    }
    /// Tests less than or equal to (for `self` and `other`) and is used by the
    /// `<=` operator.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(1.0 <= 1.0, true);
    /// assert_eq!(1.0 <= 2.0, true);
    /// assert_eq!(2.0 <= 1.0, false);
    /// ```
    #[inline]
    #[must_use]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_diagnostic_item = "cmp_partialord_le"]
2
    fn le(&self, other: &Rhs) -> bool {
2
        self.partial_cmp(other).is_some_and(Ordering::is_le)
2
    }
    /// Tests greater than (for `self` and `other`) and is used by the `>`
    /// operator.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(1.0 > 1.0, false);
    /// assert_eq!(1.0 > 2.0, false);
    /// assert_eq!(2.0 > 1.0, true);
    /// ```
    #[inline]
    #[must_use]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_diagnostic_item = "cmp_partialord_gt"]
    fn gt(&self, other: &Rhs) -> bool {
        self.partial_cmp(other).is_some_and(Ordering::is_gt)
    }
    /// Tests greater than or equal to (for `self` and `other`) and is used by
    /// the `>=` operator.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(1.0 >= 1.0, true);
    /// assert_eq!(1.0 >= 2.0, false);
    /// assert_eq!(2.0 >= 1.0, true);
    /// ```
    #[inline]
    #[must_use]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_diagnostic_item = "cmp_partialord_ge"]
4144
    fn ge(&self, other: &Rhs) -> bool {
4144
        self.partial_cmp(other).is_some_and(Ordering::is_ge)
4144
    }
    /// If `self == other`, returns `ControlFlow::Continue(())`.
    /// Otherwise, returns `ControlFlow::Break(self < other)`.
    ///
    /// This is useful for chaining together calls when implementing a lexical
    /// `PartialOrd::lt`, as it allows types (like primitives) which can cheaply
    /// check `==` and `<` separately to do rather than needing to calculate
    /// (then optimize out) the three-way `Ordering` result.
    #[inline]
    // Added to improve the behaviour of tuples; not necessarily stabilization-track.
    #[unstable(feature = "partial_ord_chaining_methods", issue = "none")]
    #[doc(hidden)]
1
    fn __chaining_lt(&self, other: &Rhs) -> ControlFlow<bool> {
1
        default_chaining_impl(self, other, Ordering::is_lt)
1
    }
    /// Same as `__chaining_lt`, but for `<=` instead of `<`.
    #[inline]
    #[unstable(feature = "partial_ord_chaining_methods", issue = "none")]
    #[doc(hidden)]
1
    fn __chaining_le(&self, other: &Rhs) -> ControlFlow<bool> {
1
        default_chaining_impl(self, other, Ordering::is_le)
1
    }
    /// Same as `__chaining_lt`, but for `>` instead of `<`.
    #[inline]
    #[unstable(feature = "partial_ord_chaining_methods", issue = "none")]
    #[doc(hidden)]
1
    fn __chaining_gt(&self, other: &Rhs) -> ControlFlow<bool> {
1
        default_chaining_impl(self, other, Ordering::is_gt)
1
    }
    /// Same as `__chaining_lt`, but for `>=` instead of `<`.
    #[inline]
    #[unstable(feature = "partial_ord_chaining_methods", issue = "none")]
    #[doc(hidden)]
1
    fn __chaining_ge(&self, other: &Rhs) -> ControlFlow<bool> {
1
        default_chaining_impl(self, other, Ordering::is_ge)
1
    }
}
4
fn default_chaining_impl<T, U>(
4
    lhs: &T,
4
    rhs: &U,
4
    p: impl FnOnce(Ordering) -> bool,
4
) -> ControlFlow<bool>
4
where
4
    T: PartialOrd<U> + PointeeSized,
4
    U: PointeeSized,
{
    // It's important that this only call `partial_cmp` once, not call `eq` then
    // one of the relational operators.  We don't want to `bcmp`-then-`memcp` a
    // `String`, for example, or similarly for other data structures (#108157).
4
    match <T as PartialOrd<U>>::partial_cmp(lhs, rhs) {
        Some(Equal) => ControlFlow::Continue(()),
4
        Some(c) => ControlFlow::Break(p(c)),
        None => ControlFlow::Break(false),
    }
4
}
/// Derive macro generating an impl of the trait [`PartialOrd`].
/// The behavior of this macro is described in detail [here](PartialOrd#derivable).
#[rustc_builtin_macro]
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow_internal_unstable(core_intrinsics)]
#[cfg(not(feature = "ferrocene_certified"))]
pub macro PartialOrd($item:item) {
    /* compiler built-in */
}
/// Compares and returns the minimum of two values.
///
/// Returns the first argument if the comparison determines them to be equal.
///
/// Internally uses an alias to [`Ord::min`].
///
/// # Examples
///
/// ```
/// use std::cmp;
///
/// assert_eq!(cmp::min(1, 2), 1);
/// assert_eq!(cmp::min(2, 2), 2);
/// ```
/// ```
/// use std::cmp::{self, Ordering};
///
/// #[derive(Eq)]
/// struct Equal(&'static str);
///
/// impl PartialEq for Equal {
///     fn eq(&self, other: &Self) -> bool { true }
/// }
/// impl PartialOrd for Equal {
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
/// }
/// impl Ord for Equal {
///     fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
/// }
///
/// assert_eq!(cmp::min(Equal("v1"), Equal("v2")).0, "v1");
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "cmp_min"]
1303
pub fn min<T: Ord>(v1: T, v2: T) -> T {
1303
    v1.min(v2)
1303
}
/// Returns the minimum of two values with respect to the specified comparison function.
///
/// Returns the first argument if the comparison determines them to be equal.
///
/// The parameter order is preserved when calling the `compare` function, i.e. `v1` is
/// always passed as the first argument and `v2` as the second.
///
/// # Examples
///
/// ```
/// use std::cmp;
///
/// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
///
/// let result = cmp::min_by(2, -1, abs_cmp);
/// assert_eq!(result, -1);
///
/// let result = cmp::min_by(2, -3, abs_cmp);
/// assert_eq!(result, 2);
///
/// let result = cmp::min_by(1, -1, abs_cmp);
/// assert_eq!(result, 1);
/// ```
#[inline]
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
#[cfg(not(feature = "ferrocene_certified"))]
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
    if compare(&v1, &v2).is_le() { v1 } else { v2 }
}
/// Returns the element that gives the minimum value from the specified function.
///
/// Returns the first argument if the comparison determines them to be equal.
///
/// # Examples
///
/// ```
/// use std::cmp;
///
/// let result = cmp::min_by_key(2, -1, |x: &i32| x.abs());
/// assert_eq!(result, -1);
///
/// let result = cmp::min_by_key(2, -3, |x: &i32| x.abs());
/// assert_eq!(result, 2);
///
/// let result = cmp::min_by_key(1, -1, |x: &i32| x.abs());
/// assert_eq!(result, 1);
/// ```
#[inline]
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
#[cfg(not(feature = "ferrocene_certified"))]
pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
    if f(&v2) < f(&v1) { v2 } else { v1 }
}
/// Compares and returns the maximum of two values.
///
/// Returns the second argument if the comparison determines them to be equal.
///
/// Internally uses an alias to [`Ord::max`].
///
/// # Examples
///
/// ```
/// use std::cmp;
///
/// assert_eq!(cmp::max(1, 2), 2);
/// assert_eq!(cmp::max(2, 2), 2);
/// ```
/// ```
/// use std::cmp::{self, Ordering};
///
/// #[derive(Eq)]
/// struct Equal(&'static str);
///
/// impl PartialEq for Equal {
///     fn eq(&self, other: &Self) -> bool { true }
/// }
/// impl PartialOrd for Equal {
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
/// }
/// impl Ord for Equal {
///     fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
/// }
///
/// assert_eq!(cmp::max(Equal("v1"), Equal("v2")).0, "v2");
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "cmp_max"]
552
pub fn max<T: Ord>(v1: T, v2: T) -> T {
552
    v1.max(v2)
552
}
/// Returns the maximum of two values with respect to the specified comparison function.
///
/// Returns the second argument if the comparison determines them to be equal.
///
/// The parameter order is preserved when calling the `compare` function, i.e. `v1` is
/// always passed as the first argument and `v2` as the second.
///
/// # Examples
///
/// ```
/// use std::cmp;
///
/// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
///
/// let result = cmp::max_by(3, -2, abs_cmp) ;
/// assert_eq!(result, 3);
///
/// let result = cmp::max_by(1, -2, abs_cmp);
/// assert_eq!(result, -2);
///
/// let result = cmp::max_by(1, -1, abs_cmp);
/// assert_eq!(result, -1);
/// ```
#[inline]
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
#[cfg(not(feature = "ferrocene_certified"))]
pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
    if compare(&v1, &v2).is_gt() { v1 } else { v2 }
}
/// Returns the element that gives the maximum value from the specified function.
///
/// Returns the second argument if the comparison determines them to be equal.
///
/// # Examples
///
/// ```
/// use std::cmp;
///
/// let result = cmp::max_by_key(3, -2, |x: &i32| x.abs());
/// assert_eq!(result, 3);
///
/// let result = cmp::max_by_key(1, -2, |x: &i32| x.abs());
/// assert_eq!(result, -2);
///
/// let result = cmp::max_by_key(1, -1, |x: &i32| x.abs());
/// assert_eq!(result, -1);
/// ```
#[inline]
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
#[cfg(not(feature = "ferrocene_certified"))]
pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
    if f(&v2) < f(&v1) { v1 } else { v2 }
}
/// Compares and sorts two values, returning minimum and maximum.
///
/// Returns `[v1, v2]` if the comparison determines them to be equal.
///
/// # Examples
///
/// ```
/// #![feature(cmp_minmax)]
/// use std::cmp;
///
/// assert_eq!(cmp::minmax(1, 2), [1, 2]);
/// assert_eq!(cmp::minmax(2, 1), [1, 2]);
///
/// // You can destructure the result using array patterns
/// let [min, max] = cmp::minmax(42, 17);
/// assert_eq!(min, 17);
/// assert_eq!(max, 42);
/// ```
/// ```
/// #![feature(cmp_minmax)]
/// use std::cmp::{self, Ordering};
///
/// #[derive(Eq)]
/// struct Equal(&'static str);
///
/// impl PartialEq for Equal {
///     fn eq(&self, other: &Self) -> bool { true }
/// }
/// impl PartialOrd for Equal {
///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
/// }
/// impl Ord for Equal {
///     fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
/// }
///
/// assert_eq!(cmp::minmax(Equal("v1"), Equal("v2")).map(|v| v.0), ["v1", "v2"]);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "cmp_minmax", issue = "115939")]
#[cfg(not(feature = "ferrocene_certified"))]
pub fn minmax<T>(v1: T, v2: T) -> [T; 2]
where
    T: Ord,
{
    if v2 < v1 { [v2, v1] } else { [v1, v2] }
}
/// Returns minimum and maximum values with respect to the specified comparison function.
///
/// Returns `[v1, v2]` if the comparison determines them to be equal.
///
/// The parameter order is preserved when calling the `compare` function, i.e. `v1` is
/// always passed as the first argument and `v2` as the second.
///
/// # Examples
///
/// ```
/// #![feature(cmp_minmax)]
/// use std::cmp;
///
/// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
///
/// assert_eq!(cmp::minmax_by(-2, 1, abs_cmp), [1, -2]);
/// assert_eq!(cmp::minmax_by(-1, 2, abs_cmp), [-1, 2]);
/// assert_eq!(cmp::minmax_by(-2, 2, abs_cmp), [-2, 2]);
///
/// // You can destructure the result using array patterns
/// let [min, max] = cmp::minmax_by(-42, 17, abs_cmp);
/// assert_eq!(min, 17);
/// assert_eq!(max, -42);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "cmp_minmax", issue = "115939")]
#[cfg(not(feature = "ferrocene_certified"))]
pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
where
    F: FnOnce(&T, &T) -> Ordering,
{
    if compare(&v1, &v2).is_le() { [v1, v2] } else { [v2, v1] }
}
/// Returns minimum and maximum values with respect to the specified key function.
///
/// Returns `[v1, v2]` if the comparison determines them to be equal.
///
/// # Examples
///
/// ```
/// #![feature(cmp_minmax)]
/// use std::cmp;
///
/// assert_eq!(cmp::minmax_by_key(-2, 1, |x: &i32| x.abs()), [1, -2]);
/// assert_eq!(cmp::minmax_by_key(-2, 2, |x: &i32| x.abs()), [-2, 2]);
///
/// // You can destructure the result using array patterns
/// let [min, max] = cmp::minmax_by_key(-42, 17, |x: &i32| x.abs());
/// assert_eq!(min, 17);
/// assert_eq!(max, -42);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "cmp_minmax", issue = "115939")]
#[cfg(not(feature = "ferrocene_certified"))]
pub fn minmax_by_key<T, F, K>(v1: T, v2: T, mut f: F) -> [T; 2]
where
    F: FnMut(&T) -> K,
    K: Ord,
{
    if f(&v2) < f(&v1) { [v2, v1] } else { [v1, v2] }
}
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
mod impls {
    use crate::cmp::Ordering::{self, Equal, Greater, Less};
    use crate::hint::unreachable_unchecked;
    #[cfg(not(feature = "ferrocene_certified"))]
    use crate::marker::PointeeSized;
    use crate::ops::ControlFlow::{self, Break, Continue};
    macro_rules! partial_eq_impl {
        ($($t:ty)*) => ($(
            #[stable(feature = "rust1", since = "1.0.0")]
            #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
            impl const PartialEq for $t {
                #[inline]
2787187
                fn eq(&self, other: &Self) -> bool { *self == *other }
                #[inline]
4796
                fn ne(&self, other: &Self) -> bool { *self != *other }
            }
        )*)
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl PartialEq for () {
        #[inline]
        fn eq(&self, _other: &()) -> bool {
            true
        }
        #[inline]
        fn ne(&self, _other: &()) -> bool {
            false
        }
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    partial_eq_impl! {
        bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
    }
    #[cfg(feature = "ferrocene_certified")]
    partial_eq_impl! {
        bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64
    }
    macro_rules! eq_impl {
        ($($t:ty)*) => ($(
            #[stable(feature = "rust1", since = "1.0.0")]
            impl Eq for $t {}
        )*)
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    eq_impl! { () bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
    #[cfg(feature = "ferrocene_certified")]
    eq_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
    #[rustfmt::skip]
    macro_rules! partial_ord_methods_primitive_impl {
        () => {
            #[inline(always)]
101197267
            fn lt(&self, other: &Self) -> bool { *self <  *other }
            #[inline(always)]
5923523
            fn le(&self, other: &Self) -> bool { *self <= *other }
            #[inline(always)]
21308
            fn gt(&self, other: &Self) -> bool { *self >  *other }
            #[inline(always)]
5172
            fn ge(&self, other: &Self) -> bool { *self >= *other }
            // These implementations are the same for `Ord` or `PartialOrd` types
            // because if either is NAN the `==` test will fail so we end up in
            // the `Break` case and the comparison will correctly return `false`.
            #[inline]
            fn __chaining_lt(&self, other: &Self) -> ControlFlow<bool> {
                let (lhs, rhs) = (*self, *other);
                if lhs == rhs { Continue(()) } else { Break(lhs < rhs) }
            }
            #[inline]
            fn __chaining_le(&self, other: &Self) -> ControlFlow<bool> {
                let (lhs, rhs) = (*self, *other);
                if lhs == rhs { Continue(()) } else { Break(lhs <= rhs) }
            }
            #[inline]
            fn __chaining_gt(&self, other: &Self) -> ControlFlow<bool> {
                let (lhs, rhs) = (*self, *other);
                if lhs == rhs { Continue(()) } else { Break(lhs > rhs) }
            }
            #[inline]
            fn __chaining_ge(&self, other: &Self) -> ControlFlow<bool> {
                let (lhs, rhs) = (*self, *other);
                if lhs == rhs { Continue(()) } else { Break(lhs >= rhs) }
            }
        };
    }
    macro_rules! partial_ord_impl {
        ($($t:ty)*) => ($(
            #[stable(feature = "rust1", since = "1.0.0")]
            impl PartialOrd for $t {
                #[inline]
                fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
                    match (*self <= *other, *self >= *other) {
                        (false, false) => None,
                        (false, true) => Some(Greater),
                        (true, false) => Some(Less),
                        (true, true) => Some(Equal),
                    }
                }
                partial_ord_methods_primitive_impl!();
            }
        )*)
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl PartialOrd for () {
        #[inline]
        fn partial_cmp(&self, _: &()) -> Option<Ordering> {
            Some(Equal)
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    impl PartialOrd for bool {
        #[inline]
        fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
            Some(self.cmp(other))
        }
        partial_ord_methods_primitive_impl!();
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    partial_ord_impl! { f16 f32 f64 f128 }
    #[cfg(feature = "ferrocene_certified")]
    partial_ord_impl! { f32 f64 }
    macro_rules! ord_impl {
        ($($t:ty)*) => ($(
            #[stable(feature = "rust1", since = "1.0.0")]
            impl PartialOrd for $t {
                #[inline]
308324
                fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
308324
                    Some(crate::intrinsics::three_way_compare(*self, *other))
308324
                }
                partial_ord_methods_primitive_impl!();
            }
            #[stable(feature = "rust1", since = "1.0.0")]
            impl Ord for $t {
                #[inline]
1914007
                fn cmp(&self, other: &Self) -> Ordering {
1914007
                    crate::intrinsics::three_way_compare(*self, *other)
1914007
                }
            }
        )*)
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl Ord for () {
        #[inline]
        fn cmp(&self, _other: &()) -> Ordering {
            Equal
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    impl Ord for bool {
        #[inline]
        fn cmp(&self, other: &bool) -> Ordering {
            // Casting to i8's and converting the difference to an Ordering generates
            // more optimal assembly.
            // See <https://github.com/rust-lang/rust/issues/66780> for more info.
            match (*self as i8) - (*other as i8) {
                -1 => Less,
                0 => Equal,
                1 => Greater,
                // Ferrocene annotation: This match arm cannot be covered because it is unreachable.
                // See the safety comment below.
                //
                // SAFETY: bool as i8 returns 0 or 1, so the difference can't be anything else
                _ => unsafe { unreachable_unchecked() },
            }
        }
        #[inline]
        fn min(self, other: bool) -> bool {
            self & other
        }
        #[inline]
        fn max(self, other: bool) -> bool {
            self | other
        }
        #[inline]
        fn clamp(self, min: bool, max: bool) -> bool {
            assert!(min <= max);
            self.max(min).min(max)
        }
    }
    #[cfg(not(feature = "ferrocene_certified"))]
    ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
    #[cfg(feature = "ferrocene_certified")]
    ord_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
    #[unstable(feature = "never_type", issue = "35121")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl PartialEq for ! {
        #[inline]
        fn eq(&self, _: &!) -> bool {
            *self
        }
    }
    #[unstable(feature = "never_type", issue = "35121")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl Eq for ! {}
    #[unstable(feature = "never_type", issue = "35121")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl PartialOrd for ! {
        #[inline]
        fn partial_cmp(&self, _: &!) -> Option<Ordering> {
            *self
        }
    }
    #[unstable(feature = "never_type", issue = "35121")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl Ord for ! {
        #[inline]
        fn cmp(&self, _: &!) -> Ordering {
            *self
        }
    }
    // & pointers
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &A
    where
        A: [const] PartialEq<B>,
    {
        #[inline]
9005
        fn eq(&self, other: &&B) -> bool {
9005
            PartialEq::eq(*self, *other)
9005
        }
        #[inline]
        fn ne(&self, other: &&B) -> bool {
            PartialEq::ne(*self, *other)
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized, B: PointeeSized> PartialOrd<&B> for &A
    where
        A: PartialOrd<B>,
    {
        #[inline]
        fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
            PartialOrd::partial_cmp(*self, *other)
        }
        #[inline]
3
        fn lt(&self, other: &&B) -> bool {
3
            PartialOrd::lt(*self, *other)
3
        }
        #[inline]
1793
        fn le(&self, other: &&B) -> bool {
1793
            PartialOrd::le(*self, *other)
1793
        }
        #[inline]
1
        fn gt(&self, other: &&B) -> bool {
1
            PartialOrd::gt(*self, *other)
1
        }
        #[inline]
1
        fn ge(&self, other: &&B) -> bool {
1
            PartialOrd::ge(*self, *other)
1
        }
        #[inline]
1
        fn __chaining_lt(&self, other: &&B) -> ControlFlow<bool> {
1
            PartialOrd::__chaining_lt(*self, *other)
1
        }
        #[inline]
1
        fn __chaining_le(&self, other: &&B) -> ControlFlow<bool> {
1
            PartialOrd::__chaining_le(*self, *other)
1
        }
        #[inline]
1
        fn __chaining_gt(&self, other: &&B) -> ControlFlow<bool> {
1
            PartialOrd::__chaining_gt(*self, *other)
1
        }
        #[inline]
1
        fn __chaining_ge(&self, other: &&B) -> ControlFlow<bool> {
1
            PartialOrd::__chaining_ge(*self, *other)
1
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized> Ord for &A
    where
        A: Ord,
    {
        #[inline]
1637
        fn cmp(&self, other: &Self) -> Ordering {
1637
            Ord::cmp(*self, *other)
1637
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized> Eq for &A where A: Eq {}
    // &mut pointers
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &mut A
    where
        A: [const] PartialEq<B>,
    {
        #[inline]
        fn eq(&self, other: &&mut B) -> bool {
            PartialEq::eq(*self, *other)
        }
        #[inline]
        fn ne(&self, other: &&mut B) -> bool {
            PartialEq::ne(*self, *other)
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized, B: PointeeSized> PartialOrd<&mut B> for &mut A
    where
        A: PartialOrd<B>,
    {
        #[inline]
        fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
            PartialOrd::partial_cmp(*self, *other)
        }
        #[inline]
        fn lt(&self, other: &&mut B) -> bool {
            PartialOrd::lt(*self, *other)
        }
        #[inline]
        fn le(&self, other: &&mut B) -> bool {
            PartialOrd::le(*self, *other)
        }
        #[inline]
        fn gt(&self, other: &&mut B) -> bool {
            PartialOrd::gt(*self, *other)
        }
        #[inline]
        fn ge(&self, other: &&mut B) -> bool {
            PartialOrd::ge(*self, *other)
        }
        #[inline]
        fn __chaining_lt(&self, other: &&mut B) -> ControlFlow<bool> {
            PartialOrd::__chaining_lt(*self, *other)
        }
        #[inline]
        fn __chaining_le(&self, other: &&mut B) -> ControlFlow<bool> {
            PartialOrd::__chaining_le(*self, *other)
        }
        #[inline]
        fn __chaining_gt(&self, other: &&mut B) -> ControlFlow<bool> {
            PartialOrd::__chaining_gt(*self, *other)
        }
        #[inline]
        fn __chaining_ge(&self, other: &&mut B) -> ControlFlow<bool> {
            PartialOrd::__chaining_ge(*self, *other)
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized> Ord for &mut A
    where
        A: Ord,
    {
        #[inline]
        fn cmp(&self, other: &Self) -> Ordering {
            Ord::cmp(*self, *other)
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized> Eq for &mut A where A: Eq {}
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &A
    where
        A: [const] PartialEq<B>,
    {
        #[inline]
        fn eq(&self, other: &&mut B) -> bool {
            PartialEq::eq(*self, *other)
        }
        #[inline]
        fn ne(&self, other: &&mut B) -> bool {
            PartialEq::ne(*self, *other)
        }
    }
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
    #[cfg(not(feature = "ferrocene_certified"))]
    impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &mut A
    where
        A: [const] PartialEq<B>,
    {
        #[inline]
        fn eq(&self, other: &&B) -> bool {
            PartialEq::eq(*self, *other)
        }
        #[inline]
        fn ne(&self, other: &&B) -> bool {
            PartialEq::ne(*self, *other)
        }
    }
}