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§
Provided Methods§
sourcefn compares_lt(&self, l: &L, r: &R) -> bool
fn compares_lt(&self, l: &L, r: &R) -> bool
Checks if l
is less than r
.
sourcefn compares_le(&self, l: &L, r: &R) -> bool
fn compares_le(&self, l: &L, r: &R) -> bool
Checks if l
is less than or equal to r
.
sourcefn compares_ge(&self, l: &L, r: &R) -> bool
fn compares_ge(&self, l: &L, r: &R) -> bool
Checks if l
is greater than or equal to r
.
sourcefn compares_gt(&self, l: &L, r: &R) -> bool
fn compares_gt(&self, l: &L, r: &R) -> bool
Checks if l
is greater than r
.
sourcefn compares_eq(&self, l: &L, r: &R) -> bool
fn compares_eq(&self, l: &L, r: &R) -> bool
Checks if l
is equal to r
.
sourcefn compares_ne(&self, l: &L, r: &R) -> bool
fn compares_ne(&self, l: &L, r: &R) -> bool
Checks if l
is not equal to r
.
sourcefn borrowing(self) -> Borrowing<Self, L, R>where
Self: Sized,
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);
sourcefn rev(self) -> Rev<Self>where
Self: Sized,
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);
sourcefn swap(self) -> Swap<Self>where
Self: Sized,
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);
sourcefn then<D>(self, then: D) -> Then<Self, D>where
D: Compare<L, R>,
Self: Sized,
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);