]> git.lizzy.rs Git - rust.git/blob - src/libcore/cmp.rs
Auto merge of #68689 - estebank:where-clause-sugg-missing-fn, r=varkor
[rust.git] / src / libcore / cmp.rs
1 //! Functionality for ordering and comparison.
2 //!
3 //! This module contains various tools for ordering and comparing 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 //! [`Eq`]: trait.Eq.html
21 //! [`PartialEq`]: trait.PartialEq.html
22 //! [`Ord`]: trait.Ord.html
23 //! [`PartialOrd`]: trait.PartialOrd.html
24 //! [`Ordering`]: enum.Ordering.html
25 //! [`Reverse`]: struct.Reverse.html
26 //! [`max`]: fn.max.html
27 //! [`min`]: fn.min.html
28
29 #![stable(feature = "rust1", since = "1.0.0")]
30
31 use self::Ordering::*;
32
33 /// Trait for equality comparisons which are [partial equivalence
34 /// relations](http://en.wikipedia.org/wiki/Partial_equivalence_relation).
35 ///
36 /// This trait allows for partial equality, for types that do not have a full
37 /// equivalence relation. For example, in floating point numbers `NaN != NaN`,
38 /// so floating point types implement `PartialEq` but not [`Eq`].
39 ///
40 /// Formally, the equality must be (for all `a`, `b` and `c`):
41 ///
42 /// - symmetric: `a == b` implies `b == a`; and
43 /// - transitive: `a == b` and `b == c` implies `a == c`.
44 ///
45 /// Note that these requirements mean that the trait itself must be implemented
46 /// symmetrically and transitively: if `T: PartialEq<U>` and `U: PartialEq<V>`
47 /// then `U: PartialEq<T>` and `T: PartialEq<V>`.
48 ///
49 /// ## Derivable
50 ///
51 /// This trait can be used with `#[derive]`. When `derive`d on structs, two
52 /// instances are equal if all fields are equal, and not equal if any fields
53 /// are not equal. When `derive`d on enums, each variant is equal to itself
54 /// and not equal to the other variants.
55 ///
56 /// ## How can I implement `PartialEq`?
57 ///
58 /// `PartialEq` only requires the [`eq`] method to be implemented; [`ne`] is defined
59 /// in terms of it by default. Any manual implementation of [`ne`] *must* respect
60 /// the rule that [`eq`] is a strict inverse of [`ne`]; that is, `!(a == b)` if and
61 /// only if `a != b`.
62 ///
63 /// Implementations of `PartialEq`, [`PartialOrd`], and [`Ord`] *must* agree with
64 /// each other. It's easy to accidentally make them disagree by deriving some
65 /// of the traits and manually implementing others.
66 ///
67 /// An example implementation for a domain in which two books are considered
68 /// the same book if their ISBN matches, even if the formats differ:
69 ///
70 /// ```
71 /// enum BookFormat {
72 ///     Paperback,
73 ///     Hardback,
74 ///     Ebook,
75 /// }
76 ///
77 /// struct Book {
78 ///     isbn: i32,
79 ///     format: BookFormat,
80 /// }
81 ///
82 /// impl PartialEq for Book {
83 ///     fn eq(&self, other: &Self) -> bool {
84 ///         self.isbn == other.isbn
85 ///     }
86 /// }
87 ///
88 /// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
89 /// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
90 /// let b3 = Book { isbn: 10, format: BookFormat::Paperback };
91 ///
92 /// assert!(b1 == b2);
93 /// assert!(b1 != b3);
94 /// ```
95 ///
96 /// ## How can I compare two different types?
97 ///
98 /// The type you can compare with is controlled by `PartialEq`'s type parameter.
99 /// For example, let's tweak our previous code a bit:
100 ///
101 /// ```
102 /// // The derive implements <BookFormat> == <BookFormat> comparisons
103 /// #[derive(PartialEq)]
104 /// enum BookFormat {
105 ///     Paperback,
106 ///     Hardback,
107 ///     Ebook,
108 /// }
109 ///
110 /// struct Book {
111 ///     isbn: i32,
112 ///     format: BookFormat,
113 /// }
114 ///
115 /// // Implement <Book> == <BookFormat> comparisons
116 /// impl PartialEq<BookFormat> for Book {
117 ///     fn eq(&self, other: &BookFormat) -> bool {
118 ///         self.format == *other
119 ///     }
120 /// }
121 ///
122 /// // Implement <BookFormat> == <Book> comparisons
123 /// impl PartialEq<Book> for BookFormat {
124 ///     fn eq(&self, other: &Book) -> bool {
125 ///         *self == other.format
126 ///     }
127 /// }
128 ///
129 /// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
130 ///
131 /// assert!(b1 == BookFormat::Paperback);
132 /// assert!(BookFormat::Ebook != b1);
133 /// ```
134 ///
135 /// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`,
136 /// we allow `BookFormat`s to be compared with `Book`s.
137 ///
138 /// A comparison like the one above, which ignores some fields of the struct,
139 /// can be dangerous. It can easily lead to an unintended violation of the
140 /// requirements for a partial equivalence relation. For example, if we kept
141 /// the above implementation of `PartialEq<Book>` for `BookFormat` and added an
142 /// implementation of `PartialEq<Book>` for `Book` (either via a `#[derive]` or
143 /// via the manual implementation from the first example) then the result would
144 /// violate transitivity:
145 ///
146 /// ```should_panic
147 /// #[derive(PartialEq)]
148 /// enum BookFormat {
149 ///     Paperback,
150 ///     Hardback,
151 ///     Ebook,
152 /// }
153 ///
154 /// #[derive(PartialEq)]
155 /// struct Book {
156 ///     isbn: i32,
157 ///     format: BookFormat,
158 /// }
159 ///
160 /// impl PartialEq<BookFormat> for Book {
161 ///     fn eq(&self, other: &BookFormat) -> bool {
162 ///         self.format == *other
163 ///     }
164 /// }
165 ///
166 /// impl PartialEq<Book> for BookFormat {
167 ///     fn eq(&self, other: &Book) -> bool {
168 ///         *self == other.format
169 ///     }
170 /// }
171 ///
172 /// fn main() {
173 ///     let b1 = Book { isbn: 1, format: BookFormat::Paperback };
174 ///     let b2 = Book { isbn: 2, format: BookFormat::Paperback };
175 ///
176 ///     assert!(b1 == BookFormat::Paperback);
177 ///     assert!(BookFormat::Paperback == b2);
178 ///
179 ///     // The following should hold by transitivity but doesn't.
180 ///     assert!(b1 == b2); // <-- PANICS
181 /// }
182 /// ```
183 ///
184 /// # Examples
185 ///
186 /// ```
187 /// let x: u32 = 0;
188 /// let y: u32 = 1;
189 ///
190 /// assert_eq!(x == y, false);
191 /// assert_eq!(x.eq(&y), false);
192 /// ```
193 ///
194 /// [`eq`]: PartialEq::eq
195 /// [`ne`]: PartialEq::ne
196 #[lang = "eq"]
197 #[stable(feature = "rust1", since = "1.0.0")]
198 #[doc(alias = "==")]
199 #[doc(alias = "!=")]
200 #[rustc_on_unimplemented(
201     message = "can't compare `{Self}` with `{Rhs}`",
202     label = "no implementation for `{Self} == {Rhs}`"
203 )]
204 pub trait PartialEq<Rhs: ?Sized = Self> {
205     /// This method tests for `self` and `other` values to be equal, and is used
206     /// by `==`.
207     #[must_use]
208     #[stable(feature = "rust1", since = "1.0.0")]
209     fn eq(&self, other: &Rhs) -> bool;
210
211     /// This method tests for `!=`.
212     #[inline]
213     #[must_use]
214     #[stable(feature = "rust1", since = "1.0.0")]
215     fn ne(&self, other: &Rhs) -> bool {
216         !self.eq(other)
217     }
218 }
219
220 /// Derive macro generating an impl of the trait `PartialEq`.
221 #[rustc_builtin_macro]
222 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
223 #[allow_internal_unstable(core_intrinsics, structural_match)]
224 pub macro PartialEq($item:item) {
225     /* compiler built-in */
226 }
227
228 /// Trait for equality comparisons which are [equivalence relations](
229 /// https://en.wikipedia.org/wiki/Equivalence_relation).
230 ///
231 /// This means, that in addition to `a == b` and `a != b` being strict inverses, the equality must
232 /// be (for all `a`, `b` and `c`):
233 ///
234 /// - reflexive: `a == a`;
235 /// - symmetric: `a == b` implies `b == a`; and
236 /// - transitive: `a == b` and `b == c` implies `a == c`.
237 ///
238 /// This property cannot be checked by the compiler, and therefore `Eq` implies
239 /// [`PartialEq`], and has no extra methods.
240 ///
241 /// ## Derivable
242 ///
243 /// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has
244 /// no extra methods, it is only informing the compiler that this is an
245 /// equivalence relation rather than a partial equivalence relation. Note that
246 /// the `derive` strategy requires all fields are `Eq`, which isn't
247 /// always desired.
248 ///
249 /// ## How can I implement `Eq`?
250 ///
251 /// If you cannot use the `derive` strategy, specify that your type implements
252 /// `Eq`, which has no methods:
253 ///
254 /// ```
255 /// enum BookFormat { Paperback, Hardback, Ebook }
256 /// struct Book {
257 ///     isbn: i32,
258 ///     format: BookFormat,
259 /// }
260 /// impl PartialEq for Book {
261 ///     fn eq(&self, other: &Self) -> bool {
262 ///         self.isbn == other.isbn
263 ///     }
264 /// }
265 /// impl Eq for Book {}
266 /// ```
267 #[doc(alias = "==")]
268 #[doc(alias = "!=")]
269 #[stable(feature = "rust1", since = "1.0.0")]
270 pub trait Eq: PartialEq<Self> {
271     // this method is used solely by #[deriving] to assert
272     // that every component of a type implements #[deriving]
273     // itself, the current deriving infrastructure means doing this
274     // assertion without using a method on this trait is nearly
275     // impossible.
276     //
277     // This should never be implemented by hand.
278     #[doc(hidden)]
279     #[inline]
280     #[stable(feature = "rust1", since = "1.0.0")]
281     fn assert_receiver_is_total_eq(&self) {}
282 }
283
284 /// Derive macro generating an impl of the trait `Eq`.
285 #[rustc_builtin_macro]
286 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
287 #[allow_internal_unstable(core_intrinsics, derive_eq, structural_match)]
288 pub macro Eq($item:item) {
289     /* compiler built-in */
290 }
291
292 // FIXME: this struct is used solely by #[derive] to
293 // assert that every component of a type implements Eq.
294 //
295 // This struct should never appear in user code.
296 #[doc(hidden)]
297 #[allow(missing_debug_implementations)]
298 #[unstable(feature = "derive_eq", reason = "deriving hack, should not be public", issue = "none")]
299 pub struct AssertParamIsEq<T: Eq + ?Sized> {
300     _field: crate::marker::PhantomData<T>,
301 }
302
303 /// An `Ordering` is the result of a comparison between two values.
304 ///
305 /// # Examples
306 ///
307 /// ```
308 /// use std::cmp::Ordering;
309 ///
310 /// let result = 1.cmp(&2);
311 /// assert_eq!(Ordering::Less, result);
312 ///
313 /// let result = 1.cmp(&1);
314 /// assert_eq!(Ordering::Equal, result);
315 ///
316 /// let result = 2.cmp(&1);
317 /// assert_eq!(Ordering::Greater, result);
318 /// ```
319 #[derive(Clone, Copy, PartialEq, Debug, Hash)]
320 #[stable(feature = "rust1", since = "1.0.0")]
321 pub enum Ordering {
322     /// An ordering where a compared value is less than another.
323     #[stable(feature = "rust1", since = "1.0.0")]
324     Less = -1,
325     /// An ordering where a compared value is equal to another.
326     #[stable(feature = "rust1", since = "1.0.0")]
327     Equal = 0,
328     /// An ordering where a compared value is greater than another.
329     #[stable(feature = "rust1", since = "1.0.0")]
330     Greater = 1,
331 }
332
333 impl Ordering {
334     /// Reverses the `Ordering`.
335     ///
336     /// * `Less` becomes `Greater`.
337     /// * `Greater` becomes `Less`.
338     /// * `Equal` becomes `Equal`.
339     ///
340     /// # Examples
341     ///
342     /// Basic behavior:
343     ///
344     /// ```
345     /// use std::cmp::Ordering;
346     ///
347     /// assert_eq!(Ordering::Less.reverse(), Ordering::Greater);
348     /// assert_eq!(Ordering::Equal.reverse(), Ordering::Equal);
349     /// assert_eq!(Ordering::Greater.reverse(), Ordering::Less);
350     /// ```
351     ///
352     /// This method can be used to reverse a comparison:
353     ///
354     /// ```
355     /// let data: &mut [_] = &mut [2, 10, 5, 8];
356     ///
357     /// // sort the array from largest to smallest.
358     /// data.sort_by(|a, b| a.cmp(b).reverse());
359     ///
360     /// let b: &mut [_] = &mut [10, 8, 5, 2];
361     /// assert!(data == b);
362     /// ```
363     #[inline]
364     #[stable(feature = "rust1", since = "1.0.0")]
365     pub fn reverse(self) -> Ordering {
366         match self {
367             Less => Greater,
368             Equal => Equal,
369             Greater => Less,
370         }
371     }
372
373     /// Chains two orderings.
374     ///
375     /// Returns `self` when it's not `Equal`. Otherwise returns `other`.
376     ///
377     /// # Examples
378     ///
379     /// ```
380     /// use std::cmp::Ordering;
381     ///
382     /// let result = Ordering::Equal.then(Ordering::Less);
383     /// assert_eq!(result, Ordering::Less);
384     ///
385     /// let result = Ordering::Less.then(Ordering::Equal);
386     /// assert_eq!(result, Ordering::Less);
387     ///
388     /// let result = Ordering::Less.then(Ordering::Greater);
389     /// assert_eq!(result, Ordering::Less);
390     ///
391     /// let result = Ordering::Equal.then(Ordering::Equal);
392     /// assert_eq!(result, Ordering::Equal);
393     ///
394     /// let x: (i64, i64, i64) = (1, 2, 7);
395     /// let y: (i64, i64, i64) = (1, 5, 3);
396     /// let result = x.0.cmp(&y.0).then(x.1.cmp(&y.1)).then(x.2.cmp(&y.2));
397     ///
398     /// assert_eq!(result, Ordering::Less);
399     /// ```
400     #[inline]
401     #[stable(feature = "ordering_chaining", since = "1.17.0")]
402     pub fn then(self, other: Ordering) -> Ordering {
403         match self {
404             Equal => other,
405             _ => self,
406         }
407     }
408
409     /// Chains the ordering with the given function.
410     ///
411     /// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
412     /// the result.
413     ///
414     /// # Examples
415     ///
416     /// ```
417     /// use std::cmp::Ordering;
418     ///
419     /// let result = Ordering::Equal.then_with(|| Ordering::Less);
420     /// assert_eq!(result, Ordering::Less);
421     ///
422     /// let result = Ordering::Less.then_with(|| Ordering::Equal);
423     /// assert_eq!(result, Ordering::Less);
424     ///
425     /// let result = Ordering::Less.then_with(|| Ordering::Greater);
426     /// assert_eq!(result, Ordering::Less);
427     ///
428     /// let result = Ordering::Equal.then_with(|| Ordering::Equal);
429     /// assert_eq!(result, Ordering::Equal);
430     ///
431     /// let x: (i64, i64, i64) = (1, 2, 7);
432     /// let y: (i64, i64, i64)  = (1, 5, 3);
433     /// let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));
434     ///
435     /// assert_eq!(result, Ordering::Less);
436     /// ```
437     #[inline]
438     #[stable(feature = "ordering_chaining", since = "1.17.0")]
439     pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
440         match self {
441             Equal => f(),
442             _ => self,
443         }
444     }
445 }
446
447 /// A helper struct for reverse ordering.
448 ///
449 /// This struct is a helper to be used with functions like [`Vec::sort_by_key`] and
450 /// can be used to reverse order a part of a key.
451 ///
452 /// [`Vec::sort_by_key`]: ../../std/vec/struct.Vec.html#method.sort_by_key
453 ///
454 /// # Examples
455 ///
456 /// ```
457 /// use std::cmp::Reverse;
458 ///
459 /// let mut v = vec![1, 2, 3, 4, 5, 6];
460 /// v.sort_by_key(|&num| (num > 3, Reverse(num)));
461 /// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
462 /// ```
463 #[derive(PartialEq, Eq, Debug, Copy, Clone, Default, Hash)]
464 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
465 pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
466
467 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
468 impl<T: PartialOrd> PartialOrd for Reverse<T> {
469     #[inline]
470     fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
471         other.0.partial_cmp(&self.0)
472     }
473
474     #[inline]
475     fn lt(&self, other: &Self) -> bool {
476         other.0 < self.0
477     }
478     #[inline]
479     fn le(&self, other: &Self) -> bool {
480         other.0 <= self.0
481     }
482     #[inline]
483     fn gt(&self, other: &Self) -> bool {
484         other.0 > self.0
485     }
486     #[inline]
487     fn ge(&self, other: &Self) -> bool {
488         other.0 >= self.0
489     }
490 }
491
492 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
493 impl<T: Ord> Ord for Reverse<T> {
494     #[inline]
495     fn cmp(&self, other: &Reverse<T>) -> Ordering {
496         other.0.cmp(&self.0)
497     }
498 }
499
500 /// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
501 ///
502 /// An order is a total order if it is (for all `a`, `b` and `c`):
503 ///
504 /// - total and asymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and
505 /// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
506 ///
507 /// ## Derivable
508 ///
509 /// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
510 /// lexicographic ordering based on the top-to-bottom declaration order of the struct's members.
511 /// When `derive`d on enums, variants are ordered by their top-to-bottom declaration order.
512 ///
513 /// ## How can I implement `Ord`?
514 ///
515 /// `Ord` requires that the type also be [`PartialOrd`] and [`Eq`] (which requires [`PartialEq`]).
516 ///
517 /// Then you must define an implementation for [`cmp`]. You may find it useful to use
518 /// [`cmp`] on your type's fields.
519 ///
520 /// Implementations of [`PartialEq`], [`PartialOrd`], and `Ord` *must*
521 /// agree with each other. That is, `a.cmp(b) == Ordering::Equal` if
522 /// and only if `a == b` and `Some(a.cmp(b)) == a.partial_cmp(b)` for
523 /// all `a` and `b`. It's easy to accidentally make them disagree by
524 /// deriving some of the traits and manually implementing others.
525 ///
526 /// Here's an example where you want to sort people by height only, disregarding `id`
527 /// and `name`:
528 ///
529 /// ```
530 /// use std::cmp::Ordering;
531 ///
532 /// #[derive(Eq)]
533 /// struct Person {
534 ///     id: u32,
535 ///     name: String,
536 ///     height: u32,
537 /// }
538 ///
539 /// impl Ord for Person {
540 ///     fn cmp(&self, other: &Self) -> Ordering {
541 ///         self.height.cmp(&other.height)
542 ///     }
543 /// }
544 ///
545 /// impl PartialOrd for Person {
546 ///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
547 ///         Some(self.cmp(other))
548 ///     }
549 /// }
550 ///
551 /// impl PartialEq for Person {
552 ///     fn eq(&self, other: &Self) -> bool {
553 ///         self.height == other.height
554 ///     }
555 /// }
556 /// ```
557 ///
558 /// [`cmp`]: Ord::cmp
559 #[doc(alias = "<")]
560 #[doc(alias = ">")]
561 #[doc(alias = "<=")]
562 #[doc(alias = ">=")]
563 #[stable(feature = "rust1", since = "1.0.0")]
564 pub trait Ord: Eq + PartialOrd<Self> {
565     /// This method returns an [`Ordering`] between `self` and `other`.
566     ///
567     /// By convention, `self.cmp(&other)` returns the ordering matching the expression
568     /// `self <operator> other` if true.
569     ///
570     /// # Examples
571     ///
572     /// ```
573     /// use std::cmp::Ordering;
574     ///
575     /// assert_eq!(5.cmp(&10), Ordering::Less);
576     /// assert_eq!(10.cmp(&5), Ordering::Greater);
577     /// assert_eq!(5.cmp(&5), Ordering::Equal);
578     /// ```
579     #[stable(feature = "rust1", since = "1.0.0")]
580     fn cmp(&self, other: &Self) -> Ordering;
581
582     /// Compares and returns the maximum of two values.
583     ///
584     /// Returns the second argument if the comparison determines them to be equal.
585     ///
586     /// # Examples
587     ///
588     /// ```
589     /// assert_eq!(2, 1.max(2));
590     /// assert_eq!(2, 2.max(2));
591     /// ```
592     #[stable(feature = "ord_max_min", since = "1.21.0")]
593     #[inline]
594     fn max(self, other: Self) -> Self
595     where
596         Self: Sized,
597     {
598         max_by(self, other, Ord::cmp)
599     }
600
601     /// Compares and returns the minimum of two values.
602     ///
603     /// Returns the first argument if the comparison determines them to be equal.
604     ///
605     /// # Examples
606     ///
607     /// ```
608     /// assert_eq!(1, 1.min(2));
609     /// assert_eq!(2, 2.min(2));
610     /// ```
611     #[stable(feature = "ord_max_min", since = "1.21.0")]
612     #[inline]
613     fn min(self, other: Self) -> Self
614     where
615         Self: Sized,
616     {
617         min_by(self, other, Ord::cmp)
618     }
619
620     /// Restrict a value to a certain interval.
621     ///
622     /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
623     /// less than `min`. Otherwise this returns `self`.
624     ///
625     /// # Panics
626     ///
627     /// Panics if `min > max`.
628     ///
629     /// # Examples
630     ///
631     /// ```
632     /// #![feature(clamp)]
633     ///
634     /// assert!((-3).clamp(-2, 1) == -2);
635     /// assert!(0.clamp(-2, 1) == 0);
636     /// assert!(2.clamp(-2, 1) == 1);
637     /// ```
638     #[unstable(feature = "clamp", issue = "44095")]
639     fn clamp(self, min: Self, max: Self) -> Self
640     where
641         Self: Sized,
642     {
643         assert!(min <= max);
644         if self < min {
645             min
646         } else if self > max {
647             max
648         } else {
649             self
650         }
651     }
652 }
653
654 /// Derive macro generating an impl of the trait `Ord`.
655 #[rustc_builtin_macro]
656 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
657 #[allow_internal_unstable(core_intrinsics)]
658 pub macro Ord($item:item) {
659     /* compiler built-in */
660 }
661
662 #[stable(feature = "rust1", since = "1.0.0")]
663 impl Eq for Ordering {}
664
665 #[stable(feature = "rust1", since = "1.0.0")]
666 impl Ord for Ordering {
667     #[inline]
668     fn cmp(&self, other: &Ordering) -> Ordering {
669         (*self as i32).cmp(&(*other as i32))
670     }
671 }
672
673 #[stable(feature = "rust1", since = "1.0.0")]
674 impl PartialOrd for Ordering {
675     #[inline]
676     fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
677         (*self as i32).partial_cmp(&(*other as i32))
678     }
679 }
680
681 /// Trait for values that can be compared for a sort-order.
682 ///
683 /// The comparison must satisfy, for all `a`, `b` and `c`:
684 ///
685 /// - asymmetry: if `a < b` then `!(a > b)`, as well as `a > b` implying `!(a < b)`; and
686 /// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
687 ///
688 /// Note that these requirements mean that the trait itself must be implemented symmetrically and
689 /// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T:
690 /// PartialOrd<V>`.
691 ///
692 /// ## Derivable
693 ///
694 /// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
695 /// lexicographic ordering based on the top-to-bottom declaration order of the struct's members.
696 /// When `derive`d on enums, variants are ordered by their top-to-bottom declaration order.
697 ///
698 /// ## How can I implement `PartialOrd`?
699 ///
700 /// `PartialOrd` only requires implementation of the [`partial_cmp`] method, with the others
701 /// generated from default implementations.
702 ///
703 /// However it remains possible to implement the others separately for types which do not have a
704 /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 ==
705 /// false` (cf. IEEE 754-2008 section 5.11).
706 ///
707 /// `PartialOrd` requires your type to be [`PartialEq`].
708 ///
709 /// Implementations of [`PartialEq`], `PartialOrd`, and [`Ord`] *must* agree with each other. It's
710 /// easy to accidentally make them disagree by deriving some of the traits and manually
711 /// implementing others.
712 ///
713 /// If your type is [`Ord`], you can implement [`partial_cmp`] by using [`cmp`]:
714 ///
715 /// ```
716 /// use std::cmp::Ordering;
717 ///
718 /// #[derive(Eq)]
719 /// struct Person {
720 ///     id: u32,
721 ///     name: String,
722 ///     height: u32,
723 /// }
724 ///
725 /// impl PartialOrd for Person {
726 ///     fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
727 ///         Some(self.cmp(other))
728 ///     }
729 /// }
730 ///
731 /// impl Ord for Person {
732 ///     fn cmp(&self, other: &Person) -> Ordering {
733 ///         self.height.cmp(&other.height)
734 ///     }
735 /// }
736 ///
737 /// impl PartialEq for Person {
738 ///     fn eq(&self, other: &Person) -> bool {
739 ///         self.height == other.height
740 ///     }
741 /// }
742 /// ```
743 ///
744 /// You may also find it useful to use [`partial_cmp`] on your type's fields. Here
745 /// is an example of `Person` types who have a floating-point `height` field that
746 /// is the only field to be used for sorting:
747 ///
748 /// ```
749 /// use std::cmp::Ordering;
750 ///
751 /// struct Person {
752 ///     id: u32,
753 ///     name: String,
754 ///     height: f64,
755 /// }
756 ///
757 /// impl PartialOrd for Person {
758 ///     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
759 ///         self.height.partial_cmp(&other.height)
760 ///     }
761 /// }
762 ///
763 /// impl PartialEq for Person {
764 ///     fn eq(&self, other: &Self) -> bool {
765 ///         self.height == other.height
766 ///     }
767 /// }
768 /// ```
769 ///
770 /// # Examples
771 ///
772 /// ```
773 /// let x : u32 = 0;
774 /// let y : u32 = 1;
775 ///
776 /// assert_eq!(x < y, true);
777 /// assert_eq!(x.lt(&y), true);
778 /// ```
779 ///
780 /// [`partial_cmp`]: PartialOrd::partial_cmp
781 /// [`cmp`]: Ord::cmp
782 #[lang = "partial_ord"]
783 #[stable(feature = "rust1", since = "1.0.0")]
784 #[doc(alias = ">")]
785 #[doc(alias = "<")]
786 #[doc(alias = "<=")]
787 #[doc(alias = ">=")]
788 #[rustc_on_unimplemented(
789     message = "can't compare `{Self}` with `{Rhs}`",
790     label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`"
791 )]
792 pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
793     /// This method returns an ordering between `self` and `other` values if one exists.
794     ///
795     /// # Examples
796     ///
797     /// ```
798     /// use std::cmp::Ordering;
799     ///
800     /// let result = 1.0.partial_cmp(&2.0);
801     /// assert_eq!(result, Some(Ordering::Less));
802     ///
803     /// let result = 1.0.partial_cmp(&1.0);
804     /// assert_eq!(result, Some(Ordering::Equal));
805     ///
806     /// let result = 2.0.partial_cmp(&1.0);
807     /// assert_eq!(result, Some(Ordering::Greater));
808     /// ```
809     ///
810     /// When comparison is impossible:
811     ///
812     /// ```
813     /// let result = std::f64::NAN.partial_cmp(&1.0);
814     /// assert_eq!(result, None);
815     /// ```
816     #[must_use]
817     #[stable(feature = "rust1", since = "1.0.0")]
818     fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
819
820     /// This method tests less than (for `self` and `other`) and is used by the `<` operator.
821     ///
822     /// # Examples
823     ///
824     /// ```
825     /// let result = 1.0 < 2.0;
826     /// assert_eq!(result, true);
827     ///
828     /// let result = 2.0 < 1.0;
829     /// assert_eq!(result, false);
830     /// ```
831     #[inline]
832     #[must_use]
833     #[stable(feature = "rust1", since = "1.0.0")]
834     fn lt(&self, other: &Rhs) -> bool {
835         matches!(self.partial_cmp(other), Some(Less))
836     }
837
838     /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
839     /// operator.
840     ///
841     /// # Examples
842     ///
843     /// ```
844     /// let result = 1.0 <= 2.0;
845     /// assert_eq!(result, true);
846     ///
847     /// let result = 2.0 <= 2.0;
848     /// assert_eq!(result, true);
849     /// ```
850     #[inline]
851     #[must_use]
852     #[stable(feature = "rust1", since = "1.0.0")]
853     fn le(&self, other: &Rhs) -> bool {
854         matches!(self.partial_cmp(other), Some(Less) | Some(Equal))
855     }
856
857     /// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
858     ///
859     /// # Examples
860     ///
861     /// ```
862     /// let result = 1.0 > 2.0;
863     /// assert_eq!(result, false);
864     ///
865     /// let result = 2.0 > 2.0;
866     /// assert_eq!(result, false);
867     /// ```
868     #[inline]
869     #[must_use]
870     #[stable(feature = "rust1", since = "1.0.0")]
871     fn gt(&self, other: &Rhs) -> bool {
872         matches!(self.partial_cmp(other), Some(Greater))
873     }
874
875     /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
876     /// operator.
877     ///
878     /// # Examples
879     ///
880     /// ```
881     /// let result = 2.0 >= 1.0;
882     /// assert_eq!(result, true);
883     ///
884     /// let result = 2.0 >= 2.0;
885     /// assert_eq!(result, true);
886     /// ```
887     #[inline]
888     #[must_use]
889     #[stable(feature = "rust1", since = "1.0.0")]
890     fn ge(&self, other: &Rhs) -> bool {
891         matches!(self.partial_cmp(other), Some(Greater) | Some(Equal))
892     }
893 }
894
895 /// Derive macro generating an impl of the trait `PartialOrd`.
896 #[rustc_builtin_macro]
897 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
898 #[allow_internal_unstable(core_intrinsics)]
899 pub macro PartialOrd($item:item) {
900     /* compiler built-in */
901 }
902
903 /// Compares and returns the minimum of two values.
904 ///
905 /// Returns the first argument if the comparison determines them to be equal.
906 ///
907 /// Internally uses an alias to [`Ord::min`].
908 ///
909 /// # Examples
910 ///
911 /// ```
912 /// use std::cmp;
913 ///
914 /// assert_eq!(1, cmp::min(1, 2));
915 /// assert_eq!(2, cmp::min(2, 2));
916 /// ```
917 #[inline]
918 #[stable(feature = "rust1", since = "1.0.0")]
919 pub fn min<T: Ord>(v1: T, v2: T) -> T {
920     v1.min(v2)
921 }
922
923 /// Returns the minimum of two values with respect to the specified comparison function.
924 ///
925 /// Returns the first argument if the comparison determines them to be equal.
926 ///
927 /// # Examples
928 ///
929 /// ```
930 /// #![feature(cmp_min_max_by)]
931 ///
932 /// use std::cmp;
933 ///
934 /// assert_eq!(cmp::min_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 1);
935 /// assert_eq!(cmp::min_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
936 /// ```
937 #[inline]
938 #[unstable(feature = "cmp_min_max_by", issue = "64460")]
939 pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
940     match compare(&v1, &v2) {
941         Ordering::Less | Ordering::Equal => v1,
942         Ordering::Greater => v2,
943     }
944 }
945
946 /// Returns the element that gives the minimum value from the specified function.
947 ///
948 /// Returns the first argument if the comparison determines them to be equal.
949 ///
950 /// # Examples
951 ///
952 /// ```
953 /// #![feature(cmp_min_max_by)]
954 ///
955 /// use std::cmp;
956 ///
957 /// assert_eq!(cmp::min_by_key(-2, 1, |x: &i32| x.abs()), 1);
958 /// assert_eq!(cmp::min_by_key(-2, 2, |x: &i32| x.abs()), -2);
959 /// ```
960 #[inline]
961 #[unstable(feature = "cmp_min_max_by", issue = "64460")]
962 pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
963     min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
964 }
965
966 /// Compares and returns the maximum of two values.
967 ///
968 /// Returns the second argument if the comparison determines them to be equal.
969 ///
970 /// Internally uses an alias to [`Ord::max`].
971 ///
972 /// # Examples
973 ///
974 /// ```
975 /// use std::cmp;
976 ///
977 /// assert_eq!(2, cmp::max(1, 2));
978 /// assert_eq!(2, cmp::max(2, 2));
979 /// ```
980 #[inline]
981 #[stable(feature = "rust1", since = "1.0.0")]
982 pub fn max<T: Ord>(v1: T, v2: T) -> T {
983     v1.max(v2)
984 }
985
986 /// Returns the maximum of two values with respect to the specified comparison function.
987 ///
988 /// Returns the second argument if the comparison determines them to be equal.
989 ///
990 /// # Examples
991 ///
992 /// ```
993 /// #![feature(cmp_min_max_by)]
994 ///
995 /// use std::cmp;
996 ///
997 /// assert_eq!(cmp::max_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
998 /// assert_eq!(cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 2);
999 /// ```
1000 #[inline]
1001 #[unstable(feature = "cmp_min_max_by", issue = "64460")]
1002 pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1003     match compare(&v1, &v2) {
1004         Ordering::Less | Ordering::Equal => v2,
1005         Ordering::Greater => v1,
1006     }
1007 }
1008
1009 /// Returns the element that gives the maximum value from the specified function.
1010 ///
1011 /// Returns the second argument if the comparison determines them to be equal.
1012 ///
1013 /// # Examples
1014 ///
1015 /// ```
1016 /// #![feature(cmp_min_max_by)]
1017 ///
1018 /// use std::cmp;
1019 ///
1020 /// assert_eq!(cmp::max_by_key(-2, 1, |x: &i32| x.abs()), -2);
1021 /// assert_eq!(cmp::max_by_key(-2, 2, |x: &i32| x.abs()), 2);
1022 /// ```
1023 #[inline]
1024 #[unstable(feature = "cmp_min_max_by", issue = "64460")]
1025 pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1026     max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
1027 }
1028
1029 // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
1030 mod impls {
1031     use crate::cmp::Ordering::{self, Equal, Greater, Less};
1032     use crate::hint::unreachable_unchecked;
1033
1034     macro_rules! partial_eq_impl {
1035         ($($t:ty)*) => ($(
1036             #[stable(feature = "rust1", since = "1.0.0")]
1037             impl PartialEq for $t {
1038                 #[inline]
1039                 fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
1040                 #[inline]
1041                 fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
1042             }
1043         )*)
1044     }
1045
1046     #[stable(feature = "rust1", since = "1.0.0")]
1047     impl PartialEq for () {
1048         #[inline]
1049         fn eq(&self, _other: &()) -> bool {
1050             true
1051         }
1052         #[inline]
1053         fn ne(&self, _other: &()) -> bool {
1054             false
1055         }
1056     }
1057
1058     partial_eq_impl! {
1059         bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64
1060     }
1061
1062     macro_rules! eq_impl {
1063         ($($t:ty)*) => ($(
1064             #[stable(feature = "rust1", since = "1.0.0")]
1065             impl Eq for $t {}
1066         )*)
1067     }
1068
1069     eq_impl! { () bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
1070
1071     macro_rules! partial_ord_impl {
1072         ($($t:ty)*) => ($(
1073             #[stable(feature = "rust1", since = "1.0.0")]
1074             impl PartialOrd for $t {
1075                 #[inline]
1076                 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
1077                     match (self <= other, self >= other) {
1078                         (false, false) => None,
1079                         (false, true) => Some(Greater),
1080                         (true, false) => Some(Less),
1081                         (true, true) => Some(Equal),
1082                     }
1083                 }
1084                 #[inline]
1085                 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
1086                 #[inline]
1087                 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
1088                 #[inline]
1089                 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
1090                 #[inline]
1091                 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
1092             }
1093         )*)
1094     }
1095
1096     #[stable(feature = "rust1", since = "1.0.0")]
1097     impl PartialOrd for () {
1098         #[inline]
1099         fn partial_cmp(&self, _: &()) -> Option<Ordering> {
1100             Some(Equal)
1101         }
1102     }
1103
1104     #[stable(feature = "rust1", since = "1.0.0")]
1105     impl PartialOrd for bool {
1106         #[inline]
1107         fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
1108             (*self as u8).partial_cmp(&(*other as u8))
1109         }
1110     }
1111
1112     partial_ord_impl! { f32 f64 }
1113
1114     macro_rules! ord_impl {
1115         ($($t:ty)*) => ($(
1116             #[stable(feature = "rust1", since = "1.0.0")]
1117             impl PartialOrd for $t {
1118                 #[inline]
1119                 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
1120                     Some(self.cmp(other))
1121                 }
1122                 #[inline]
1123                 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
1124                 #[inline]
1125                 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
1126                 #[inline]
1127                 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
1128                 #[inline]
1129                 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
1130             }
1131
1132             #[stable(feature = "rust1", since = "1.0.0")]
1133             impl Ord for $t {
1134                 #[inline]
1135                 fn cmp(&self, other: &$t) -> Ordering {
1136                     // The order here is important to generate more optimal assembly.
1137                     // See <https://github.com/rust-lang/rust/issues/63758> for more info.
1138                     if *self < *other { Less }
1139                     else if *self == *other { Equal }
1140                     else { Greater }
1141                 }
1142             }
1143         )*)
1144     }
1145
1146     #[stable(feature = "rust1", since = "1.0.0")]
1147     impl Ord for () {
1148         #[inline]
1149         fn cmp(&self, _other: &()) -> Ordering {
1150             Equal
1151         }
1152     }
1153
1154     #[stable(feature = "rust1", since = "1.0.0")]
1155     impl Ord for bool {
1156         #[inline]
1157         fn cmp(&self, other: &bool) -> Ordering {
1158             // Casting to i8's and converting the difference to an Ordering generates
1159             // more optimal assembly.
1160             // See <https://github.com/rust-lang/rust/issues/66780> for more info.
1161             match (*self as i8) - (*other as i8) {
1162                 -1 => Less,
1163                 0 => Equal,
1164                 1 => Greater,
1165                 // SAFETY: bool as i8 returns 0 or 1, so the difference can't be anything else
1166                 _ => unsafe { unreachable_unchecked() },
1167             }
1168         }
1169     }
1170
1171     ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
1172
1173     #[unstable(feature = "never_type", issue = "35121")]
1174     impl PartialEq for ! {
1175         fn eq(&self, _: &!) -> bool {
1176             *self
1177         }
1178     }
1179
1180     #[unstable(feature = "never_type", issue = "35121")]
1181     impl Eq for ! {}
1182
1183     #[unstable(feature = "never_type", issue = "35121")]
1184     impl PartialOrd for ! {
1185         fn partial_cmp(&self, _: &!) -> Option<Ordering> {
1186             *self
1187         }
1188     }
1189
1190     #[unstable(feature = "never_type", issue = "35121")]
1191     impl Ord for ! {
1192         fn cmp(&self, _: &!) -> Ordering {
1193             *self
1194         }
1195     }
1196
1197     // & pointers
1198
1199     #[stable(feature = "rust1", since = "1.0.0")]
1200     impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A
1201     where
1202         A: PartialEq<B>,
1203     {
1204         #[inline]
1205         fn eq(&self, other: &&B) -> bool {
1206             PartialEq::eq(*self, *other)
1207         }
1208         #[inline]
1209         fn ne(&self, other: &&B) -> bool {
1210             PartialEq::ne(*self, *other)
1211         }
1212     }
1213     #[stable(feature = "rust1", since = "1.0.0")]
1214     impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
1215     where
1216         A: PartialOrd<B>,
1217     {
1218         #[inline]
1219         fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
1220             PartialOrd::partial_cmp(*self, *other)
1221         }
1222         #[inline]
1223         fn lt(&self, other: &&B) -> bool {
1224             PartialOrd::lt(*self, *other)
1225         }
1226         #[inline]
1227         fn le(&self, other: &&B) -> bool {
1228             PartialOrd::le(*self, *other)
1229         }
1230         #[inline]
1231         fn gt(&self, other: &&B) -> bool {
1232             PartialOrd::gt(*self, *other)
1233         }
1234         #[inline]
1235         fn ge(&self, other: &&B) -> bool {
1236             PartialOrd::ge(*self, *other)
1237         }
1238     }
1239     #[stable(feature = "rust1", since = "1.0.0")]
1240     impl<A: ?Sized> Ord for &A
1241     where
1242         A: Ord,
1243     {
1244         #[inline]
1245         fn cmp(&self, other: &Self) -> Ordering {
1246             Ord::cmp(*self, *other)
1247         }
1248     }
1249     #[stable(feature = "rust1", since = "1.0.0")]
1250     impl<A: ?Sized> Eq for &A where A: Eq {}
1251
1252     // &mut pointers
1253
1254     #[stable(feature = "rust1", since = "1.0.0")]
1255     impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A
1256     where
1257         A: PartialEq<B>,
1258     {
1259         #[inline]
1260         fn eq(&self, other: &&mut B) -> bool {
1261             PartialEq::eq(*self, *other)
1262         }
1263         #[inline]
1264         fn ne(&self, other: &&mut B) -> bool {
1265             PartialEq::ne(*self, *other)
1266         }
1267     }
1268     #[stable(feature = "rust1", since = "1.0.0")]
1269     impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A
1270     where
1271         A: PartialOrd<B>,
1272     {
1273         #[inline]
1274         fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
1275             PartialOrd::partial_cmp(*self, *other)
1276         }
1277         #[inline]
1278         fn lt(&self, other: &&mut B) -> bool {
1279             PartialOrd::lt(*self, *other)
1280         }
1281         #[inline]
1282         fn le(&self, other: &&mut B) -> bool {
1283             PartialOrd::le(*self, *other)
1284         }
1285         #[inline]
1286         fn gt(&self, other: &&mut B) -> bool {
1287             PartialOrd::gt(*self, *other)
1288         }
1289         #[inline]
1290         fn ge(&self, other: &&mut B) -> bool {
1291             PartialOrd::ge(*self, *other)
1292         }
1293     }
1294     #[stable(feature = "rust1", since = "1.0.0")]
1295     impl<A: ?Sized> Ord for &mut A
1296     where
1297         A: Ord,
1298     {
1299         #[inline]
1300         fn cmp(&self, other: &Self) -> Ordering {
1301             Ord::cmp(*self, *other)
1302         }
1303     }
1304     #[stable(feature = "rust1", since = "1.0.0")]
1305     impl<A: ?Sized> Eq for &mut A where A: Eq {}
1306
1307     #[stable(feature = "rust1", since = "1.0.0")]
1308     impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A
1309     where
1310         A: PartialEq<B>,
1311     {
1312         #[inline]
1313         fn eq(&self, other: &&mut B) -> bool {
1314             PartialEq::eq(*self, *other)
1315         }
1316         #[inline]
1317         fn ne(&self, other: &&mut B) -> bool {
1318             PartialEq::ne(*self, *other)
1319         }
1320     }
1321
1322     #[stable(feature = "rust1", since = "1.0.0")]
1323     impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A
1324     where
1325         A: PartialEq<B>,
1326     {
1327         #[inline]
1328         fn eq(&self, other: &&B) -> bool {
1329             PartialEq::eq(*self, *other)
1330         }
1331         #[inline]
1332         fn ne(&self, other: &&B) -> bool {
1333             PartialEq::ne(*self, *other)
1334         }
1335     }
1336 }