Trait Comparator

Source
pub trait Comparator<T: ?Sized> {
    // Required method
    fn cmp(&self, left: &T, right: &T) -> Ordering;

    // Provided methods
    fn reversed(self) -> ReversedOrder<T, Self>
       where Self: Sized { ... }
    fn then<N>(self, next: N) -> CompareThen<T, Self, N>
       where Self: Sized,
             N: Comparator<T> { ... }
    fn max<'a>(&self, left: &'a T, right: &'a T) -> &'a T { ... }
    fn min<'a>(&self, left: &'a T, right: &'a T) -> &'a T { ... }
}
Expand description

Implementations define a linear ordering over the type T, which can be semantically different from Ord::cmp.

By convention, the ordering should fulfill the following properties (for all a, b and c):

  • connex: a <= b or b <= a
  • antisymmetric: if a <= b and b <= a then a == b
  • transitive: if a <= b and b <= c then a <= c

(where a <= b can be also understood as !(a > b))

§Examples

use std::cmp::Ordering;
use classific::{Comparator, comparing, natural_order, reverse_order};

assert_eq!(natural_order().cmp(&1, &2), Ordering::Less);
assert_eq!(comparing(|v| v & !3).cmp(&1, &2), Ordering::Equal);
assert_eq!(reverse_order().cmp(&1, &2), Ordering::Greater);

Required Methods§

Source

fn cmp(&self, left: &T, right: &T) -> Ordering

This method returns an Ordering between left and right.

By convention, comparator.cmp(&left, &right) returns the ordering matching the expression left <operator> right if true.

§Examples
use std::cmp::Ordering;
use classific::{Comparator, comparing, natural_order, reverse_order};

assert_eq!(natural_order().cmp(&1, &2), Ordering::Less);
assert_eq!(comparing(|v| v & !3).cmp(&1, &2), Ordering::Equal);
assert_eq!(reverse_order().cmp(&1, &2), Ordering::Greater);

Provided Methods§

Source

fn reversed(self) -> ReversedOrder<T, Self>
where Self: Sized,

This method returns a Comparator which is semantically reversed from self.

The ordering of the result Comparator will fulfill the following (for all a, b):

use classific::Comparator;

fn test<T>(a: &T, b: &T, comparator: impl Comparator<T>) {
    assert_eq!(comparator.cmp(a, b).reverse(), comparator.reversed().cmp(a, b));
}
Source

fn then<N>(self, next: N) -> CompareThen<T, Self, N>
where Self: Sized, N: Comparator<T>,

This method returns a Comparator which is further refining the semantics of self.

next::cmp is called only when self::cmp returns Ordering::Equal.

§Examples
use std::cmp::Ordering;
use classific::{Comparator, comparing, natural_order, reverse_order};

assert_eq!(comparing(|t: &(i8, i8)| t.0).then(comparing(|t: &(i8, i8)| t.1)).cmp(&(1, 2), &(1, 3)), Ordering::Less);
Source

fn max<'a>(&self, left: &'a T, right: &'a T) -> &'a T

This function returns the greater instace (out of left and right) according to cmp.

§Examples
use classific::{Comparator, reverse_order};

assert_eq!(reverse_order().max(&1, &2), &1);
Source

fn min<'a>(&self, left: &'a T, right: &'a T) -> &'a T

This function returns the less instace (out of left and right) according to cmp.

§Examples
use classific::{Comparator, reverse_order};

assert_eq!(reverse_order().min(&1, &2), &2);

Implementors§

Source§

impl<S, T, F> Comparator<S> for Comparing<S, T, F>
where S: ?Sized, T: Ord, F: Fn(&S) -> T,

Source§

impl<S, T, F> Comparator<S> for ComparingRef<S, T, F>
where S: ?Sized, T: ?Sized + Ord, F: Fn(&S) -> &T,

Source§

impl<S, T, F, C> Comparator<S> for ComparingRefWith<S, T, F, C>
where S: ?Sized, T: ?Sized + Ord, F: Fn(&S) -> &T, C: Comparator<T>,

Source§

impl<S, T, F, C> Comparator<S> for ComparingWith<S, T, F, C>
where S: ?Sized, T: Ord, F: Fn(&S) -> T, C: Comparator<T>,

Source§

impl<T> Comparator<T> for NaturalOrder<T>
where T: ?Sized + Ord,

Source§

impl<T> Comparator<T> for ReversedNaturalOrder<T>
where T: ?Sized + Ord,

Source§

impl<T, C> Comparator<T> for PartialOrderOr<T, C>
where T: ?Sized + PartialOrd<T>, C: Comparator<T>,

Source§

impl<T, C> Comparator<T> for ReversedOrder<T, C>
where T: ?Sized, C: Comparator<T>,

Source§

impl<T, C, N> Comparator<T> for CompareThen<T, C, N>
where T: ?Sized, C: Comparator<T>, N: Comparator<T>,

Source§

impl<T, F> Comparator<T> for AtGreatest<T, F>
where T: ?Sized, F: Fn(&T) -> bool,

Source§

impl<T, F> Comparator<T> for AtLeast<T, F>
where T: ?Sized, F: Fn(&T) -> bool,

Source§

impl<T, F> Comparator<T> for F
where T: ?Sized, F: Fn(&T, &T) -> Ordering,