]> git.lizzy.rs Git - rust.git/blob - library/core/src/cmp.rs
Fix naming format of IEEE 754 standard
[rust.git] / library / core / src / cmp.rs
1 //! Utilities for comparing and ordering values.
2 //!
3 //! This module contains various tools for comparing and ordering values. In
4 //! summary:
5 //!
6 //! * [`Eq`] and [`PartialEq`] are traits that allow you to define total and
7 //!   partial equality between values, respectively. Implementing them overloads
8 //!   the `==` and `!=` operators.
9 //! * [`Ord`] and [`PartialOrd`] are traits that allow you to define total and
10 //!   partial orderings between values, respectively. Implementing them overloads
11 //!   the `<`, `<=`, `>`, and `>=` operators.
12 //! * [`Ordering`] is an enum returned by the main functions of [`Ord`] and
13 //!   [`PartialOrd`], and describes an ordering.
14 //! * [`Reverse`] is a struct that allows you to easily reverse an ordering.
15 //! * [`max`] and [`min`] are functions that build off of [`Ord`] and allow you
16 //!   to find the maximum or minimum of two values.
17 //!
18 //! For more details, see the respective documentation of each item in the list.
19 //!
20 //! [`max`]: Ord::max
21 //! [`min`]: Ord::min
22
23 #![stable(feature = "rust1", since = "1.0.0")]
24
25 use crate::marker::Destruct;
26
27 use self::Ordering::*;
28
29 /// Trait for equality comparisons which are [partial equivalence
30 /// relations](https://en.wikipedia.org/wiki/Partial_equivalence_relation).
31 ///
32 /// `x.eq(y)` can also be written `x == y`, and `x.ne(y)` can be written `x != y`.
33 /// We use the easier-to-read infix notation in the remainder of this documentation.
34 ///
35 /// This trait allows for partial equality, for types that do not have a full
36 /// equivalence relation. For example, in floating point numbers `NaN != NaN`,
37 /// so floating point types implement `PartialEq` but not [`trait@Eq`].
38 ///
39 /// Implementations must ensure that `eq` and `ne` are consistent with each other:
40 ///
41 /// - `a != b` if and only if `!(a == b)`.
42 ///
43 /// The default implementation of `ne` provides this consistency and is almost
44 /// always sufficient. It should not be overridden without very good reason.
45 ///
46 /// If [`PartialOrd`] or [`Ord`] are also implemented for `Self` and `Rhs`, their methods must also
47 /// be consistent with `PartialEq` (see the documentation of those traits for the exact
48 /// requirements). It's easy to accidentally make them disagree by deriving some of the traits and
49 /// manually implementing others.
50 ///
51 /// The equality relation `==` must satisfy the following conditions
52 /// (for all `a`, `b`, `c` of type `A`, `B`, `C`):
53 ///
54 /// - **Symmetric**: if `A: PartialEq<B>` and `B: PartialEq<A>`, then **`a == b`
55 ///   implies `b == a`**; and
56 ///
57 /// - **Transitive**: if `A: PartialEq<B>` and `B: PartialEq<C>` and `A:
58 ///   PartialEq<C>`, then **`a == b` and `b == c` implies `a == c`**.
59 ///
60 /// Note that the `B: PartialEq<A>` (symmetric) and `A: PartialEq<C>`
61 /// (transitive) impls are not forced to exist, but these requirements apply
62 /// whenever they do exist.
63 ///
64 /// ## Derivable
65 ///
66 /// This trait can be used with `#[derive]`. When `derive`d on structs, two
67 /// instances are equal if all fields are equal, and not equal if any fields
68 /// are not equal. When `derive`d on enums, two instances are equal if they
69 /// are the same variant and all fields are equal.
70 ///
71 /// ## How can I implement `PartialEq`?
72 ///
73 /// An example implementation for a domain in which two books are considered
74 /// the same book if their ISBN matches, even if the formats differ:
75 ///
76 /// ```
77 /// enum BookFormat {
78 ///     Paperback,
79 ///     Hardback,
80 ///     Ebook,
81 /// }
82 ///
83 /// struct Book {
84 ///     isbn: i32,
85 ///     format: BookFormat,
86 /// }
87 ///
88 /// impl PartialEq for Book {
89 ///     fn eq(&self, other: &Self) -> bool {
90 ///         self.isbn == other.isbn
91 ///     }
92 /// }
93 ///
94 /// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
95 /// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
96 /// let b3 = Book { isbn: 10, format: BookFormat::Paperback };
97 ///
98 /// assert!(b1 == b2);
99 /// assert!(b1 != b3);
100 /// ```
101 ///
102 /// ## How can I compare two different types?
103 ///
104 /// The type you can compare with is controlled by `PartialEq`'s type parameter.
105 /// For example, let's tweak our previous code a bit:
106 ///
107 /// ```
108 /// // The derive implements <BookFormat> == <BookFormat> comparisons
109 /// #[derive(PartialEq)]
110 /// enum BookFormat {
111 ///     Paperback,
112 ///     Hardback,
113 ///     Ebook,
114 /// }
115 ///
116 /// struct Book {
117 ///     isbn: i32,
118 ///     format: BookFormat,
119 /// }
120 ///
121 /// // Implement <Book> == <BookFormat> comparisons
122 /// impl PartialEq<BookFormat> for Book {
123 ///     fn eq(&self, other: &BookFormat) -> bool {
124 ///         self.format == *other
125 ///     }
126 /// }
127 ///
128 /// // Implement <BookFormat> == <Book> comparisons
129 /// impl PartialEq<Book> for BookFormat {
130 ///     fn eq(&self, other: &Book) -> bool {
131 ///         *self == other.format
132 ///     }
133 /// }
134 ///
135 /// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
136 ///
137 /// assert!(b1 == BookFormat::Paperback);
138 /// assert!(BookFormat::Ebook != b1);
139 /// ```
140 ///
141 /// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`,
142 /// we allow `BookFormat`s to be compared with `Book`s.
143 ///
144 /// A comparison like the one above, which ignores some fields of the struct,
145 /// can be dangerous. It can easily lead to an unintended violation of the
146 /// requirements for a partial equivalence relation. For example, if we kept
147 /// the above implementation of `PartialEq<Book>` for `BookFormat` and added an
148 /// implementation of `PartialEq<Book>` for `Book` (either via a `#[derive]` or
149 /// via the manual implementation from the first example) then the result would
150 /// violate transitivity:
151 ///
152 /// ```should_panic
153 /// #[derive(PartialEq)]
154 /// enum BookFormat {
155 ///     Paperback,
156 ///     Hardback,
157 ///     Ebook,
158 /// }
159 ///
160 /// #[derive(PartialEq)]
161 /// struct Book {
162 ///     isbn: i32,
163 ///     format: BookFormat,
164 /// }
165 ///
166 /// impl PartialEq<BookFormat> for Book {
167 ///     fn eq(&self, other: &BookFormat) -> bool {
168 ///         self.format == *other
169 ///     }
170 /// }
171 ///
172 /// impl PartialEq<Book> for BookFormat {
173 ///     fn eq(&self, other: &Book) -> bool {
174 ///         *self == other.format
175 ///     }
176 /// }
177 ///
178 /// fn main() {
179 ///     let b1 = Book { isbn: 1, format: BookFormat::Paperback };
180 ///     let b2 = Book { isbn: 2, format: BookFormat::Paperback };
181 ///
182 ///     assert!(b1 == BookFormat::Paperback);
183 ///     assert!(BookFormat::Paperback == b2);
184 ///
185 ///     // The following should hold by transitivity but doesn't.
186 ///     assert!(b1 == b2); // <-- PANICS
187 /// }
188 /// ```
189 ///
190 /// # Examples
191 ///
192 /// ```
193 /// let x: u32 = 0;
194 /// let y: u32 = 1;
195 ///
196 /// assert_eq!(x == y, false);
197 /// assert_eq!(x.eq(&y), false);
198 /// ```
199 ///
200 /// [`eq`]: PartialEq::eq
201 /// [`ne`]: PartialEq::ne
202 #[lang = "eq"]
203 #[stable(feature = "rust1", since = "1.0.0")]
204 #[doc(alias = "==")]
205 #[doc(alias = "!=")]
206 #[cfg_attr(
207     bootstrap,
208     rustc_on_unimplemented(
209         message = "can't compare `{Self}` with `{Rhs}`",
210         label = "no implementation for `{Self} == {Rhs}`"
211     )
212 )]
213 #[cfg_attr(
214     not(bootstrap),
215     rustc_on_unimplemented(
216         message = "can't compare `{Self}` with `{Rhs}`",
217         label = "no implementation for `{Self} == {Rhs}`",
218         append_const_msg,
219     )
220 )]
221 #[const_trait]
222 #[rustc_diagnostic_item = "PartialEq"]
223 pub trait PartialEq<Rhs: ?Sized = Self> {
224     /// This method tests for `self` and `other` values to be equal, and is used
225     /// by `==`.
226     #[must_use]
227     #[stable(feature = "rust1", since = "1.0.0")]
228     fn eq(&self, other: &Rhs) -> bool;
229
230     /// This method tests for `!=`. The default implementation is almost always
231     /// sufficient, and should not be overridden without very good reason.
232     #[inline]
233     #[must_use]
234     #[stable(feature = "rust1", since = "1.0.0")]
235     fn ne(&self, other: &Rhs) -> bool {
236         !self.eq(other)
237     }
238 }
239
240 /// Derive macro generating an impl of the trait `PartialEq`.
241 #[rustc_builtin_macro]
242 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
243 #[allow_internal_unstable(core_intrinsics, structural_match)]
244 pub macro PartialEq($item:item) {
245     /* compiler built-in */
246 }
247
248 /// Trait for equality comparisons which are [equivalence relations](
249 /// https://en.wikipedia.org/wiki/Equivalence_relation).
250 ///
251 /// This means, that in addition to `a == b` and `a != b` being strict inverses, the equality must
252 /// be (for all `a`, `b` and `c`):
253 ///
254 /// - reflexive: `a == a`;
255 /// - symmetric: `a == b` implies `b == a`; and
256 /// - transitive: `a == b` and `b == c` implies `a == c`.
257 ///
258 /// This property cannot be checked by the compiler, and therefore `Eq` implies
259 /// [`PartialEq`], and has no extra methods.
260 ///
261 /// ## Derivable
262 ///
263 /// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has
264 /// no extra methods, it is only informing the compiler that this is an
265 /// equivalence relation rather than a partial equivalence relation. Note that
266 /// the `derive` strategy requires all fields are `Eq`, which isn't
267 /// always desired.
268 ///
269 /// ## How can I implement `Eq`?
270 ///
271 /// If you cannot use the `derive` strategy, specify that your type implements
272 /// `Eq`, which has no methods:
273 ///
274 /// ```
275 /// enum BookFormat { Paperback, Hardback, Ebook }
276 /// struct Book {
277 ///     isbn: i32,
278 ///     format: BookFormat,
279 /// }
280 /// impl PartialEq for Book {
281 ///     fn eq(&self, other: &Self) -> bool {
282 ///         self.isbn == other.isbn
283 ///     }
284 /// }
285 /// impl Eq for Book {}
286 /// ```
287 #[doc(alias = "==")]
288 #[doc(alias = "!=")]
289 #[stable(feature = "rust1", since = "1.0.0")]
290 #[rustc_diagnostic_item = "Eq"]
291 pub trait Eq: PartialEq<Self> {
292     // this method is used solely by #[deriving] to assert
293     // that every component of a type implements #[deriving]
294     // itself, the current deriving infrastructure means doing this
295     // assertion without using a method on this trait is nearly
296     // impossible.
297     //
298     // This should never be implemented by hand.
299     #[doc(hidden)]
300     #[no_coverage] // rust-lang/rust#84605
301     #[inline]
302     #[stable(feature = "rust1", since = "1.0.0")]
303     fn assert_receiver_is_total_eq(&self) {}
304 }
305
306 /// Derive macro generating an impl of the trait `Eq`.
307 #[rustc_builtin_macro]
308 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
309 #[allow_internal_unstable(core_intrinsics, derive_eq, structural_match, no_coverage)]
310 pub macro Eq($item:item) {
311     /* compiler built-in */
312 }
313
314 // FIXME: this struct is used solely by #[derive] to
315 // assert that every component of a type implements Eq.
316 //
317 // This struct should never appear in user code.
318 #[doc(hidden)]
319 #[allow(missing_debug_implementations)]
320 #[unstable(feature = "derive_eq", reason = "deriving hack, should not be public", issue = "none")]
321 pub struct AssertParamIsEq<T: Eq + ?Sized> {
322     _field: crate::marker::PhantomData<T>,
323 }
324
325 /// An `Ordering` is the result of a comparison between two values.
326 ///
327 /// # Examples
328 ///
329 /// ```
330 /// use std::cmp::Ordering;
331 ///
332 /// let result = 1.cmp(&2);
333 /// assert_eq!(Ordering::Less, result);
334 ///
335 /// let result = 1.cmp(&1);
336 /// assert_eq!(Ordering::Equal, result);
337 ///
338 /// let result = 2.cmp(&1);
339 /// assert_eq!(Ordering::Greater, result);
340 /// ```
341 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
342 #[stable(feature = "rust1", since = "1.0.0")]
343 #[repr(i8)]
344 pub enum Ordering {
345     /// An ordering where a compared value is less than another.
346     #[stable(feature = "rust1", since = "1.0.0")]
347     Less = -1,
348     /// An ordering where a compared value is equal to another.
349     #[stable(feature = "rust1", since = "1.0.0")]
350     Equal = 0,
351     /// An ordering where a compared value is greater than another.
352     #[stable(feature = "rust1", since = "1.0.0")]
353     Greater = 1,
354 }
355
356 impl Ordering {
357     /// Returns `true` if the ordering is the `Equal` variant.
358     ///
359     /// # Examples
360     ///
361     /// ```
362     /// use std::cmp::Ordering;
363     ///
364     /// assert_eq!(Ordering::Less.is_eq(), false);
365     /// assert_eq!(Ordering::Equal.is_eq(), true);
366     /// assert_eq!(Ordering::Greater.is_eq(), false);
367     /// ```
368     #[inline]
369     #[must_use]
370     #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
371     #[stable(feature = "ordering_helpers", since = "1.53.0")]
372     pub const fn is_eq(self) -> bool {
373         matches!(self, Equal)
374     }
375
376     /// Returns `true` if the ordering is not the `Equal` variant.
377     ///
378     /// # Examples
379     ///
380     /// ```
381     /// use std::cmp::Ordering;
382     ///
383     /// assert_eq!(Ordering::Less.is_ne(), true);
384     /// assert_eq!(Ordering::Equal.is_ne(), false);
385     /// assert_eq!(Ordering::Greater.is_ne(), true);
386     /// ```
387     #[inline]
388     #[must_use]
389     #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
390     #[stable(feature = "ordering_helpers", since = "1.53.0")]
391     pub const fn is_ne(self) -> bool {
392         !matches!(self, Equal)
393     }
394
395     /// Returns `true` if the ordering is the `Less` variant.
396     ///
397     /// # Examples
398     ///
399     /// ```
400     /// use std::cmp::Ordering;
401     ///
402     /// assert_eq!(Ordering::Less.is_lt(), true);
403     /// assert_eq!(Ordering::Equal.is_lt(), false);
404     /// assert_eq!(Ordering::Greater.is_lt(), false);
405     /// ```
406     #[inline]
407     #[must_use]
408     #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
409     #[stable(feature = "ordering_helpers", since = "1.53.0")]
410     pub const fn is_lt(self) -> bool {
411         matches!(self, Less)
412     }
413
414     /// Returns `true` if the ordering is the `Greater` variant.
415     ///
416     /// # Examples
417     ///
418     /// ```
419     /// use std::cmp::Ordering;
420     ///
421     /// assert_eq!(Ordering::Less.is_gt(), false);
422     /// assert_eq!(Ordering::Equal.is_gt(), false);
423     /// assert_eq!(Ordering::Greater.is_gt(), true);
424     /// ```
425     #[inline]
426     #[must_use]
427     #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
428     #[stable(feature = "ordering_helpers", since = "1.53.0")]
429     pub const fn is_gt(self) -> bool {
430         matches!(self, Greater)
431     }
432
433     /// Returns `true` if the ordering is either the `Less` or `Equal` variant.
434     ///
435     /// # Examples
436     ///
437     /// ```
438     /// use std::cmp::Ordering;
439     ///
440     /// assert_eq!(Ordering::Less.is_le(), true);
441     /// assert_eq!(Ordering::Equal.is_le(), true);
442     /// assert_eq!(Ordering::Greater.is_le(), false);
443     /// ```
444     #[inline]
445     #[must_use]
446     #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
447     #[stable(feature = "ordering_helpers", since = "1.53.0")]
448     pub const fn is_le(self) -> bool {
449         !matches!(self, Greater)
450     }
451
452     /// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
453     ///
454     /// # Examples
455     ///
456     /// ```
457     /// use std::cmp::Ordering;
458     ///
459     /// assert_eq!(Ordering::Less.is_ge(), false);
460     /// assert_eq!(Ordering::Equal.is_ge(), true);
461     /// assert_eq!(Ordering::Greater.is_ge(), true);
462     /// ```
463     #[inline]
464     #[must_use]
465     #[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
466     #[stable(feature = "ordering_helpers", since = "1.53.0")]
467     pub const fn is_ge(self) -> bool {
468         !matches!(self, Less)
469     }
470
471     /// Reverses the `Ordering`.
472     ///
473     /// * `Less` becomes `Greater`.
474     /// * `Greater` becomes `Less`.
475     /// * `Equal` becomes `Equal`.
476     ///
477     /// # Examples
478     ///
479     /// Basic behavior:
480     ///
481     /// ```
482     /// use std::cmp::Ordering;
483     ///
484     /// assert_eq!(Ordering::Less.reverse(), Ordering::Greater);
485     /// assert_eq!(Ordering::Equal.reverse(), Ordering::Equal);
486     /// assert_eq!(Ordering::Greater.reverse(), Ordering::Less);
487     /// ```
488     ///
489     /// This method can be used to reverse a comparison:
490     ///
491     /// ```
492     /// let data: &mut [_] = &mut [2, 10, 5, 8];
493     ///
494     /// // sort the array from largest to smallest.
495     /// data.sort_by(|a, b| a.cmp(b).reverse());
496     ///
497     /// let b: &mut [_] = &mut [10, 8, 5, 2];
498     /// assert!(data == b);
499     /// ```
500     #[inline]
501     #[must_use]
502     #[rustc_const_stable(feature = "const_ordering", since = "1.48.0")]
503     #[stable(feature = "rust1", since = "1.0.0")]
504     pub const fn reverse(self) -> Ordering {
505         match self {
506             Less => Greater,
507             Equal => Equal,
508             Greater => Less,
509         }
510     }
511
512     /// Chains two orderings.
513     ///
514     /// Returns `self` when it's not `Equal`. Otherwise returns `other`.
515     ///
516     /// # Examples
517     ///
518     /// ```
519     /// use std::cmp::Ordering;
520     ///
521     /// let result = Ordering::Equal.then(Ordering::Less);
522     /// assert_eq!(result, Ordering::Less);
523     ///
524     /// let result = Ordering::Less.then(Ordering::Equal);
525     /// assert_eq!(result, Ordering::Less);
526     ///
527     /// let result = Ordering::Less.then(Ordering::Greater);
528     /// assert_eq!(result, Ordering::Less);
529     ///
530     /// let result = Ordering::Equal.then(Ordering::Equal);
531     /// assert_eq!(result, Ordering::Equal);
532     ///
533     /// let x: (i64, i64, i64) = (1, 2, 7);
534     /// let y: (i64, i64, i64) = (1, 5, 3);
535     /// let result = x.0.cmp(&y.0).then(x.1.cmp(&y.1)).then(x.2.cmp(&y.2));
536     ///
537     /// assert_eq!(result, Ordering::Less);
538     /// ```
539     #[inline]
540     #[must_use]
541     #[rustc_const_stable(feature = "const_ordering", since = "1.48.0")]
542     #[stable(feature = "ordering_chaining", since = "1.17.0")]
543     pub const fn then(self, other: Ordering) -> Ordering {
544         match self {
545             Equal => other,
546             _ => self,
547         }
548     }
549
550     /// Chains the ordering with the given function.
551     ///
552     /// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
553     /// the result.
554     ///
555     /// # Examples
556     ///
557     /// ```
558     /// use std::cmp::Ordering;
559     ///
560     /// let result = Ordering::Equal.then_with(|| Ordering::Less);
561     /// assert_eq!(result, Ordering::Less);
562     ///
563     /// let result = Ordering::Less.then_with(|| Ordering::Equal);
564     /// assert_eq!(result, Ordering::Less);
565     ///
566     /// let result = Ordering::Less.then_with(|| Ordering::Greater);
567     /// assert_eq!(result, Ordering::Less);
568     ///
569     /// let result = Ordering::Equal.then_with(|| Ordering::Equal);
570     /// assert_eq!(result, Ordering::Equal);
571     ///
572     /// let x: (i64, i64, i64) = (1, 2, 7);
573     /// let y: (i64, i64, i64) = (1, 5, 3);
574     /// let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));
575     ///
576     /// assert_eq!(result, Ordering::Less);
577     /// ```
578     #[inline]
579     #[must_use]
580     #[stable(feature = "ordering_chaining", since = "1.17.0")]
581     pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
582         match self {
583             Equal => f(),
584             _ => self,
585         }
586     }
587 }
588
589 /// A helper struct for reverse ordering.
590 ///
591 /// This struct is a helper to be used with functions like [`Vec::sort_by_key`] and
592 /// can be used to reverse order a part of a key.
593 ///
594 /// [`Vec::sort_by_key`]: ../../std/vec/struct.Vec.html#method.sort_by_key
595 ///
596 /// # Examples
597 ///
598 /// ```
599 /// use std::cmp::Reverse;
600 ///
601 /// let mut v = vec![1, 2, 3, 4, 5, 6];
602 /// v.sort_by_key(|&num| (num > 3, Reverse(num)));
603 /// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
604 /// ```
605 #[derive(PartialEq, Eq, Debug, Copy, Default, Hash)]
606 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
607 #[repr(transparent)]
608 pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
609
610 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
611 #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
612 impl<T: ~const PartialOrd> const PartialOrd for Reverse<T> {
613     #[inline]
614     fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
615         other.0.partial_cmp(&self.0)
616     }
617
618     #[inline]
619     fn lt(&self, other: &Self) -> bool {
620         other.0 < self.0
621     }
622     #[inline]
623     fn le(&self, other: &Self) -> bool {
624         other.0 <= self.0
625     }
626     #[inline]
627     fn gt(&self, other: &Self) -> bool {
628         other.0 > self.0
629     }
630     #[inline]
631     fn ge(&self, other: &Self) -> bool {
632         other.0 >= self.0
633     }
634 }
635
636 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
637 impl<T: Ord> Ord for Reverse<T> {
638     #[inline]
639     fn cmp(&self, other: &Reverse<T>) -> Ordering {
640         other.0.cmp(&self.0)
641     }
642 }
643
644 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
645 impl<T: Clone> Clone for Reverse<T> {
646     #[inline]
647     fn clone(&self) -> Reverse<T> {
648         Reverse(self.0.clone())
649     }
650
651     #[inline]
652     fn clone_from(&mut self, other: &Self) {
653         self.0.clone_from(&other.0)
654     }
655 }
656
657 /// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
658 ///
659 /// Implementations must be consistent with the [`PartialOrd`] implementation, and ensure
660 /// `max`, `min`, and `clamp` are consistent with `cmp`:
661 ///
662 /// - `partial_cmp(a, b) == Some(cmp(a, b))`.
663 /// - `max(a, b) == max_by(a, b, cmp)` (ensured by the default implementation).
664 /// - `min(a, b) == min_by(a, b, cmp)` (ensured by the default implementation).
665 /// - For `a.clamp(min, max)`, see the [method docs](#method.clamp)
666 ///   (ensured by the default implementation).
667 ///
668 /// It's easy to accidentally make `cmp` and `partial_cmp` disagree by
669 /// deriving some of the traits and manually implementing others.
670 ///
671 /// ## Corollaries
672 ///
673 /// From the above and the requirements of `PartialOrd`, it follows that `<` defines a strict total order.
674 /// This means that for all `a`, `b` and `c`:
675 ///
676 /// - exactly one of `a < b`, `a == b` or `a > b` is true; and
677 /// - `<` is transitive: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
678 ///
679 /// ## Derivable
680 ///
681 /// This trait can be used with `#[derive]`.
682 ///
683 /// When `derive`d on structs, it will produce a
684 /// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering
685 /// based on the top-to-bottom declaration order of the struct's members.
686 ///
687 /// When `derive`d on enums, variants are ordered by their discriminants.
688 /// By default, the discriminant is smallest for variants at the top, and
689 /// largest for variants at the bottom. Here's an example:
690 ///
691 /// ```
692 /// #[derive(PartialEq, Eq, PartialOrd, Ord)]
693 /// enum E {
694 ///     Top,
695 ///     Bottom,
696 /// }
697 ///
698 /// assert!(E::Top < E::Bottom);
699 /// ```
700 ///
701 /// However, manually setting the discriminants can override this default
702 /// behavior:
703 ///
704 /// ```
705 /// #[derive(PartialEq, Eq, PartialOrd, Ord)]
706 /// enum E {
707 ///     Top = 2,
708 ///     Bottom = 1,
709 /// }
710 ///
711 /// assert!(E::Bottom < E::Top);
712 /// ```
713 ///
714 /// ## Lexicographical comparison
715 ///
716 /// Lexicographical comparison is an operation with the following properties:
717 ///  - Two sequences are compared element by element.
718 ///  - The first mismatching element defines which sequence is lexicographically less or greater than the other.
719 ///  - If one sequence is a prefix of another, the shorter sequence is lexicographically less than the other.
720 ///  - If two sequence have equivalent elements and are of the same length, then the sequences are lexicographically equal.
721 ///  - An empty sequence is lexicographically less than any non-empty sequence.
722 ///  - Two empty sequences are lexicographically equal.
723 ///
724 /// ## How can I implement `Ord`?
725 ///
726 /// `Ord` requires that the type also be [`PartialOrd`] and [`Eq`] (which requires [`PartialEq`]).
727 ///
728 /// Then you must define an implementation for [`cmp`]. You may find it useful to use
729 /// [`cmp`] on your type's fields.
730 ///
731 /// Here's an example where you want to sort people by height only, disregarding `id`
732 /// and `name`:
733 ///
734 /// ```
735 /// use std::cmp::Ordering;
736 ///
737 /// #[derive(Eq)]
738 /// struct Person {
739 ///     id: u32,
740 ///     name: String,
741 ///     height: u32,
742 /// }
743 ///
744 /// impl Ord for Person {
745 ///     fn cmp(&self, other: &Self) -> Ordering {
746 ///         self.height.cmp(&other.height)
747 ///     }
748 /// }
749 ///
750 /// impl PartialOrd for Person {
751 ///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
752 ///         Some(self.cmp(other))
753 ///     }
754 /// }
755 ///
756 /// impl PartialEq for Person {
757 ///     fn eq(&self, other: &Self) -> bool {
758 ///         self.height == other.height
759 ///     }
760 /// }
761 /// ```
762 ///
763 /// [`cmp`]: Ord::cmp
764 #[doc(alias = "<")]
765 #[doc(alias = ">")]
766 #[doc(alias = "<=")]
767 #[doc(alias = ">=")]
768 #[stable(feature = "rust1", since = "1.0.0")]
769 #[rustc_diagnostic_item = "Ord"]
770 #[const_trait]
771 pub trait Ord: Eq + PartialOrd<Self> {
772     /// This method returns an [`Ordering`] between `self` and `other`.
773     ///
774     /// By convention, `self.cmp(&other)` returns the ordering matching the expression
775     /// `self <operator> other` if true.
776     ///
777     /// # Examples
778     ///
779     /// ```
780     /// use std::cmp::Ordering;
781     ///
782     /// assert_eq!(5.cmp(&10), Ordering::Less);
783     /// assert_eq!(10.cmp(&5), Ordering::Greater);
784     /// assert_eq!(5.cmp(&5), Ordering::Equal);
785     /// ```
786     #[must_use]
787     #[stable(feature = "rust1", since = "1.0.0")]
788     fn cmp(&self, other: &Self) -> Ordering;
789
790     /// Compares and returns the maximum of two values.
791     ///
792     /// Returns the second argument if the comparison determines them to be equal.
793     ///
794     /// # Examples
795     ///
796     /// ```
797     /// assert_eq!(2, 1.max(2));
798     /// assert_eq!(2, 2.max(2));
799     /// ```
800     #[stable(feature = "ord_max_min", since = "1.21.0")]
801     #[inline]
802     #[must_use]
803     fn max(self, other: Self) -> Self
804     where
805         Self: Sized,
806         Self: ~const Destruct,
807     {
808         // HACK(fee1-dead): go back to using `self.max_by(other, Ord::cmp)`
809         // when trait methods are allowed to be used when a const closure is
810         // expected.
811         match self.cmp(&other) {
812             Ordering::Less | Ordering::Equal => other,
813             Ordering::Greater => self,
814         }
815     }
816
817     /// Compares and returns the minimum of two values.
818     ///
819     /// Returns the first argument if the comparison determines them to be equal.
820     ///
821     /// # Examples
822     ///
823     /// ```
824     /// assert_eq!(1, 1.min(2));
825     /// assert_eq!(2, 2.min(2));
826     /// ```
827     #[stable(feature = "ord_max_min", since = "1.21.0")]
828     #[inline]
829     #[must_use]
830     fn min(self, other: Self) -> Self
831     where
832         Self: Sized,
833         Self: ~const Destruct,
834     {
835         // HACK(fee1-dead): go back to using `self.min_by(other, Ord::cmp)`
836         // when trait methods are allowed to be used when a const closure is
837         // expected.
838         match self.cmp(&other) {
839             Ordering::Less | Ordering::Equal => self,
840             Ordering::Greater => other,
841         }
842     }
843
844     /// Restrict a value to a certain interval.
845     ///
846     /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
847     /// less than `min`. Otherwise this returns `self`.
848     ///
849     /// # Panics
850     ///
851     /// Panics if `min > max`.
852     ///
853     /// # Examples
854     ///
855     /// ```
856     /// assert!((-3).clamp(-2, 1) == -2);
857     /// assert!(0.clamp(-2, 1) == 0);
858     /// assert!(2.clamp(-2, 1) == 1);
859     /// ```
860     #[must_use]
861     #[stable(feature = "clamp", since = "1.50.0")]
862     fn clamp(self, min: Self, max: Self) -> Self
863     where
864         Self: Sized,
865         Self: ~const Destruct,
866         Self: ~const PartialOrd,
867     {
868         assert!(min <= max);
869         if self < min {
870             min
871         } else if self > max {
872             max
873         } else {
874             self
875         }
876     }
877 }
878
879 /// Derive macro generating an impl of the trait `Ord`.
880 #[rustc_builtin_macro]
881 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
882 #[allow_internal_unstable(core_intrinsics)]
883 pub macro Ord($item:item) {
884     /* compiler built-in */
885 }
886
887 #[stable(feature = "rust1", since = "1.0.0")]
888 #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
889 impl const Ord for Ordering {
890     #[inline]
891     fn cmp(&self, other: &Ordering) -> Ordering {
892         (*self as i32).cmp(&(*other as i32))
893     }
894 }
895
896 #[stable(feature = "rust1", since = "1.0.0")]
897 #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
898 impl const PartialOrd for Ordering {
899     #[inline]
900     fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
901         (*self as i32).partial_cmp(&(*other as i32))
902     }
903 }
904
905 /// Trait for types that form a [partial order](https://en.wikipedia.org/wiki/Partial_order).
906 ///
907 /// The `lt`, `le`, `gt`, and `ge` methods of this trait can be called using
908 /// the `<`, `<=`, `>`, and `>=` operators, respectively.
909 ///
910 /// The methods of this trait must be consistent with each other and with those of [`PartialEq`].
911 /// The following conditions must hold:
912 ///
913 /// 1. `a == b` if and only if `partial_cmp(a, b) == Some(Equal)`.
914 /// 2. `a < b` if and only if `partial_cmp(a, b) == Some(Less)`
915 /// 3. `a > b` if and only if `partial_cmp(a, b) == Some(Greater)`
916 /// 4. `a <= b` if and only if `a < b || a == b`
917 /// 5. `a >= b` if and only if `a > b || a == b`
918 /// 6. `a != b` if and only if `!(a == b)`.
919 ///
920 /// Conditions 2–5 above are ensured by the default implementation.
921 /// Condition 6 is already ensured by [`PartialEq`].
922 ///
923 /// If [`Ord`] is also implemented for `Self` and `Rhs`, it must also be consistent with
924 /// `partial_cmp` (see the documentation of that trait for the exact requirements). It's
925 /// easy to accidentally make them disagree by deriving some of the traits and manually
926 /// implementing others.
927 ///
928 /// The comparison must satisfy, for all `a`, `b` and `c`:
929 ///
930 /// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
931 /// - duality: `a < b` if and only if `b > a`.
932 ///
933 /// Note that these requirements mean that the trait itself must be implemented symmetrically and
934 /// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T:
935 /// PartialOrd<V>`.
936 ///
937 /// ## Corollaries
938 ///
939 /// The following corollaries follow from the above requirements:
940 ///
941 /// - irreflexivity of `<` and `>`: `!(a < a)`, `!(a > a)`
942 /// - transitivity of `>`: if `a > b` and `b > c` then `a > c`
943 /// - duality of `partial_cmp`: `partial_cmp(a, b) == partial_cmp(b, a).map(Ordering::reverse)`
944 ///
945 /// ## Derivable
946 ///
947 /// This trait can be used with `#[derive]`.
948 ///
949 /// When `derive`d on structs, it will produce a
950 /// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering
951 /// based on the top-to-bottom declaration order of the struct's members.
952 ///
953 /// When `derive`d on enums, variants are ordered by their discriminants.
954 /// By default, the discriminant is smallest for variants at the top, and
955 /// largest for variants at the bottom. Here's an example:
956 ///
957 /// ```
958 /// #[derive(PartialEq, PartialOrd)]
959 /// enum E {
960 ///     Top,
961 ///     Bottom,
962 /// }
963 ///
964 /// assert!(E::Top < E::Bottom);
965 /// ```
966 ///
967 /// However, manually setting the discriminants can override this default
968 /// behavior:
969 ///
970 /// ```
971 /// #[derive(PartialEq, PartialOrd)]
972 /// enum E {
973 ///     Top = 2,
974 ///     Bottom = 1,
975 /// }
976 ///
977 /// assert!(E::Bottom < E::Top);
978 /// ```
979 ///
980 /// ## How can I implement `PartialOrd`?
981 ///
982 /// `PartialOrd` only requires implementation of the [`partial_cmp`] method, with the others
983 /// generated from default implementations.
984 ///
985 /// However it remains possible to implement the others separately for types which do not have a
986 /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 ==
987 /// false` (cf. IEEE 754-2008 section 5.11).
988 ///
989 /// `PartialOrd` requires your type to be [`PartialEq`].
990 ///
991 /// If your type is [`Ord`], you can implement [`partial_cmp`] by using [`cmp`]:
992 ///
993 /// ```
994 /// use std::cmp::Ordering;
995 ///
996 /// #[derive(Eq)]
997 /// struct Person {
998 ///     id: u32,
999 ///     name: String,
1000 ///     height: u32,
1001 /// }
1002 ///
1003 /// impl PartialOrd for Person {
1004 ///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1005 ///         Some(self.cmp(other))
1006 ///     }
1007 /// }
1008 ///
1009 /// impl Ord for Person {
1010 ///     fn cmp(&self, other: &Self) -> Ordering {
1011 ///         self.height.cmp(&other.height)
1012 ///     }
1013 /// }
1014 ///
1015 /// impl PartialEq for Person {
1016 ///     fn eq(&self, other: &Self) -> bool {
1017 ///         self.height == other.height
1018 ///     }
1019 /// }
1020 /// ```
1021 ///
1022 /// You may also find it useful to use [`partial_cmp`] on your type's fields. Here
1023 /// is an example of `Person` types who have a floating-point `height` field that
1024 /// is the only field to be used for sorting:
1025 ///
1026 /// ```
1027 /// use std::cmp::Ordering;
1028 ///
1029 /// struct Person {
1030 ///     id: u32,
1031 ///     name: String,
1032 ///     height: f64,
1033 /// }
1034 ///
1035 /// impl PartialOrd for Person {
1036 ///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1037 ///         self.height.partial_cmp(&other.height)
1038 ///     }
1039 /// }
1040 ///
1041 /// impl PartialEq for Person {
1042 ///     fn eq(&self, other: &Self) -> bool {
1043 ///         self.height == other.height
1044 ///     }
1045 /// }
1046 /// ```
1047 ///
1048 /// # Examples
1049 ///
1050 /// ```
1051 /// let x: u32 = 0;
1052 /// let y: u32 = 1;
1053 ///
1054 /// assert_eq!(x < y, true);
1055 /// assert_eq!(x.lt(&y), true);
1056 /// ```
1057 ///
1058 /// [`partial_cmp`]: PartialOrd::partial_cmp
1059 /// [`cmp`]: Ord::cmp
1060 #[lang = "partial_ord"]
1061 #[stable(feature = "rust1", since = "1.0.0")]
1062 #[doc(alias = ">")]
1063 #[doc(alias = "<")]
1064 #[doc(alias = "<=")]
1065 #[doc(alias = ">=")]
1066 #[cfg_attr(
1067     bootstrap,
1068     rustc_on_unimplemented(
1069         message = "can't compare `{Self}` with `{Rhs}`",
1070         label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
1071     )
1072 )]
1073 #[cfg_attr(
1074     not(bootstrap),
1075     rustc_on_unimplemented(
1076         message = "can't compare `{Self}` with `{Rhs}`",
1077         label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
1078         append_const_msg,
1079     )
1080 )]
1081 #[const_trait]
1082 #[rustc_diagnostic_item = "PartialOrd"]
1083 pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1084     /// This method returns an ordering between `self` and `other` values if one exists.
1085     ///
1086     /// # Examples
1087     ///
1088     /// ```
1089     /// use std::cmp::Ordering;
1090     ///
1091     /// let result = 1.0.partial_cmp(&2.0);
1092     /// assert_eq!(result, Some(Ordering::Less));
1093     ///
1094     /// let result = 1.0.partial_cmp(&1.0);
1095     /// assert_eq!(result, Some(Ordering::Equal));
1096     ///
1097     /// let result = 2.0.partial_cmp(&1.0);
1098     /// assert_eq!(result, Some(Ordering::Greater));
1099     /// ```
1100     ///
1101     /// When comparison is impossible:
1102     ///
1103     /// ```
1104     /// let result = f64::NAN.partial_cmp(&1.0);
1105     /// assert_eq!(result, None);
1106     /// ```
1107     #[must_use]
1108     #[stable(feature = "rust1", since = "1.0.0")]
1109     fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
1110
1111     /// This method tests less than (for `self` and `other`) and is used by the `<` operator.
1112     ///
1113     /// # Examples
1114     ///
1115     /// ```
1116     /// let result = 1.0 < 2.0;
1117     /// assert_eq!(result, true);
1118     ///
1119     /// let result = 2.0 < 1.0;
1120     /// assert_eq!(result, false);
1121     /// ```
1122     #[inline]
1123     #[must_use]
1124     #[stable(feature = "rust1", since = "1.0.0")]
1125     fn lt(&self, other: &Rhs) -> bool {
1126         matches!(self.partial_cmp(other), Some(Less))
1127     }
1128
1129     /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
1130     /// operator.
1131     ///
1132     /// # Examples
1133     ///
1134     /// ```
1135     /// let result = 1.0 <= 2.0;
1136     /// assert_eq!(result, true);
1137     ///
1138     /// let result = 2.0 <= 2.0;
1139     /// assert_eq!(result, true);
1140     /// ```
1141     #[inline]
1142     #[must_use]
1143     #[stable(feature = "rust1", since = "1.0.0")]
1144     fn le(&self, other: &Rhs) -> bool {
1145         matches!(self.partial_cmp(other), Some(Less | Equal))
1146     }
1147
1148     /// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
1149     ///
1150     /// # Examples
1151     ///
1152     /// ```
1153     /// let result = 1.0 > 2.0;
1154     /// assert_eq!(result, false);
1155     ///
1156     /// let result = 2.0 > 2.0;
1157     /// assert_eq!(result, false);
1158     /// ```
1159     #[inline]
1160     #[must_use]
1161     #[stable(feature = "rust1", since = "1.0.0")]
1162     fn gt(&self, other: &Rhs) -> bool {
1163         matches!(self.partial_cmp(other), Some(Greater))
1164     }
1165
1166     /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
1167     /// operator.
1168     ///
1169     /// # Examples
1170     ///
1171     /// ```
1172     /// let result = 2.0 >= 1.0;
1173     /// assert_eq!(result, true);
1174     ///
1175     /// let result = 2.0 >= 2.0;
1176     /// assert_eq!(result, true);
1177     /// ```
1178     #[inline]
1179     #[must_use]
1180     #[stable(feature = "rust1", since = "1.0.0")]
1181     fn ge(&self, other: &Rhs) -> bool {
1182         matches!(self.partial_cmp(other), Some(Greater | Equal))
1183     }
1184 }
1185
1186 /// Derive macro generating an impl of the trait `PartialOrd`.
1187 #[rustc_builtin_macro]
1188 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
1189 #[allow_internal_unstable(core_intrinsics)]
1190 pub macro PartialOrd($item:item) {
1191     /* compiler built-in */
1192 }
1193
1194 /// Compares and returns the minimum of two values.
1195 ///
1196 /// Returns the first argument if the comparison determines them to be equal.
1197 ///
1198 /// Internally uses an alias to [`Ord::min`].
1199 ///
1200 /// # Examples
1201 ///
1202 /// ```
1203 /// use std::cmp;
1204 ///
1205 /// assert_eq!(1, cmp::min(1, 2));
1206 /// assert_eq!(2, cmp::min(2, 2));
1207 /// ```
1208 #[inline]
1209 #[must_use]
1210 #[stable(feature = "rust1", since = "1.0.0")]
1211 #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1212 #[cfg_attr(not(test), rustc_diagnostic_item = "cmp_min")]
1213 pub const fn min<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
1214     v1.min(v2)
1215 }
1216
1217 /// Returns the minimum of two values with respect to the specified comparison function.
1218 ///
1219 /// Returns the first argument if the comparison determines them to be equal.
1220 ///
1221 /// # Examples
1222 ///
1223 /// ```
1224 /// use std::cmp;
1225 ///
1226 /// assert_eq!(cmp::min_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 1);
1227 /// assert_eq!(cmp::min_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
1228 /// ```
1229 #[inline]
1230 #[must_use]
1231 #[stable(feature = "cmp_min_max_by", since = "1.53.0")]
1232 pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1233     match compare(&v1, &v2) {
1234         Ordering::Less | Ordering::Equal => v1,
1235         Ordering::Greater => v2,
1236     }
1237 }
1238
1239 /// Returns the element that gives the minimum value from the specified function.
1240 ///
1241 /// Returns the first argument if the comparison determines them to be equal.
1242 ///
1243 /// # Examples
1244 ///
1245 /// ```
1246 /// use std::cmp;
1247 ///
1248 /// assert_eq!(cmp::min_by_key(-2, 1, |x: &i32| x.abs()), 1);
1249 /// assert_eq!(cmp::min_by_key(-2, 2, |x: &i32| x.abs()), -2);
1250 /// ```
1251 #[inline]
1252 #[must_use]
1253 #[stable(feature = "cmp_min_max_by", since = "1.53.0")]
1254 pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1255     min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
1256 }
1257
1258 /// Compares and returns the maximum of two values.
1259 ///
1260 /// Returns the second argument if the comparison determines them to be equal.
1261 ///
1262 /// Internally uses an alias to [`Ord::max`].
1263 ///
1264 /// # Examples
1265 ///
1266 /// ```
1267 /// use std::cmp;
1268 ///
1269 /// assert_eq!(2, cmp::max(1, 2));
1270 /// assert_eq!(2, cmp::max(2, 2));
1271 /// ```
1272 #[inline]
1273 #[must_use]
1274 #[stable(feature = "rust1", since = "1.0.0")]
1275 #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1276 #[cfg_attr(not(test), rustc_diagnostic_item = "cmp_max")]
1277 pub const fn max<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
1278     v1.max(v2)
1279 }
1280
1281 /// Returns the maximum of two values with respect to the specified comparison function.
1282 ///
1283 /// Returns the second argument if the comparison determines them to be equal.
1284 ///
1285 /// # Examples
1286 ///
1287 /// ```
1288 /// use std::cmp;
1289 ///
1290 /// assert_eq!(cmp::max_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
1291 /// assert_eq!(cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 2);
1292 /// ```
1293 #[inline]
1294 #[must_use]
1295 #[stable(feature = "cmp_min_max_by", since = "1.53.0")]
1296 pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1297     match compare(&v1, &v2) {
1298         Ordering::Less | Ordering::Equal => v2,
1299         Ordering::Greater => v1,
1300     }
1301 }
1302
1303 /// Returns the element that gives the maximum value from the specified function.
1304 ///
1305 /// Returns the second argument if the comparison determines them to be equal.
1306 ///
1307 /// # Examples
1308 ///
1309 /// ```
1310 /// use std::cmp;
1311 ///
1312 /// assert_eq!(cmp::max_by_key(-2, 1, |x: &i32| x.abs()), -2);
1313 /// assert_eq!(cmp::max_by_key(-2, 2, |x: &i32| x.abs()), 2);
1314 /// ```
1315 #[inline]
1316 #[must_use]
1317 #[stable(feature = "cmp_min_max_by", since = "1.53.0")]
1318 pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1319     max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
1320 }
1321
1322 // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
1323 mod impls {
1324     use crate::cmp::Ordering::{self, Equal, Greater, Less};
1325     use crate::hint::unreachable_unchecked;
1326
1327     macro_rules! partial_eq_impl {
1328         ($($t:ty)*) => ($(
1329             #[stable(feature = "rust1", since = "1.0.0")]
1330             #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1331             impl const PartialEq for $t {
1332                 #[inline]
1333                 fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
1334                 #[inline]
1335                 fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
1336             }
1337         )*)
1338     }
1339
1340     #[stable(feature = "rust1", since = "1.0.0")]
1341     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1342     impl const PartialEq for () {
1343         #[inline]
1344         fn eq(&self, _other: &()) -> bool {
1345             true
1346         }
1347         #[inline]
1348         fn ne(&self, _other: &()) -> bool {
1349             false
1350         }
1351     }
1352
1353     partial_eq_impl! {
1354         bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64
1355     }
1356
1357     macro_rules! eq_impl {
1358         ($($t:ty)*) => ($(
1359             #[stable(feature = "rust1", since = "1.0.0")]
1360             impl Eq for $t {}
1361         )*)
1362     }
1363
1364     eq_impl! { () bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
1365
1366     macro_rules! partial_ord_impl {
1367         ($($t:ty)*) => ($(
1368             #[stable(feature = "rust1", since = "1.0.0")]
1369             #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1370             impl const PartialOrd for $t {
1371                 #[inline]
1372                 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
1373                     match (*self <= *other, *self >= *other) {
1374                         (false, false) => None,
1375                         (false, true) => Some(Greater),
1376                         (true, false) => Some(Less),
1377                         (true, true) => Some(Equal),
1378                     }
1379                 }
1380                 #[inline]
1381                 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
1382                 #[inline]
1383                 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
1384                 #[inline]
1385                 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
1386                 #[inline]
1387                 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
1388             }
1389         )*)
1390     }
1391
1392     #[stable(feature = "rust1", since = "1.0.0")]
1393     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1394     impl const PartialOrd for () {
1395         #[inline]
1396         fn partial_cmp(&self, _: &()) -> Option<Ordering> {
1397             Some(Equal)
1398         }
1399     }
1400
1401     #[stable(feature = "rust1", since = "1.0.0")]
1402     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1403     impl const PartialOrd for bool {
1404         #[inline]
1405         fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
1406             Some(self.cmp(other))
1407         }
1408     }
1409
1410     partial_ord_impl! { f32 f64 }
1411
1412     macro_rules! ord_impl {
1413         ($($t:ty)*) => ($(
1414             #[stable(feature = "rust1", since = "1.0.0")]
1415             #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1416             impl const PartialOrd for $t {
1417                 #[inline]
1418                 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
1419                     Some(self.cmp(other))
1420                 }
1421                 #[inline]
1422                 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
1423                 #[inline]
1424                 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
1425                 #[inline]
1426                 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
1427                 #[inline]
1428                 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
1429             }
1430
1431             #[stable(feature = "rust1", since = "1.0.0")]
1432             #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1433             impl const Ord for $t {
1434                 #[inline]
1435                 fn cmp(&self, other: &$t) -> Ordering {
1436                     // The order here is important to generate more optimal assembly.
1437                     // See <https://github.com/rust-lang/rust/issues/63758> for more info.
1438                     if *self < *other { Less }
1439                     else if *self == *other { Equal }
1440                     else { Greater }
1441                 }
1442             }
1443         )*)
1444     }
1445
1446     #[stable(feature = "rust1", since = "1.0.0")]
1447     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1448     impl const Ord for () {
1449         #[inline]
1450         fn cmp(&self, _other: &()) -> Ordering {
1451             Equal
1452         }
1453     }
1454
1455     #[stable(feature = "rust1", since = "1.0.0")]
1456     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1457     impl const Ord for bool {
1458         #[inline]
1459         fn cmp(&self, other: &bool) -> Ordering {
1460             // Casting to i8's and converting the difference to an Ordering generates
1461             // more optimal assembly.
1462             // See <https://github.com/rust-lang/rust/issues/66780> for more info.
1463             match (*self as i8) - (*other as i8) {
1464                 -1 => Less,
1465                 0 => Equal,
1466                 1 => Greater,
1467                 // SAFETY: bool as i8 returns 0 or 1, so the difference can't be anything else
1468                 _ => unsafe { unreachable_unchecked() },
1469             }
1470         }
1471     }
1472
1473     ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
1474
1475     #[unstable(feature = "never_type", issue = "35121")]
1476     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1477     impl const PartialEq for ! {
1478         fn eq(&self, _: &!) -> bool {
1479             *self
1480         }
1481     }
1482
1483     #[unstable(feature = "never_type", issue = "35121")]
1484     impl Eq for ! {}
1485
1486     #[unstable(feature = "never_type", issue = "35121")]
1487     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1488     impl const PartialOrd for ! {
1489         fn partial_cmp(&self, _: &!) -> Option<Ordering> {
1490             *self
1491         }
1492     }
1493
1494     #[unstable(feature = "never_type", issue = "35121")]
1495     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1496     impl const Ord for ! {
1497         fn cmp(&self, _: &!) -> Ordering {
1498             *self
1499         }
1500     }
1501
1502     // & pointers
1503
1504     #[stable(feature = "rust1", since = "1.0.0")]
1505     #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1506     impl<A: ?Sized, B: ?Sized> const PartialEq<&B> for &A
1507     where
1508         A: ~const PartialEq<B>,
1509     {
1510         #[inline]
1511         fn eq(&self, other: &&B) -> bool {
1512             PartialEq::eq(*self, *other)
1513         }
1514         #[inline]
1515         fn ne(&self, other: &&B) -> bool {
1516             PartialEq::ne(*self, *other)
1517         }
1518     }
1519     #[stable(feature = "rust1", since = "1.0.0")]
1520     impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
1521     where
1522         A: PartialOrd<B>,
1523     {
1524         #[inline]
1525         fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
1526             PartialOrd::partial_cmp(*self, *other)
1527         }
1528         #[inline]
1529         fn lt(&self, other: &&B) -> bool {
1530             PartialOrd::lt(*self, *other)
1531         }
1532         #[inline]
1533         fn le(&self, other: &&B) -> bool {
1534             PartialOrd::le(*self, *other)
1535         }
1536         #[inline]
1537         fn gt(&self, other: &&B) -> bool {
1538             PartialOrd::gt(*self, *other)
1539         }
1540         #[inline]
1541         fn ge(&self, other: &&B) -> bool {
1542             PartialOrd::ge(*self, *other)
1543         }
1544     }
1545     #[stable(feature = "rust1", since = "1.0.0")]
1546     impl<A: ?Sized> Ord for &A
1547     where
1548         A: Ord,
1549     {
1550         #[inline]
1551         fn cmp(&self, other: &Self) -> Ordering {
1552             Ord::cmp(*self, *other)
1553         }
1554     }
1555     #[stable(feature = "rust1", since = "1.0.0")]
1556     impl<A: ?Sized> Eq for &A where A: Eq {}
1557
1558     // &mut pointers
1559
1560     #[stable(feature = "rust1", since = "1.0.0")]
1561     impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A
1562     where
1563         A: PartialEq<B>,
1564     {
1565         #[inline]
1566         fn eq(&self, other: &&mut B) -> bool {
1567             PartialEq::eq(*self, *other)
1568         }
1569         #[inline]
1570         fn ne(&self, other: &&mut B) -> bool {
1571             PartialEq::ne(*self, *other)
1572         }
1573     }
1574     #[stable(feature = "rust1", since = "1.0.0")]
1575     impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A
1576     where
1577         A: PartialOrd<B>,
1578     {
1579         #[inline]
1580         fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
1581             PartialOrd::partial_cmp(*self, *other)
1582         }
1583         #[inline]
1584         fn lt(&self, other: &&mut B) -> bool {
1585             PartialOrd::lt(*self, *other)
1586         }
1587         #[inline]
1588         fn le(&self, other: &&mut B) -> bool {
1589             PartialOrd::le(*self, *other)
1590         }
1591         #[inline]
1592         fn gt(&self, other: &&mut B) -> bool {
1593             PartialOrd::gt(*self, *other)
1594         }
1595         #[inline]
1596         fn ge(&self, other: &&mut B) -> bool {
1597             PartialOrd::ge(*self, *other)
1598         }
1599     }
1600     #[stable(feature = "rust1", since = "1.0.0")]
1601     impl<A: ?Sized> Ord for &mut A
1602     where
1603         A: Ord,
1604     {
1605         #[inline]
1606         fn cmp(&self, other: &Self) -> Ordering {
1607             Ord::cmp(*self, *other)
1608         }
1609     }
1610     #[stable(feature = "rust1", since = "1.0.0")]
1611     impl<A: ?Sized> Eq for &mut A where A: Eq {}
1612
1613     #[stable(feature = "rust1", since = "1.0.0")]
1614     impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A
1615     where
1616         A: PartialEq<B>,
1617     {
1618         #[inline]
1619         fn eq(&self, other: &&mut B) -> bool {
1620             PartialEq::eq(*self, *other)
1621         }
1622         #[inline]
1623         fn ne(&self, other: &&mut B) -> bool {
1624             PartialEq::ne(*self, *other)
1625         }
1626     }
1627
1628     #[stable(feature = "rust1", since = "1.0.0")]
1629     impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A
1630     where
1631         A: PartialEq<B>,
1632     {
1633         #[inline]
1634         fn eq(&self, other: &&B) -> bool {
1635             PartialEq::eq(*self, *other)
1636         }
1637         #[inline]
1638         fn ne(&self, other: &&B) -> bool {
1639             PartialEq::ne(*self, *other)
1640         }
1641     }
1642 }