Trait sp_std::cmp::PartialOrd

1.0.0 · source · []
pub trait PartialOrd<Rhs = Self>: PartialEq<Rhs> where
    Rhs: ?Sized
{ fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>; fn lt(&self, other: &Rhs) -> bool { ... }
fn le(&self, other: &Rhs) -> bool { ... }
fn gt(&self, other: &Rhs) -> bool { ... }
fn ge(&self, other: &Rhs) -> bool { ... } }
Expand description

Trait for types that form a partial order.

The lt, le, gt, and ge methods of this trait can be called using the <, <=, >, and >= operators, respectively.

The methods of this trait must be consistent with each other and with those of PartialEq in the following sense:

  • a == b if and only if partial_cmp(a, b) == Some(Equal).
  • a < b if and only if partial_cmp(a, b) == Some(Less) (ensured by the default implementation).
  • a > b if and only if partial_cmp(a, b) == Some(Greater) (ensured by the default implementation).
  • a <= b if and only if a < b || a == b (ensured by the default implementation).
  • a >= b if and only if a > b || a == b (ensured by the default implementation).
  • a != b if and only if !(a == b) (already part of PartialEq).

If Ord is also implemented for Self and Rhs, it must also be consistent with partial_cmp (see the documentation of that trait for the exact requirements). It’s easy to accidentally make them disagree by deriving some of the traits and manually implementing others.

The comparison must satisfy, for all a, b and c:

  • transitivity: a < b and b < c implies a < c. The same must hold for both == and >.
  • duality: a < b if and only if b > a.

Note that these requirements mean that the trait itself must be implemented symmetrically and transitively: if T: PartialOrd<U> and U: PartialOrd<V> then U: PartialOrd<T> and T: PartialOrd<V>.

Corollaries

The following corollaries follow from the above requirements:

  • irreflexivity of < and >: !(a < a), !(a > a)
  • transitivity of >: if a > b and b > c then a > c
  • duality of partial_cmp: partial_cmp(a, b) == partial_cmp(b, a).map(Ordering::reverse)

Derivable

This trait can be used with #[derive].

When derived on structs, it will produce a lexicographic ordering based on the top-to-bottom declaration order of the struct’s members.

When derived on enums, variants are ordered by their discriminants. By default, the discriminant is smallest for variants at the top, and largest for variants at the bottom. Here’s an example:

#[derive(PartialEq, PartialOrd)]
enum E {
    Top,
    Bottom,
}

assert!(E::Top < E::Bottom);

However, manually setting the discriminants can override this default behavior:

#[derive(PartialEq, PartialOrd)]
enum E {
    Top = 2,
    Bottom = 1,
}

assert!(E::Bottom < E::Top);

How can I implement PartialOrd?

PartialOrd only requires implementation of the partial_cmp method, with the others generated from default implementations.

However it remains possible to implement the others separately for types which do not have a total order. For example, for floating point numbers, NaN < 0 == false and NaN >= 0 == false (cf. IEEE 754-2008 section 5.11).

PartialOrd requires your type to be PartialEq.

If your type is Ord, you can implement partial_cmp by using cmp:

use std::cmp::Ordering;

#[derive(Eq)]
struct Person {
    id: u32,
    name: String,
    height: u32,
}

impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Person {
    fn cmp(&self, other: &Self) -> Ordering {
        self.height.cmp(&other.height)
    }
}

impl PartialEq for Person {
    fn eq(&self, other: &Self) -> bool {
        self.height == other.height
    }
}

You may also find it useful to use partial_cmp on your type’s fields. Here is an example of Person types who have a floating-point height field that is the only field to be used for sorting:

use std::cmp::Ordering;

struct Person {
    id: u32,
    name: String,
    height: f64,
}

impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.height.partial_cmp(&other.height)
    }
}

impl PartialEq for Person {
    fn eq(&self, other: &Self) -> bool {
        self.height == other.height
    }
}

Examples

let x: u32 = 0;
let y: u32 = 1;

assert_eq!(x < y, true);
assert_eq!(x.lt(&y), true);

Required methods

This method returns an ordering between self and other values if one exists.

Examples
use std::cmp::Ordering;

