Trait stream_more::Compare

source ·
pub trait Compare<L, R = L>where
    L: ?Sized,
    R: ?Sized,{
    // Required method
    fn compare(&self, l: &L, r: &R) -> Ordering;

    // Provided methods
    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 { ... }
}
Expand description

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§

source

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§

source

fn compares_lt(&self, l: &L, r: &R) -> bool

Checks if l is less than r.

source

fn compares_le(&self, l: &L, r: &R) -> bool

Checks if l is less than or equal to r.

source

fn compares_ge(&self, l: &L, r: &R) -> bool

Checks if l is greater than or equal to r.

source

fn compares_gt(&self, l: &L, r: &R) -> bool

Checks if l is greater than r.

source

fn compares_eq(&self, l: &L, r: &R) -> bool

Checks if l is equal to r.

source

fn compares_ne(&self, l: &L, r: &R) -> bool

Checks if l is not equal to r.

source

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);
source

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);
source

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);
source

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);

Implementations on Foreign Types§

source§

impl<T> Compare<T, T> for MinComparatorwhere T: Ord,

source§

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

source§

impl<T, F> Compare<T, T> for FnComparator<F>where F: Fn(&T, &T) -> Ordering,

source§

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

source§

impl<T> Compare<T, T> for MaxComparatorwhere T: Ord,

source§

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

source§

impl<K, T, F> Compare<T, T> for KeyComparator<F>where K: Ord, F: Fn(&T) -> K,

source§

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

Implementors§

source§

impl<C, D, L, R> Compare<L, R> for Then<C, D>where C: Compare<L, R>, D: Compare<L, R>, L: ?Sized, R: ?Sized,

source§

impl<C, L, R> Compare<L, R> for Rev<C>where C: Compare<L, R>, L: ?Sized, R: ?Sized,

source§

impl<C, L, R> Compare<R, L> for Swap<C>where C: Compare<L, R>, L: ?Sized, R: ?Sized,

source§

impl<C, L, R, Lb, Rb> Compare<L, R> for Borrowing<C, Lb, Rb>where C: Compare<Lb, Rb>, L: Borrow<Lb> + ?Sized, R: Borrow<Rb> + ?Sized, Lb: ?Sized, Rb: ?Sized,

source§

impl<D, F> Compare<D, D> for FnCmp<F>where F: Fn(&D, &D) -> bool,

source§

impl<E, C, T, K> Compare<T, T> for Extract<E, C>where E: Fn(&T) -> K, C: Compare<K, K>, T: ?Sized,

source§

impl<F, L, R> Compare<L, R> for Fwhere F: Fn(&L, &R) -> Ordering + ?Sized, L: ?Sized, R: ?Sized,

source§

impl<T> Compare<T, T> for Ascendingwhere T: Ord,

source§

impl<T> Compare<T, T> for Descendingwhere T: Ord,

source§

impl<T> Compare<T, T> for Natural<T>where T: Ord + ?Sized,