Trait kempt::Sort

source ·
pub trait Sort<Other = Self>
where Other: ?Sized,
{ // Required method fn compare(&self, other: &Other) -> Ordering; }
Expand description

Provides a comparison between Self and Other.

This function should only be implemented for types who guarantee that their PartialOrd<Other> implementations are identical to their PartialOrd implementations. For example, Path and PathBuf can be interchangeably compared regardless of whether the left or right or both are a Path or PathBuf.

Why not just use PartialOrd<Other>? Unfortunately, PartialOrd<str> is not implemented for String. This led to issues implementing the Map::entry function when passing a &str when the Key type was String.

This trait is automatically implemented for types that implement Ord and PartialOrd<Other>, but it additionally provides implementations for String/str and Vec<T>/[T].

In general, this trait should not need to be implemented. Implement Ord on your Key type, and if needed, implement PartialOrd<Other> for your borrowed form.

Required Methods§

source

fn compare(&self, other: &Other) -> Ordering

Compare self and other, returning the comparison result.

This function should be implemented identically to Ord::cmp/PartialOrd::partial_cmp.

Implementations on Foreign Types§

source§

impl Sort<str> for String

source§

fn compare(&self, b: &str) -> Ordering

source§

impl<T> Sort<[T]> for Vec<T>
where T: Ord,

source§

fn compare(&self, b: &[T]) -> Ordering

Implementors§

source§

impl<Key, SearchFor> Sort<SearchFor> for Key
where Key: Ord + PartialOrd<SearchFor>,