let result = 1.0.partial_cmp(&2.0);
assert_eq!(result, Some(Ordering::Less));

let result = 1.0.partial_cmp(&1.0);
assert_eq!(result, Some(Ordering::Equal));

let result = 2.0.partial_cmp(&1.0);
assert_eq!(result, Some(Ordering::Greater));

When comparison is impossible:

let result = f64::NAN.partial_cmp(&1.0);
assert_eq!(result, None);

Provided methods

This method tests less than (for self and other) and is used by the < operator.

Examples
let result = 1.0 < 2.0;
assert_eq!(result, true);

let result = 2.0 < 1.0;
assert_eq!(result, false);

This method tests less than or equal to (for self and other) and is used by the <= operator.

Examples
let result = 1.0 <= 2.0;
assert_eq!(result, true);

let result = 2.0 <= 2.0;
assert_eq!(result, true);

This method tests greater than (for self and other) and is used by the > operator.

Examples
let result = 1.0 > 2.0;
assert_eq!(result, false);

let result = 2.0 > 2.0;
assert_eq!(result, false);

This method tests greater than or equal to (for self and other) and is used by the >= operator.

Examples
let result = 2.0 >= 1.0;
assert_eq!(result, true);

let result = 2.0 >= 2.0;
assert_eq!(result, true);

Implementations on Foreign Types

Implements comparison operations on strings.

Strings are compared lexicographically by their byte values. This compares Unicode code points based on their positions in the code charts. This is not necessarily the same as “alphabetical” order, which varies by language and locale. Comparing strings according to culturally-accepted standards requires locale-specific data that is outside the scope of the str type.

Implements comparison of vectors lexicographically.

Implementors

Implements comparison of vectors, lexicographically.

impl<T, const CAP: usize> PartialOrd<ArrayVec<T, CAP>> for ArrayVec<T, CAP> where
    T: PartialOrd

impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for ArrayString<CAP>

impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>

impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str

impl<'a> PartialOrd<Components<'a>> for Components<'a>

impl PartialOrd<Path> for Path

impl<'a, 'b> PartialOrd<Path> for PathBuf

impl<'a, 'b> PartialOrd<PathBuf> for Path

impl<'a, 'b> PartialOrd<&'a Path> for PathBuf

impl<'a, 'b> PartialOrd<PathBuf> for &'a Path

impl<'a, 'b> PartialOrd<Path> for Cow<'a, Path>

impl<'a, 'b> PartialOrd<Cow<'a, Path>> for Path

impl<'a, 'b> PartialOrd<&'b Path> for Cow<'a, Path>

impl<'a, 'b> PartialOrd<Cow<'a, Path>> for &'b Path

impl<'a, 'b> PartialOrd<PathBuf> for Cow<'a, Path>

impl<'a, 'b> PartialOrd<Cow<'a, Path>> for PathBuf

impl<'a, 'b> PartialOrd<OsStr> for PathBuf

impl<'a, 'b> PartialOrd<PathBuf> for OsStr

impl<'a, 'b> PartialOrd<&'a OsStr> for PathBuf

impl<'a, 'b> PartialOrd<PathBuf> for &'a OsStr

impl<'a, 'b> PartialOrd<Cow<'a, OsStr>> for PathBuf

impl<'a, 'b> PartialOrd<PathBuf> for Cow<'a, OsStr>

impl<'a, 'b> PartialOrd<OsString> for PathBuf

impl<'a, 'b> PartialOrd<PathBuf> for OsString

impl<'a, 'b> PartialOrd<OsStr> for Path

impl<'a, 'b> PartialOrd<Path> for OsStr

impl<'a, 'b> PartialOrd<&'a OsStr> for Path

impl<'a, 'b> PartialOrd<Path> for &'a OsStr

impl<'a, 'b> PartialOrd<Cow<'a, OsStr>> for Path

impl<'a, 'b> PartialOrd<Path> for Cow<'a, OsStr>

impl<'a, 'b> PartialOrd<OsString> for Path

impl<'a, 'b> PartialOrd<Path> for OsString

impl<'a, 'b> PartialOrd<OsStr> for &'a Path

impl<'a, 'b> PartialOrd<&'a Path> for OsStr

