]> git.proxmox.com Git - rustc.git/blob - src/libcore/cmp.rs
New upstream version 1.44.1+dfsg1
[rustc.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 #[must_use]
365 #[stable(feature = "rust1", since = "1.0.0")]
366 pub fn reverse(self) -> Ordering {
367 match self {
368 Less => Greater,
369 Equal => Equal,
370 Greater => Less,
371 }
372 }
373
374 /// Chains two orderings.
375 ///
376 /// Returns `self` when it's not `Equal`. Otherwise returns `other`.
377 ///
378 /// # Examples
379 ///
380 /// ```
381 /// use std::cmp::Ordering;
382 ///
383 /// let result = Ordering::Equal.then(Ordering::Less);
384 /// assert_eq!(result, Ordering::Less);
385 ///
386 /// let result = Ordering::Less.then(Ordering::Equal);
387 /// assert_eq!(result, Ordering::Less);
388 ///
389 /// let result = Ordering::Less.then(Ordering::Greater);
390 /// assert_eq!(result, Ordering::Less);
391 ///
392 /// let result = Ordering::Equal.then(Ordering::Equal);
393 /// assert_eq!(result, Ordering::Equal);
394 ///
395 /// let x: (i64, i64, i64) = (1, 2, 7);
396 /// let y: (i64, i64, i64) = (1, 5, 3);
397 /// let result = x.0.cmp(&y.0).then(x.1.cmp(&y.1)).then(x.2.cmp(&y.2));
398 ///
399 /// assert_eq!(result, Ordering::Less);
400 /// ```
401 #[inline]
402 #[must_use]
403 #[stable(feature = "ordering_chaining", since = "1.17.0")]
404 pub fn then(self, other: Ordering) -> Ordering {
405 match self {
406 Equal => other,
407 _ => self,
408 }
409 }
410
411 /// Chains the ordering with the given function.
412 ///
413 /// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
414 /// the result.
415 ///
416 /// # Examples
417 ///
418 /// ```
419 /// use std::cmp::Ordering;
420 ///
421 /// let result = Ordering::Equal.then_with(|| Ordering::Less);
422 /// assert_eq!(result, Ordering::Less);
423 ///
424 /// let result = Ordering::Less.then_with(|| Ordering::Equal);
425 /// assert_eq!(result, Ordering::Less);
426 ///
427 /// let result = Ordering::Less.then_with(|| Ordering::Greater);
428 /// assert_eq!(result, Ordering::Less);
429 ///
430 /// let result = Ordering::Equal.then_with(|| Ordering::Equal);
431 /// assert_eq!(result, Ordering::Equal);
432 ///
433 /// let x: (i64, i64, i64) = (1, 2, 7);
434 /// let y: (i64, i64, i64) = (1, 5, 3);
435 /// let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));
436 ///
437 /// assert_eq!(result, Ordering::Less);
438 /// ```
439 #[inline]
440 #[must_use]
441 #[stable(feature = "ordering_chaining", since = "1.17.0")]
442 pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
443 match self {
444 Equal => f(),
445 _ => self,
446 }
447 }
448 }
449
450 /// A helper struct for reverse ordering.
451 ///
452 /// This struct is a helper to be used with functions like [`Vec::sort_by_key`] and
453 /// can be used to reverse order a part of a key.
454 ///
455 /// [`Vec::sort_by_key`]: ../../std/vec/struct.Vec.html#method.sort_by_key
456 ///
457 /// # Examples
458 ///
459 /// ```
460 /// use std::cmp::Reverse;
461 ///
462 /// let mut v = vec![1, 2, 3, 4, 5, 6];
463 /// v.sort_by_key(|&num| (num > 3, Reverse(num)));
464 /// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
465 /// ```
466 #[derive(PartialEq, Eq, Debug, Copy, Clone, Default, Hash)]
467 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
468 pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
469
470 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
471 impl<T: PartialOrd> PartialOrd for Reverse<T> {
472 #[inline]
473 fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
474 other.0.partial_cmp(&self.0)
475 }
476
477 #[inline]
478 fn lt(&self, other: &Self) -> bool {
479 other.0 < self.0
480 }
481 #[inline]
482 fn le(&self, other: &Self) -> bool {
483 other.0 <= self.0
484 }
485 #[inline]
486 fn gt(&self, other: &Self) -> bool {
487 other.0 > self.0
488 }
489 #[inline]
490 fn ge(&self, other: &Self) -> bool {
491 other.0 >= self.0
492 }
493 }
494
495 #[stable(feature = "reverse_cmp_key", since = "1.19.0")]
496 impl<T: Ord> Ord for Reverse<T> {
497 #[inline]
498 fn cmp(&self, other: &Reverse<T>) -> Ordering {
499 other.0.cmp(&self.0)
500 }
501 }
502
503 /// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
504 ///
505 /// An order is a total order if it is (for all `a`, `b` and `c`):
506 ///
507 /// - total and asymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and
508 /// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
509 ///
510 /// ## Derivable
511 ///
512 /// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
513 /// lexicographic ordering based on the top-to-bottom declaration order of the struct's members.
514 /// When `derive`d on enums, variants are ordered by their top-to-bottom declaration order.
515 ///
516 /// ## How can I implement `Ord`?
517 ///
518 /// `Ord` requires that the type also be [`PartialOrd`] and [`Eq`] (which requires [`PartialEq`]).
519 ///
520 /// Then you must define an implementation for [`cmp`]. You may find it useful to use
521 /// [`cmp`] on your type's fields.
522 ///
523 /// Implementations of [`PartialEq`], [`PartialOrd`], and `Ord` *must*
524 /// agree with each other. That is, `a.cmp(b) == Ordering::Equal` if
525 /// and only if `a == b` and `Some(a.cmp(b)) == a.partial_cmp(b)` for
526 /// all `a` and `b`. It's easy to accidentally make them disagree by
527 /// deriving some of the traits and manually implementing others.
528 ///
529 /// Here's an example where you want to sort people by height only, disregarding `id`
530 /// and `name`:
531 ///
532 /// ```
533 /// use std::cmp::Ordering;
534 ///
535 /// #[derive(Eq)]
536 /// struct Person {
537 /// id: u32,
538 /// name: String,
539 /// height: u32,
540 /// }
541 ///
542 /// impl Ord for Person {
543 /// fn cmp(&self, other: &Self) -> Ordering {
544 /// self.height.cmp(&other.height)
545 /// }
546 /// }
547 ///
548 /// impl PartialOrd for Person {
549 /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
550 /// Some(self.cmp(other))
551 /// }
552 /// }
553 ///
554 /// impl PartialEq for Person {
555 /// fn eq(&self, other: &Self) -> bool {
556 /// self.height == other.height
557 /// }
558 /// }
559 /// ```
560 ///
561 /// [`cmp`]: Ord::cmp
562 #[doc(alias = "<")]
563 #[doc(alias = ">")]
564 #[doc(alias = "<=")]
565 #[doc(alias = ">=")]
566 #[stable(feature = "rust1", since = "1.0.0")]
567 pub trait Ord: Eq + PartialOrd<Self> {
568 /// This method returns an [`Ordering`] between `self` and `other`.
569 ///
570 /// By convention, `self.cmp(&other)` returns the ordering matching the expression
571 /// `self <operator> other` if true.
572 ///
573 /// # Examples
574 ///
575 /// ```
576 /// use std::cmp::Ordering;
577 ///
578 /// assert_eq!(5.cmp(&10), Ordering::Less);
579 /// assert_eq!(10.cmp(&5), Ordering::Greater);
580 /// assert_eq!(5.cmp(&5), Ordering::Equal);
581 /// ```
582 #[must_use]
583 #[stable(feature = "rust1", since = "1.0.0")]
584 fn cmp(&self, other: &Self) -> Ordering;
585
586 /// Compares and returns the maximum of two values.
587 ///
588 /// Returns the second argument if the comparison determines them to be equal.
589 ///
590 /// # Examples
591 ///
592 /// ```
593 /// assert_eq!(2, 1.max(2));
594 /// assert_eq!(2, 2.max(2));
595 /// ```
596 #[stable(feature = "ord_max_min", since = "1.21.0")]
597 #[inline]
598 #[must_use]
599 fn max(self, other: Self) -> Self
600 where
601 Self: Sized,
602 {
603 max_by(self, other, Ord::cmp)
604 }
605
606 /// Compares and returns the minimum of two values.
607 ///
608 /// Returns the first argument if the comparison determines them to be equal.
609 ///
610 /// # Examples
611 ///
612 /// ```
613 /// assert_eq!(1, 1.min(2));
614 /// assert_eq!(2, 2.min(2));
615 /// ```
616 #[stable(feature = "ord_max_min", since = "1.21.0")]
617 #[inline]
618 #[must_use]
619 fn min(self, other: Self) -> Self
620 where
621 Self: Sized,
622 {
623 min_by(self, other, Ord::cmp)
624 }
625
626 /// Restrict a value to a certain interval.
627 ///
628 /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
629 /// less than `min`. Otherwise this returns `self`.
630 ///
631 /// # Panics
632 ///
633 /// Panics if `min > max`.
634 ///
635 /// # Examples
636 ///
637 /// ```
638 /// #![feature(clamp)]
639 ///
640 /// assert!((-3).clamp(-2, 1) == -2);
641 /// assert!(0.clamp(-2, 1) == 0);
642 /// assert!(2.clamp(-2, 1) == 1);
643 /// ```
644 #[must_use]
645 #[unstable(feature = "clamp", issue = "44095")]
646 fn clamp(self, min: Self, max: Self) -> Self
647 where
648 Self: Sized,
649 {
650 assert!(min <= max);
651 if self < min {
652 min
653 } else if self > max {
654 max
655 } else {
656 self
657 }
658 }
659 }
660
661 /// Derive macro generating an impl of the trait `Ord`.
662 #[rustc_builtin_macro]
663 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
664 #[allow_internal_unstable(core_intrinsics)]
665 pub macro Ord($item:item) {
666 /* compiler built-in */
667 }
668
669 #[stable(feature = "rust1", since = "1.0.0")]
670 impl Eq for Ordering {}
671
672 #[stable(feature = "rust1", since = "1.0.0")]
673 impl Ord for Ordering {
674 #[inline]
675 fn cmp(&self, other: &Ordering) -> Ordering {
676 (*self as i32).cmp(&(*other as i32))
677 }
678 }
679
680 #[stable(feature = "rust1", since = "1.0.0")]
681 impl PartialOrd for Ordering {
682 #[inline]
683 fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
684 (*self as i32).partial_cmp(&(*other as i32))
685 }
686 }
687
688 /// Trait for values that can be compared for a sort-order.
689 ///
690 /// The comparison must satisfy, for all `a`, `b` and `c`:
691 ///
692 /// - asymmetry: if `a < b` then `!(a > b)`, as well as `a > b` implying `!(a < b)`; and
693 /// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
694 ///
695 /// Note that these requirements mean that the trait itself must be implemented symmetrically and
696 /// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T:
697 /// PartialOrd<V>`.
698 ///
699 /// ## Derivable
700 ///
701 /// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
702 /// lexicographic ordering based on the top-to-bottom declaration order of the struct's members.
703 /// When `derive`d on enums, variants are ordered by their top-to-bottom declaration order.
704 ///
705 /// ## How can I implement `PartialOrd`?
706 ///
707 /// `PartialOrd` only requires implementation of the [`partial_cmp`] method, with the others
708 /// generated from default implementations.
709 ///
710 /// However it remains possible to implement the others separately for types which do not have a
711 /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 ==
712 /// false` (cf. IEEE 754-2008 section 5.11).
713 ///
714 /// `PartialOrd` requires your type to be [`PartialEq`].
715 ///
716 /// Implementations of [`PartialEq`], `PartialOrd`, and [`Ord`] *must* agree with each other. It's
717 /// easy to accidentally make them disagree by deriving some of the traits and manually
718 /// implementing others.
719 ///
720 /// If your type is [`Ord`], you can implement [`partial_cmp`] by using [`cmp`]:
721 ///
722 /// ```
723 /// use std::cmp::Ordering;
724 ///
725 /// #[derive(Eq)]
726 /// struct Person {
727 /// id: u32,
728 /// name: String,
729 /// height: u32,
730 /// }
731 ///
732 /// impl PartialOrd for Person {
733 /// fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
734 /// Some(self.cmp(other))
735 /// }
736 /// }
737 ///
738 /// impl Ord for Person {
739 /// fn cmp(&self, other: &Person) -> Ordering {
740 /// self.height.cmp(&other.height)
741 /// }
742 /// }
743 ///
744 /// impl PartialEq for Person {
745 /// fn eq(&self, other: &Person) -> bool {
746 /// self.height == other.height
747 /// }
748 /// }
749 /// ```
750 ///
751 /// You may also find it useful to use [`partial_cmp`] on your type's fields. Here
752 /// is an example of `Person` types who have a floating-point `height` field that
753 /// is the only field to be used for sorting:
754 ///
755 /// ```
756 /// use std::cmp::Ordering;
757 ///
758 /// struct Person {
759 /// id: u32,
760 /// name: String,
761 /// height: f64,
762 /// }
763 ///
764 /// impl PartialOrd for Person {
765 /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
766 /// self.height.partial_cmp(&other.height)
767 /// }
768 /// }
769 ///
770 /// impl PartialEq for Person {
771 /// fn eq(&self, other: &Self) -> bool {
772 /// self.height == other.height
773 /// }
774 /// }
775 /// ```
776 ///
777 /// # Examples
778 ///
779 /// ```
780 /// let x : u32 = 0;
781 /// let y : u32 = 1;
782 ///
783 /// assert_eq!(x < y, true);
784 /// assert_eq!(x.lt(&y), true);
785 /// ```
786 ///
787 /// [`partial_cmp`]: PartialOrd::partial_cmp
788 /// [`cmp`]: Ord::cmp
789 #[lang = "partial_ord"]
790 #[stable(feature = "rust1", since = "1.0.0")]
791 #[doc(alias = ">")]
792 #[doc(alias = "<")]
793 #[doc(alias = "<=")]
794 #[doc(alias = ">=")]
795 #[rustc_on_unimplemented(
796 message = "can't compare `{Self}` with `{Rhs}`",
797 label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`"
798 )]
799 pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
800 /// This method returns an ordering between `self` and `other` values if one exists.
801 ///
802 /// # Examples
803 ///
804 /// ```
805 /// use std::cmp::Ordering;
806 ///
807 /// let result = 1.0.partial_cmp(&2.0);
808 /// assert_eq!(result, Some(Ordering::Less));
809 ///
810 /// let result = 1.0.partial_cmp(&1.0);
811 /// assert_eq!(result, Some(Ordering::Equal));
812 ///
813 /// let result = 2.0.partial_cmp(&1.0);
814 /// assert_eq!(result, Some(Ordering::Greater));
815 /// ```
816 ///
817 /// When comparison is impossible:
818 ///
819 /// ```
820 /// let result = f64::NAN.partial_cmp(&1.0);
821 /// assert_eq!(result, None);
822 /// ```
823 #[must_use]
824 #[stable(feature = "rust1", since = "1.0.0")]
825 fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
826
827 /// This method tests less than (for `self` and `other`) and is used by the `<` operator.
828 ///
829 /// # Examples
830 ///
831 /// ```
832 /// let result = 1.0 < 2.0;
833 /// assert_eq!(result, true);
834 ///
835 /// let result = 2.0 < 1.0;
836 /// assert_eq!(result, false);
837 /// ```
838 #[inline]
839 #[must_use]
840 #[stable(feature = "rust1", since = "1.0.0")]
841 fn lt(&self, other: &Rhs) -> bool {
842 matches!(self.partial_cmp(other), Some(Less))
843 }
844
845 /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
846 /// operator.
847 ///
848 /// # Examples
849 ///
850 /// ```
851 /// let result = 1.0 <= 2.0;
852 /// assert_eq!(result, true);
853 ///
854 /// let result = 2.0 <= 2.0;
855 /// assert_eq!(result, true);
856 /// ```
857 #[inline]
858 #[must_use]
859 #[stable(feature = "rust1", since = "1.0.0")]
860 fn le(&self, other: &Rhs) -> bool {
861 matches!(self.partial_cmp(other), Some(Less | Equal))
862 }
863
864 /// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
865 ///
866 /// # Examples
867 ///
868 /// ```
869 /// let result = 1.0 > 2.0;
870 /// assert_eq!(result, false);
871 ///
872 /// let result = 2.0 > 2.0;
873 /// assert_eq!(result, false);
874 /// ```
875 #[inline]
876 #[must_use]
877 #[stable(feature = "rust1", since = "1.0.0")]
878 fn gt(&self, other: &Rhs) -> bool {
879 matches!(self.partial_cmp(other), Some(Greater))
880 }
881
882 /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
883 /// operator.
884 ///
885 /// # Examples
886 ///
887 /// ```
888 /// let result = 2.0 >= 1.0;
889 /// assert_eq!(result, true);
890 ///
891 /// let result = 2.0 >= 2.0;
892 /// assert_eq!(result, true);
893 /// ```
894 #[inline]
895 #[must_use]
896 #[stable(feature = "rust1", since = "1.0.0")]
897 fn ge(&self, other: &Rhs) -> bool {
898 matches!(self.partial_cmp(other), Some(Greater | Equal))
899 }
900 }
901
902 /// Derive macro generating an impl of the trait `PartialOrd`.
903 #[rustc_builtin_macro]
904 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
905 #[allow_internal_unstable(core_intrinsics)]
906 pub macro PartialOrd($item:item) {
907 /* compiler built-in */
908 }
909
910 /// Compares and returns the minimum of two values.
911 ///
912 /// Returns the first argument if the comparison determines them to be equal.
913 ///
914 /// Internally uses an alias to [`Ord::min`].
915 ///
916 /// # Examples
917 ///
918 /// ```
919 /// use std::cmp;
920 ///
921 /// assert_eq!(1, cmp::min(1, 2));
922 /// assert_eq!(2, cmp::min(2, 2));
923 /// ```
924 #[inline]
925 #[must_use]
926 #[stable(feature = "rust1", since = "1.0.0")]
927 pub fn min<T: Ord>(v1: T, v2: T) -> T {
928 v1.min(v2)
929 }
930
931 /// Returns the minimum of two values with respect to the specified comparison function.
932 ///
933 /// Returns the first argument if the comparison determines them to be equal.
934 ///
935 /// # Examples
936 ///
937 /// ```
938 /// #![feature(cmp_min_max_by)]
939 ///
940 /// use std::cmp;
941 ///
942 /// assert_eq!(cmp::min_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 1);
943 /// assert_eq!(cmp::min_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
944 /// ```
945 #[inline]
946 #[must_use]
947 #[unstable(feature = "cmp_min_max_by", issue = "64460")]
948 pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
949 match compare(&v1, &v2) {
950 Ordering::Less | Ordering::Equal => v1,
951 Ordering::Greater => v2,
952 }
953 }
954
955 /// Returns the element that gives the minimum value from the specified function.
956 ///
957 /// Returns the first argument if the comparison determines them to be equal.
958 ///
959 /// # Examples
960 ///
961 /// ```
962 /// #![feature(cmp_min_max_by)]
963 ///
964 /// use std::cmp;
965 ///
966 /// assert_eq!(cmp::min_by_key(-2, 1, |x: &i32| x.abs()), 1);
967 /// assert_eq!(cmp::min_by_key(-2, 2, |x: &i32| x.abs()), -2);
968 /// ```
969 #[inline]
970 #[must_use]
971 #[unstable(feature = "cmp_min_max_by", issue = "64460")]
972 pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
973 min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
974 }
975
976 /// Compares and returns the maximum of two values.
977 ///
978 /// Returns the second argument if the comparison determines them to be equal.
979 ///
980 /// Internally uses an alias to [`Ord::max`].
981 ///
982 /// # Examples
983 ///
984 /// ```
985 /// use std::cmp;
986 ///
987 /// assert_eq!(2, cmp::max(1, 2));
988 /// assert_eq!(2, cmp::max(2, 2));
989 /// ```
990 #[inline]
991 #[must_use]
992 #[stable(feature = "rust1", since = "1.0.0")]
993 pub fn max<T: Ord>(v1: T, v2: T) -> T {
994 v1.max(v2)
995 }
996
997 /// Returns the maximum of two values with respect to the specified comparison function.
998 ///
999 /// Returns the second argument if the comparison determines them to be equal.
1000 ///
1001 /// # Examples
1002 ///
1003 /// ```
1004 /// #![feature(cmp_min_max_by)]
1005 ///
1006 /// use std::cmp;
1007 ///
1008 /// assert_eq!(cmp::max_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
1009 /// assert_eq!(cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 2);
1010 /// ```
1011 #[inline]
1012 #[must_use]
1013 #[unstable(feature = "cmp_min_max_by", issue = "64460")]
1014 pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1015 match compare(&v1, &v2) {
1016 Ordering::Less | Ordering::Equal => v2,
1017 Ordering::Greater => v1,
1018 }
1019 }
1020
1021 /// Returns the element that gives the maximum value from the specified function.
1022 ///
1023 /// Returns the second argument if the comparison determines them to be equal.
1024 ///
1025 /// # Examples
1026 ///
1027 /// ```
1028 /// #![feature(cmp_min_max_by)]
1029 ///
1030 /// use std::cmp;
1031 ///
1032 /// assert_eq!(cmp::max_by_key(-2, 1, |x: &i32| x.abs()), -2);
1033 /// assert_eq!(cmp::max_by_key(-2, 2, |x: &i32| x.abs()), 2);
1034 /// ```
1035 #[inline]
1036 #[must_use]
1037 #[unstable(feature = "cmp_min_max_by", issue = "64460")]
1038 pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1039 max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
1040 }
1041
1042 // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
1043 mod impls {
1044 use crate::cmp::Ordering::{self, Equal, Greater, Less};
1045 use crate::hint::unreachable_unchecked;
1046
1047 macro_rules! partial_eq_impl {
1048 ($($t:ty)*) => ($(
1049 #[stable(feature = "rust1", since = "1.0.0")]
1050 impl PartialEq for $t {
1051 #[inline]
1052 fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
1053 #[inline]
1054 fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
1055 }
1056 )*)
1057 }
1058
1059 #[stable(feature = "rust1", since = "1.0.0")]
1060 impl PartialEq for () {
1061 #[inline]
1062 fn eq(&self, _other: &()) -> bool {
1063 true
1064 }
1065 #[inline]
1066 fn ne(&self, _other: &()) -> bool {
1067 false
1068 }
1069 }
1070
1071 partial_eq_impl! {
1072 bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64
1073 }
1074
1075 macro_rules! eq_impl {
1076 ($($t:ty)*) => ($(
1077 #[stable(feature = "rust1", since = "1.0.0")]
1078 impl Eq for $t {}
1079 )*)
1080 }
1081
1082 eq_impl! { () bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
1083
1084 macro_rules! partial_ord_impl {
1085 ($($t:ty)*) => ($(
1086 #[stable(feature = "rust1", since = "1.0.0")]
1087 impl PartialOrd for $t {
1088 #[inline]
1089 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
1090 match (self <= other, self >= other) {
1091 (false, false) => None,
1092 (false, true) => Some(Greater),
1093 (true, false) => Some(Less),
1094 (true, true) => Some(Equal),
1095 }
1096 }
1097 #[inline]
1098 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
1099 #[inline]
1100 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
1101 #[inline]
1102 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
1103 #[inline]
1104 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
1105 }
1106 )*)
1107 }
1108
1109 #[stable(feature = "rust1", since = "1.0.0")]
1110 impl PartialOrd for () {
1111 #[inline]
1112 fn partial_cmp(&self, _: &()) -> Option<Ordering> {
1113 Some(Equal)
1114 }
1115 }
1116
1117 #[stable(feature = "rust1", since = "1.0.0")]
1118 impl PartialOrd for bool {
1119 #[inline]
1120 fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
1121 (*self as u8).partial_cmp(&(*other as u8))
1122 }
1123 }
1124
1125 partial_ord_impl! { f32 f64 }
1126
1127 macro_rules! ord_impl {
1128 ($($t:ty)*) => ($(
1129 #[stable(feature = "rust1", since = "1.0.0")]
1130 impl PartialOrd for $t {
1131 #[inline]
1132 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
1133 Some(self.cmp(other))
1134 }
1135 #[inline]
1136 fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
1137 #[inline]
1138 fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
1139 #[inline]
1140 fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
1141 #[inline]
1142 fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
1143 }
1144
1145 #[stable(feature = "rust1", since = "1.0.0")]
1146 impl Ord for $t {
1147 #[inline]
1148 fn cmp(&self, other: &$t) -> Ordering {
1149 // The order here is important to generate more optimal assembly.
1150 // See <https://github.com/rust-lang/rust/issues/63758> for more info.
1151 if *self < *other { Less }
1152 else if *self == *other { Equal }
1153 else { Greater }
1154 }
1155 }
1156 )*)
1157 }
1158
1159 #[stable(feature = "rust1", since = "1.0.0")]
1160 impl Ord for () {
1161 #[inline]
1162 fn cmp(&self, _other: &()) -> Ordering {
1163 Equal
1164 }
1165 }
1166
1167 #[stable(feature = "rust1", since = "1.0.0")]
1168 impl Ord for bool {
1169 #[inline]
1170 fn cmp(&self, other: &bool) -> Ordering {
1171 // Casting to i8's and converting the difference to an Ordering generates
1172 // more optimal assembly.
1173 // See <https://github.com/rust-lang/rust/issues/66780> for more info.
1174 match (*self as i8) - (*other as i8) {
1175 -1 => Less,
1176 0 => Equal,
1177 1 => Greater,
1178 // SAFETY: bool as i8 returns 0 or 1, so the difference can't be anything else
1179 _ => unsafe { unreachable_unchecked() },
1180 }
1181 }
1182 }
1183
1184 ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
1185
1186 #[unstable(feature = "never_type", issue = "35121")]
1187 impl PartialEq for ! {
1188 fn eq(&self, _: &!) -> bool {
1189 *self
1190 }
1191 }
1192
1193 #[unstable(feature = "never_type", issue = "35121")]
1194 impl Eq for ! {}
1195
1196 #[unstable(feature = "never_type", issue = "35121")]
1197 impl PartialOrd for ! {
1198 fn partial_cmp(&self, _: &!) -> Option<Ordering> {
1199 *self
1200 }
1201 }
1202
1203 #[unstable(feature = "never_type", issue = "35121")]
1204 impl Ord for ! {
1205 fn cmp(&self, _: &!) -> Ordering {
1206 *self
1207 }
1208 }
1209
1210 // & pointers
1211
1212 #[stable(feature = "rust1", since = "1.0.0")]
1213 impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A
1214 where
1215 A: PartialEq<B>,
1216 {
1217 #[inline]
1218 fn eq(&self, other: &&B) -> bool {
1219 PartialEq::eq(*self, *other)
1220 }
1221 #[inline]
1222 fn ne(&self, other: &&B) -> bool {
1223 PartialEq::ne(*self, *other)
1224 }
1225 }
1226 #[stable(feature = "rust1", since = "1.0.0")]
1227 impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
1228 where
1229 A: PartialOrd<B>,
1230 {
1231 #[inline]
1232 fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
1233 PartialOrd::partial_cmp(*self, *other)
1234 }
1235 #[inline]
1236 fn lt(&self, other: &&B) -> bool {
1237 PartialOrd::lt(*self, *other)
1238 }
1239 #[inline]
1240 fn le(&self, other: &&B) -> bool {
1241 PartialOrd::le(*self, *other)
1242 }
1243 #[inline]
1244 fn gt(&self, other: &&B) -> bool {
1245 PartialOrd::gt(*self, *other)
1246 }
1247 #[inline]
1248 fn ge(&self, other: &&B) -> bool {
1249 PartialOrd::ge(*self, *other)
1250 }
1251 }
1252 #[stable(feature = "rust1", since = "1.0.0")]
1253 impl<A: ?Sized> Ord for &A
1254 where
1255 A: Ord,
1256 {
1257 #[inline]
1258 fn cmp(&self, other: &Self) -> Ordering {
1259 Ord::cmp(*self, *other)
1260 }
1261 }
1262 #[stable(feature = "rust1", since = "1.0.0")]
1263 impl<A: ?Sized> Eq for &A where A: Eq {}
1264
1265 // &mut pointers
1266
1267 #[stable(feature = "rust1", since = "1.0.0")]
1268 impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A
1269 where
1270 A: PartialEq<B>,
1271 {
1272 #[inline]
1273 fn eq(&self, other: &&mut B) -> bool {
1274 PartialEq::eq(*self, *other)
1275 }
1276 #[inline]
1277 fn ne(&self, other: &&mut B) -> bool {
1278 PartialEq::ne(*self, *other)
1279 }
1280 }
1281 #[stable(feature = "rust1", since = "1.0.0")]
1282 impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A
1283 where
1284 A: PartialOrd<B>,
1285 {
1286 #[inline]
1287 fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
1288 PartialOrd::partial_cmp(*self, *other)
1289 }
1290 #[inline]
1291 fn lt(&self, other: &&mut B) -> bool {
1292 PartialOrd::lt(*self, *other)
1293 }
1294 #[inline]
1295 fn le(&self, other: &&mut B) -> bool {
1296 PartialOrd::le(*self, *other)
1297 }
1298 #[inline]
1299 fn gt(&self, other: &&mut B) -> bool {
1300 PartialOrd::gt(*self, *other)
1301 }
1302 #[inline]
1303 fn ge(&self, other: &&mut B) -> bool {
1304 PartialOrd::ge(*self, *other)
1305 }
1306 }
1307 #[stable(feature = "rust1", since = "1.0.0")]
1308 impl<A: ?Sized> Ord for &mut A
1309 where
1310 A: Ord,
1311 {
1312 #[inline]
1313 fn cmp(&self, other: &Self) -> Ordering {
1314 Ord::cmp(*self, *other)
1315 }
1316 }
1317 #[stable(feature = "rust1", since = "1.0.0")]
1318 impl<A: ?Sized> Eq for &mut A where A: Eq {}
1319
1320 #[stable(feature = "rust1", since = "1.0.0")]
1321 impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A
1322 where
1323 A: PartialEq<B>,
1324 {
1325 #[inline]
1326 fn eq(&self, other: &&mut B) -> bool {
1327 PartialEq::eq(*self, *other)
1328 }
1329 #[inline]
1330 fn ne(&self, other: &&mut B) -> bool {
1331 PartialEq::ne(*self, *other)
1332 }
1333 }
1334
1335 #[stable(feature = "rust1", since = "1.0.0")]
1336 impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A
1337 where
1338 A: PartialEq<B>,
1339 {
1340 #[inline]
1341 fn eq(&self, other: &&B) -> bool {
1342 PartialEq::eq(*self, *other)
1343 }
1344 #[inline]
1345 fn ne(&self, other: &&B) -> bool {
1346 PartialEq::ne(*self, *other)
1347 }
1348 }
1349 }