[go: up one dir, main page]

Trait compare::Compare [] [src]

pub trait Compare<Lhs: ?Sized, Rhs: ?Sized = Lhs> {
    fn compare(&self, lhs: &Lhs, rhs: &Rhs) -> Ordering;

    fn compares_lt(&self, lhs: &Lhs, rhs: &Rhs) -> bool { ... }
    fn compares_le(&self, lhs: &Lhs, rhs: &Rhs) -> bool { ... }
    fn compares_ge(&self, lhs: &Lhs, rhs: &Rhs) -> bool { ... }
    fn compares_gt(&self, lhs: &Lhs, rhs: &Rhs) -> bool { ... }
    fn compares_eq(&self, lhs: &Lhs, rhs: &Rhs) -> bool { ... }
    fn compares_ne(&self, lhs: &Lhs, rhs: &Rhs) -> bool { ... }
    fn borrow(self) -> Borrow<Self, Lhs, Rhs> 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<Lhs, Rhs>, 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, lhs: &Lhs, rhs: &Rhs) -> Ordering

Compares two values, returning Less, Equal, or Greater if lhs is less than, equal to, or greater than rhs, respectively.

Provided Methods

fn compares_lt(&self, lhs: &Lhs, rhs: &Rhs) -> bool

Checks if lhs is less than rhs.

fn compares_le(&self, lhs: &Lhs, rhs: &Rhs) -> bool

Checks if lhs is less than or equal to rhs.

fn compares_ge(&self, lhs: &Lhs, rhs: &Rhs) -> bool

Checks if lhs is greater than or equal to rhs.

fn compares_gt(&self, lhs: &Lhs, rhs: &Rhs) -> bool

Checks if lhs is greater than rhs.

fn compares_eq(&self, lhs: &Lhs, rhs: &Rhs) -> bool

Checks if lhs is equal to rhs.

fn compares_ne(&self, lhs: &Lhs, rhs: &Rhs) -> bool

Checks if lhs is not equal to rhs.

fn borrow(self) -> Borrow<Self, Lhs, Rhs> 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().borrow();
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 = |lhs: &u8, rhs: &u16| (*lhs as u16).cmp(rhs);
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<Lhs, Rhs>, 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;
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 = |lhs: &Foo, rhs: &Foo| lhs.key1.cmp(&rhs.key1);
assert_eq!(cmp.compare(f1, f2), Equal);

let cmp = cmp.then(|lhs: &Foo, rhs: &Foo| lhs.key2.cmp(&rhs.key2));
assert_eq!(cmp.compare(f1, f2), Less);

Implementors