impl<'a, 'b> PartialOrd<Cow<'b, OsStr>> for &'a Path

impl<'a, 'b> PartialOrd<&'a Path> for Cow<'b, OsStr>

impl<'a, 'b> PartialOrd<OsString> for &'a Path

impl<'a, 'b> PartialOrd<&'a Path> for OsString

impl<O, V, T> PartialOrd<BitArray<O, V>> for BitSlice<O, T> where
    O: BitOrder,
    V: BitView,
    T: BitStore

impl<O, V, Rhs> PartialOrd<Rhs> for BitArray<O, V> where
    O: BitOrder,
    V: BitView,
    Rhs: ?Sized,
    BitSlice<O, V::Store>: PartialOrd<Rhs>, 

impl<R: PartialOrd> PartialOrd<BitIdx<R>> for BitIdx<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitIdxError<R>> for BitIdxError<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitTail<R>> for BitTail<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitPos<R>> for BitPos<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitSel<R>> for BitSel<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitMask<R>> for BitMask<R> where
    R: BitRegister

impl PartialOrd<Mut> for Mut

impl PartialOrd<Lsb0> for Lsb0

impl PartialOrd<Msb0> for Msb0

impl<M1, M2, T1, T2> PartialOrd<Address<M2, T2>> for Address<M1, T1> where
    M1: Mutability,
    M2: Mutability,
    T1: BitStore,
    T2: BitStore

impl<T: PartialOrd> PartialOrd<AddressError<T>> for AddressError<T> where
    T: BitStore

impl<M1, M2, O1, O2, T1, T2> PartialOrd<BitRef<'_, M2, O2, T2>> for BitRef<'_, M1, O1, T1> where
    M1: Mutability,
    M2: Mutability,
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<M, O, T> PartialOrd<bool> for BitRef<'_, M, O, T> where
    M: Mutability,
    O: BitOrder,
    T: BitStore

impl<M, O, T> PartialOrd<&'_ bool> for BitRef<'_, M, O, T> where
    M: Mutability,
    O: BitOrder,
    T: BitStore

impl<M1, M2, O, T1, T2> PartialOrd<BitPtr<M2, O, T2>> for BitPtr<M1, O, T1> where
    M1: Mutability,
    M2: Mutability,
    O: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<T: PartialOrd> PartialOrd<BitPtrError<T>> for BitPtrError<T> where
    T: BitStore,
    T::Mem: PartialOrd

impl<T: PartialOrd> PartialOrd<BitSpanError<T>> for BitSpanError<T> where
    T: BitStore

impl<'a, O: PartialOrd, T: PartialOrd> PartialOrd<IterOnes<'a, O, T>> for IterOnes<'a, O, T> where
    O: BitOrder,
    T: BitStore

impl<'a, O: PartialOrd, T: PartialOrd> PartialOrd<IterZeros<'a, O, T>> for IterZeros<'a, O, T> where
    O: BitOrder,
    T: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitSlice<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitSlice<O2, T2>> for &BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitSlice<O2, T2>> for &mut BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<&'_ BitSlice<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<&'_ mut BitSlice<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<&'_ mut BitSlice<O2, T2>> for &BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<&'_ BitSlice<O2, T2>> for &mut BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O, T, Rhs> PartialOrd<Rhs> for BitBox<O, T> where
    O: BitOrder,
    T: BitStore,
    Rhs: ?Sized + PartialOrd<BitSlice<O, T>>, 

impl<'a, O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for &'a BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<'a, O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for &'a mut BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitVec<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<'a, O1, O2, T1, T2> PartialOrd<BitVec<O2, T2>> for &'a BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<'a, O1, O2, T1, T2> PartialOrd<BitVec<O2, T2>> for &'a mut BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O, T, Rhs> PartialOrd<Rhs> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore,
    Rhs: ?Sized + PartialOrd<BitSlice<O, T>>, 

impl<'a, 'b> PartialOrd<Vec<u8, Global>> for BString

impl<'a, 'b> PartialOrd<BString> for Vec<u8>

impl<'a, 'b> PartialOrd<[u8]> for BString

impl<'a, 'b> PartialOrd<BString> for [u8]

