#[doc(alias = "==")]
#[doc(alias = "!=")]
#[rustc_on_unimplemented(
- message="can't compare `{Self}` with `{Rhs}`",
- label="no implementation for `{Self} == {Rhs}`",
+ message = "can't compare `{Self}` with `{Rhs}`",
+ label = "no implementation for `{Self} == {Rhs}`"
)]
pub trait PartialEq<Rhs: ?Sized = Self> {
/// This method tests for `self` and `other` values to be equal, and is used
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
- fn ne(&self, other: &Rhs) -> bool { !self.eq(other) }
+ fn ne(&self, other: &Rhs) -> bool {
+ !self.eq(other)
+ }
}
/// Derive macro generating an impl of the trait `PartialEq`.
#[rustc_builtin_macro]
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow_internal_unstable(core_intrinsics, structural_match)]
-pub macro PartialEq($item:item) { /* compiler built-in */ }
+pub macro PartialEq($item:item) {
+ /* compiler built-in */
+}
/// Trait for equality comparisons which are [equivalence relations](
/// https://en.wikipedia.org/wiki/Equivalence_relation).
#[rustc_builtin_macro]
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow_internal_unstable(core_intrinsics, derive_eq, structural_match)]
-pub macro Eq($item:item) { /* compiler built-in */ }
+pub macro Eq($item:item) {
+ /* compiler built-in */
+}
// FIXME: this struct is used solely by #[derive] to
// assert that every component of a type implements Eq.
// This struct should never appear in user code.
#[doc(hidden)]
#[allow(missing_debug_implementations)]
-#[unstable(feature = "derive_eq",
- reason = "deriving hack, should not be public",
- issue = "none")]
-pub struct AssertParamIsEq<T: Eq + ?Sized> { _field: crate::marker::PhantomData<T> }
+#[unstable(feature = "derive_eq", reason = "deriving hack, should not be public", issue = "none")]
+pub struct AssertParamIsEq<T: Eq + ?Sized> {
+ _field: crate::marker::PhantomData<T>,
+}
/// An `Ordering` is the result of a comparison between two values.
///
}
#[inline]
- fn lt(&self, other: &Self) -> bool { other.0 < self.0 }
+ fn lt(&self, other: &Self) -> bool {
+ other.0 < self.0
+ }
#[inline]
- fn le(&self, other: &Self) -> bool { other.0 <= self.0 }
+ fn le(&self, other: &Self) -> bool {
+ other.0 <= self.0
+ }
#[inline]
- fn gt(&self, other: &Self) -> bool { other.0 > self.0 }
+ fn gt(&self, other: &Self) -> bool {
+ other.0 > self.0
+ }
#[inline]
- fn ge(&self, other: &Self) -> bool { other.0 >= self.0 }
+ fn ge(&self, other: &Self) -> bool {
+ other.0 >= self.0
+ }
}
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
///
/// An order is a total order if it is (for all `a`, `b` and `c`):
///
-/// - total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and
+/// - total and asymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and
/// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
///
/// ## Derivable
#[stable(feature = "ord_max_min", since = "1.21.0")]
#[inline]
fn max(self, other: Self) -> Self
- where Self: Sized {
+ where
+ Self: Sized,
+ {
max_by(self, other, Ord::cmp)
}
#[stable(feature = "ord_max_min", since = "1.21.0")]
#[inline]
fn min(self, other: Self) -> Self
- where Self: Sized {
+ where
+ Self: Sized,
+ {
min_by(self, other, Ord::cmp)
}
/// ```
#[unstable(feature = "clamp", issue = "44095")]
fn clamp(self, min: Self, max: Self) -> Self
- where Self: Sized {
+ where
+ Self: Sized,
+ {
assert!(min <= max);
if self < min {
min
#[rustc_builtin_macro]
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow_internal_unstable(core_intrinsics)]
-pub macro Ord($item:item) { /* compiler built-in */ }
+pub macro Ord($item:item) {
+ /* compiler built-in */
+}
#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for Ordering {}
///
/// The comparison must satisfy, for all `a`, `b` and `c`:
///
-/// - antisymmetry: if `a < b` then `!(a > b)`, as well as `a > b` implying `!(a < b)`; and
+/// - asymmetry: if `a < b` then `!(a > b)`, as well as `a > b` implying `!(a < b)`; and
/// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
///
/// Note that these requirements mean that the trait itself must be implemented symmetrically and
#[doc(alias = "<=")]
#[doc(alias = ">=")]
#[rustc_on_unimplemented(
- message="can't compare `{Self}` with `{Rhs}`",
- label="no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
+ message = "can't compare `{Self}` with `{Rhs}`",
+ label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`"
)]
pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
/// This method returns an ordering between `self` and `other` values if one exists.
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn lt(&self, other: &Rhs) -> bool {
- match self.partial_cmp(other) {
- Some(Less) => true,
- _ => false,
- }
+ matches!(self.partial_cmp(other), Some(Less))
}
/// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn le(&self, other: &Rhs) -> bool {
- match self.partial_cmp(other) {
- Some(Less) | Some(Equal) => true,
- _ => false,
- }
+ matches!(self.partial_cmp(other), Some(Less) | Some(Equal))
}
/// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn gt(&self, other: &Rhs) -> bool {
- match self.partial_cmp(other) {
- Some(Greater) => true,
- _ => false,
- }
+ matches!(self.partial_cmp(other), Some(Greater))
}
/// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn ge(&self, other: &Rhs) -> bool {
- match self.partial_cmp(other) {
- Some(Greater) | Some(Equal) => true,
- _ => false,
- }
+ matches!(self.partial_cmp(other), Some(Greater) | Some(Equal))
}
}
#[rustc_builtin_macro]
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow_internal_unstable(core_intrinsics)]
-pub macro PartialOrd($item:item) { /* compiler built-in */ }
+pub macro PartialOrd($item:item) {
+ /* compiler built-in */
+}
/// Compares and returns the minimum of two values.
///
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
mod impls {
+ use crate::cmp::Ordering::{self, Equal, Greater, Less};
use crate::hint::unreachable_unchecked;
- use crate::cmp::Ordering::{self, Less, Greater, Equal};
macro_rules! partial_eq_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for () {
#[inline]
- fn eq(&self, _other: &()) -> bool { true }
+ fn eq(&self, _other: &()) -> bool {
+ true
+ }
#[inline]
- fn ne(&self, _other: &()) -> bool { false }
+ fn ne(&self, _other: &()) -> bool {
+ false
+ }
}
partial_eq_impl! {
#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for () {
#[inline]
- fn cmp(&self, _other: &()) -> Ordering { Equal }
+ fn cmp(&self, _other: &()) -> Ordering {
+ Equal
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
// & pointers
#[stable(feature = "rust1", since = "1.0.0")]
- impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A where A: PartialEq<B> {
+ impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A
+ where
+ A: PartialEq<B>,
+ {
#[inline]
- fn eq(&self, other: & &B) -> bool { PartialEq::eq(*self, *other) }
+ fn eq(&self, other: &&B) -> bool {
+ PartialEq::eq(*self, *other)
+ }
#[inline]
- fn ne(&self, other: & &B) -> bool { PartialEq::ne(*self, *other) }
+ fn ne(&self, other: &&B) -> bool {
+ PartialEq::ne(*self, *other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
- impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A where A: PartialOrd<B> {
+ impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
+ where
+ A: PartialOrd<B>,
+ {
#[inline]
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
PartialOrd::partial_cmp(*self, *other)
}
#[inline]
- fn lt(&self, other: & &B) -> bool { PartialOrd::lt(*self, *other) }
+ fn lt(&self, other: &&B) -> bool {
+ PartialOrd::lt(*self, *other)
+ }
#[inline]
- fn le(&self, other: & &B) -> bool { PartialOrd::le(*self, *other) }
+ fn le(&self, other: &&B) -> bool {
+ PartialOrd::le(*self, *other)
+ }
#[inline]
- fn gt(&self, other: & &B) -> bool { PartialOrd::gt(*self, *other) }
+ fn gt(&self, other: &&B) -> bool {
+ PartialOrd::gt(*self, *other)
+ }
#[inline]
- fn ge(&self, other: & &B) -> bool { PartialOrd::ge(*self, *other) }
+ fn ge(&self, other: &&B) -> bool {
+ PartialOrd::ge(*self, *other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
- impl<A: ?Sized> Ord for &A where A: Ord {
+ impl<A: ?Sized> Ord for &A
+ where
+ A: Ord,
+ {
#[inline]
- fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(*self, *other) }
+ fn cmp(&self, other: &Self) -> Ordering {
+ Ord::cmp(*self, *other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Eq for &A where A: Eq {}
// &mut pointers
#[stable(feature = "rust1", since = "1.0.0")]
- impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A where A: PartialEq<B> {
+ impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A
+ where
+ A: PartialEq<B>,
+ {
#[inline]
- fn eq(&self, other: &&mut B) -> bool { PartialEq::eq(*self, *other) }
+ fn eq(&self, other: &&mut B) -> bool {
+ PartialEq::eq(*self, *other)
+ }
#[inline]
- fn ne(&self, other: &&mut B) -> bool { PartialEq::ne(*self, *other) }
+ fn ne(&self, other: &&mut B) -> bool {
+ PartialEq::ne(*self, *other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
- impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A where A: PartialOrd<B> {
+ impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A
+ where
+ A: PartialOrd<B>,
+ {
#[inline]
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
PartialOrd::partial_cmp(*self, *other)
}
#[inline]
- fn lt(&self, other: &&mut B) -> bool { PartialOrd::lt(*self, *other) }
+ fn lt(&self, other: &&mut B) -> bool {
+ PartialOrd::lt(*self, *other)
+ }
#[inline]
- fn le(&self, other: &&mut B) -> bool { PartialOrd::le(*self, *other) }
+ fn le(&self, other: &&mut B) -> bool {
+ PartialOrd::le(*self, *other)
+ }
#[inline]
- fn gt(&self, other: &&mut B) -> bool { PartialOrd::gt(*self, *other) }
+ fn gt(&self, other: &&mut B) -> bool {
+ PartialOrd::gt(*self, *other)
+ }
#[inline]
- fn ge(&self, other: &&mut B) -> bool { PartialOrd::ge(*self, *other) }
+ fn ge(&self, other: &&mut B) -> bool {
+ PartialOrd::ge(*self, *other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
- impl<A: ?Sized> Ord for &mut A where A: Ord {
+ impl<A: ?Sized> Ord for &mut A
+ where
+ A: Ord,
+ {
#[inline]
- fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(*self, *other) }
+ fn cmp(&self, other: &Self) -> Ordering {
+ Ord::cmp(*self, *other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Eq for &mut A where A: Eq {}
#[stable(feature = "rust1", since = "1.0.0")]
- impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A where A: PartialEq<B> {
+ impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A
+ where
+ A: PartialEq<B>,
+ {
#[inline]
- fn eq(&self, other: &&mut B) -> bool { PartialEq::eq(*self, *other) }
+ fn eq(&self, other: &&mut B) -> bool {
+ PartialEq::eq(*self, *other)
+ }
#[inline]
- fn ne(&self, other: &&mut B) -> bool { PartialEq::ne(*self, *other) }
+ fn ne(&self, other: &&mut B) -> bool {
+ PartialEq::ne(*self, *other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
- impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A where A: PartialEq<B> {
+ impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A
+ where
+ A: PartialEq<B>,
+ {
#[inline]
- fn eq(&self, other: &&B) -> bool { PartialEq::eq(*self, *other) }
+ fn eq(&self, other: &&B) -> bool {
+ PartialEq::eq(*self, *other)
+ }
#[inline]
- fn ne(&self, other: &&B) -> bool { PartialEq::ne(*self, *other) }
+ fn ne(&self, other: &&B) -> bool {
+ PartialEq::ne(*self, *other)
+ }
}
}