Trait compare::Compare
[−]
[src]
pub trait Compare<L: ?Sized, R: ?Sized = L> { fn compare(&self, l: &L, r: &R) -> Ordering; fn compares_lt(&self, l: &L, r: &R) -> bool { ... } fn compares_le(&self, l: &L, r: &R) -> bool { ... } fn compares_ge(&self, l: &L, r: &R) -> bool { ... } fn compares_gt(&self, l: &L, r: &R) -> bool { ... } fn compares_eq(&self, l: &L, r: &R) -> bool { ... } fn compares_ne(&self, l: &L, r: &R) -> bool { ... } fn borrowing(self) -> Borrowing<Self, L, R> where Self: Sized { ... } fn rev(self) -> Rev<Self> where Self: Sized { ... } fn swap(self) -> Swap<Self> where Self: Sized { ... } fn then<D>(self, then: D) -> Then<Self, D> where D: Compare<L, R>, Self: Sized { ... } }
A comparator imposing a total order.
See the compare
module's documentation for detailed usage.
The compares_*
methods may be overridden to provide more efficient implementations.
Required Methods
fn compare(&self, l: &L, r: &R) -> Ordering
Compares two values, returning Less
, Equal
, or Greater
if l
is less
than, equal to, or greater than r
, respectively.
Provided Methods
fn compares_lt(&self, l: &L, r: &R) -> bool
Checks if l
is less than r
.
fn compares_le(&self, l: &L, r: &R) -> bool
Checks if l
is less than or equal to r
.
fn compares_ge(&self, l: &L, r: &R) -> bool
Checks if l
is greater than or equal to r
.
fn compares_gt(&self, l: &L, r: &R) -> bool
Checks if l
is greater than r
.
fn compares_eq(&self, l: &L, r: &R) -> bool
Checks if l
is equal to r
.
fn compares_ne(&self, l: &L, r: &R) -> bool
Checks if l
is not equal to r
.
fn borrowing(self) -> Borrowing<Self, L, R> where Self: Sized
Borrows the comparator's parameters before comparing them.
Examples
use compare::{Compare, natural}; use std::cmp::Ordering::{Less, Equal, Greater}; let a_str = "a"; let a_string = a_str.to_string(); let b_str = "b"; let b_string = b_str.to_string(); let cmp = natural().borrowing(); assert_eq!(cmp.compare(a_str, &a_string), Equal); assert_eq!(cmp.compare(a_str, b_str), Less); assert_eq!(cmp.compare(&b_string, a_str), Greater);
fn rev(self) -> Rev<Self> where Self: Sized
Reverses the ordering of the comparator.
Examples
use compare::{Compare, natural}; use std::cmp::Ordering::{Less, Equal, Greater}; let a = &1; let b = &2; let cmp = natural().rev(); assert_eq!(cmp.compare(a, b), Greater); assert_eq!(cmp.compare(b, a), Less); assert_eq!(cmp.compare(a, a), Equal);
fn swap(self) -> Swap<Self> where Self: Sized
Swaps the comparator's parameters, maintaining the underlying ordering.
This is useful for providing a comparator C: Compare<T, U>
in a context that
expects C: Compare<U, T>
.
Examples
use compare::Compare; use std::cmp::Ordering::{Less, Equal, Greater}; let cmp = |l: &u8, r: &u16| (*l as u16).cmp(r); assert_eq!(cmp.compare(&1u8, &2u16), Less); assert_eq!(cmp.compare(&2u8, &1u16), Greater); assert_eq!(cmp.compare(&1u8, &1u16), Equal); let cmp = cmp.swap(); assert_eq!(cmp.compare(&2u16, &1u8), Less); assert_eq!(cmp.compare(&1u16, &2u8), Greater); assert_eq!(cmp.compare(&1u16, &1u8), Equal);
fn then<D>(self, then: D) -> Then<Self, D> where D: Compare<L, R>, Self: Sized
Lexicographically combines the comparator with another.
The retuned comparator compares values first using self
, then, if they are
equal, using then
.
Examples
use compare::{Compare, Extract}; use std::cmp::Ordering::{Less, Equal}; struct Foo { key1: char, key2: u8 } let f1 = &Foo { key1: 'a', key2: 2}; let f2 = &Foo { key1: 'a', key2: 3}; let cmp = Extract::new(|foo: &Foo| foo.key1); assert_eq!(cmp.compare(f1, f2), Equal); let cmp = cmp.then(Extract::new(|foo: &Foo| foo.key2)); assert_eq!(cmp.compare(f1, f2), Less);
Implementors
impl<F: ?Sized, L: ?Sized, R: ?Sized> Compare<L, R> for F where F: Fn(&L, &R) -> Ordering
impl<C, L: ?Sized, R: ?Sized, Lb: ?Sized, Rb: ?Sized> Compare<L, R> for Borrowing<C, Lb, Rb> where C: Compare<Lb, Rb>, L: Borrow<Lb>, R: Borrow<Rb>
impl<E, C, T: ?Sized, K> Compare<T> for Extract<E, C> where E: Fn(&T) -> K, C: Compare<K>
impl<C, D, L: ?Sized, R: ?Sized> Compare<L, R> for Then<C, D> where C: Compare<L, R>, D: Compare<L, R>
impl<T: Ord + ?Sized> Compare<T> for Natural<T>
impl<C, L: ?Sized, R: ?Sized> Compare<L, R> for Rev<C> where C: Compare<L, R>
impl<C, L: ?Sized, R: ?Sized> Compare<R, L> for Swap<C> where C: Compare<L, R>