impl<'a, 'b> PartialOrd<&'a [u8]> for BString

impl<'a, 'b> PartialOrd<BString> for &'a [u8]

impl<'a, 'b> PartialOrd<String> for BString

impl<'a, 'b> PartialOrd<BString> for String

impl<'a, 'b> PartialOrd<str> for BString

impl<'a, 'b> PartialOrd<BString> for str

impl<'a, 'b> PartialOrd<&'a str> for BString

impl<'a, 'b> PartialOrd<BString> for &'a str

impl<'a, 'b> PartialOrd<BStr> for BString

impl<'a, 'b> PartialOrd<BString> for BStr

impl<'a, 'b> PartialOrd<&'a BStr> for BString

impl<'a, 'b> PartialOrd<BString> for &'a BStr

impl PartialOrd<BStr> for BStr

impl<'a, 'b> PartialOrd<[u8]> for BStr

impl<'a, 'b> PartialOrd<BStr> for [u8]

impl<'a, 'b> PartialOrd<&'a [u8]> for BStr

impl<'a, 'b> PartialOrd<BStr> for &'a [u8]

impl<'a, 'b> PartialOrd<str> for BStr

impl<'a, 'b> PartialOrd<BStr> for str

impl<'a, 'b> PartialOrd<&'a str> for BStr

impl<'a, 'b> PartialOrd<BStr> for &'a str

impl<'a, 'b> PartialOrd<Vec<u8, Global>> for BStr

impl<'a, 'b> PartialOrd<BStr> for Vec<u8>

impl<'a, 'b> PartialOrd<Vec<u8, Global>> for &'a BStr

impl<'a, 'b> PartialOrd<&'a BStr> for Vec<u8>

impl<'a, 'b> PartialOrd<String> for BStr

impl<'a, 'b> PartialOrd<BStr> for String

impl<'a, 'b> PartialOrd<String> for &'a BStr

impl<'a, 'b> PartialOrd<&'a BStr> for String

impl PartialOrd<str> for Bytes

impl PartialOrd<Bytes> for str

impl PartialOrd<Bytes> for Vec<u8>

impl PartialOrd<Bytes> for &str

impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes where
    Bytes: PartialOrd<T>, 

impl<'a, T: ?Sized> PartialOrd<&'a T> for BytesMut where
    BytesMut: PartialOrd<T>, 

impl<Tz: TimeZone> PartialOrd<Date<Tz>> for Date<Tz>

impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd<DateTime<Tz2>> for DateTime<Tz>

impl<const S: usize> PartialOrd<Cid<S>> for Cid<S>

impl PartialOrd<Inst> for Inst

impl PartialOrd<Heap> for Heap

impl<'g, T: ?Sized + Pointable> PartialOrd<Shared<'g, T>> for Shared<'g, T>

impl<L: PartialOrd, R: PartialOrd> PartialOrd<Either<L, R>> for Either<L, R>

impl<T: PartialOrd, N> PartialOrd<GenericArray<T, N>> for GenericArray<T, N> where
    N: ArrayLength<T>, 

impl PartialOrd<DwUt> for DwUt

impl PartialOrd<DwAt> for DwAt

impl PartialOrd<DwDs> for DwDs

impl PartialOrd<DwId> for DwId

impl PartialOrd<DwCc> for DwCc

impl PartialOrd<DwOp> for DwOp

impl<'a, T: ?Sized> PartialOrd<&'a T> for HeaderValue where
    HeaderValue: PartialOrd<T>, 

impl<'a> PartialOrd<HeaderValue> for &'a str

impl<'a> PartialOrd<Authority> for &'a str

impl<'a> PartialOrd<&'a str> for Authority

impl<'a> PartialOrd<&'a str> for PathAndQuery

impl<'a> PartialOrd<PathAndQuery> for &'a str

impl<H: PartialOrd + Hasher> PartialOrd<Topic<H>> for Topic<H>

impl<K: Hash + Eq + PartialOrd, V: PartialOrd, S: BuildHasher> PartialOrd<LinkedHashMap<K, V, S>> for LinkedHashMap<K, V, S>

impl<'k, 'ko> PartialOrd<Key<'ko>> for Key<'k>

impl<'a> PartialOrd<Metadata<'a>> for Metadata<'a>

impl PartialOrd<Type> for Type

impl PartialOrd<Tag> for Tag

impl<const S: usize> PartialOrd<Multihash<S>> for Multihash<S>

impl<T, R: Dim, C: Dim, S> PartialOrd<Matrix<T, R, C, S>> for Matrix<T, R, C, S> where
    T: Scalar + PartialOrd,
    S: Storage<T, R, C>, 

impl<T: Scalar + PartialOrd, const D: usize> PartialOrd<Point<T, D>> for Point<T, D>

impl PartialOrd<Sign> for Sign

impl<T: Clone + Integer> PartialOrd<Ratio<T>> for Ratio<T>

impl<O, T: ?Sized> PartialOrd<OwningRef<O, T>> for OwningRef<O, T> where
    T: PartialOrd

impl<O, T: ?Sized> PartialOrd<OwningRefMut<O, T>> for OwningRefMut<O, T> where
    T: PartialOrd

impl<T: PartialOrd> PartialOrd<Compact<T>> for Compact<T>

impl<'i> PartialOrd<Position<'i>> for Position<'i>

impl PartialOrd<Rule> for Rule

impl PartialOrd<U128> for U128

impl PartialOrd<U256> for U256

impl PartialOrd<U512> for U512

impl PartialOrd<H128> for H128

impl PartialOrd<H160> for H160

impl PartialOrd<H256> for H256

impl PartialOrd<H512> for H512

impl PartialOrd<Reg> for Reg

impl<R: PartialOrd + WritableBase> PartialOrd<Writable<R>> for Writable<R>

impl PartialOrd<Span> for Span

impl<Hash: PartialOrd, Number: PartialOrd> PartialOrd<EpochIdentifier<Hash, Number>> for EpochIdentifier<Hash, Number>

impl<A: Array> PartialOrd<SmallVec<A>> for SmallVec<A> where
    A::Item: PartialOrd

impl<'a> PartialOrd<Incoming<'a>> for Incoming<'a>

impl PartialOrd<Data> for Data

impl<T: Into<u64> + Copy> PartialOrd<T> for Slot

impl PartialOrd<Tm> for Tm

impl<A: Array> PartialOrd<ArrayVec<A>> for ArrayVec<A> where
    A::Item: PartialOrd

impl<'s, T> PartialOrd<SliceVec<'s, T>> for SliceVec<'s, T> where
    T: PartialOrd

impl<A: Array> PartialOrd<TinyVec<A>> for TinyVec<A> where
    A::Item: PartialOrd

impl<T: PartialOrd> PartialOrd<Spanned<T>> for Spanned<T>

impl<'a> PartialOrd<NibbleSlice<'a>> for NibbleSlice<'a>

impl PartialOrd<B0> for B0

impl PartialOrd<B1> for B1

impl<U: PartialOrd + Unsigned + NonZero> PartialOrd<PInt<U>> for PInt<U>

impl<U: PartialOrd + Unsigned + NonZero> PartialOrd<NInt<U>> for NInt<U>

impl PartialOrd<Z0> for Z0

impl<U: PartialOrd, B: PartialOrd> PartialOrd<UInt<U, B>> for UInt<U, B>

impl<V: PartialOrd, A: PartialOrd> PartialOrd<TArr<V, A>> for TArr<V, A>

impl PartialOrd<Less> for Less

impl<T: AsRef<str>> PartialOrd<Ascii<T>> for Ascii<T>

impl<T: AsRef<str>> PartialOrd<UniCase<T>> for UniCase<T>

impl<S: PartialOrd> PartialOrd<Host<S>> for Host<S>

impl PartialOrd<Url> for Url

impl<V: PartialOrd> PartialOrd<VecMap<V>> for VecMap<V>

impl<T> PartialOrd<T> for Void

impl<T: Into<F32> + Copy> PartialOrd<T> for F32

impl<T: Into<F64> + Copy> PartialOrd<T> for F64

impl<'a> PartialOrd<SectionCode<'a>> for SectionCode<'a>

impl PartialOrd<Span> for Span

impl PartialOrd<Time> for Time