[][src]Trait classific::Comparator

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

    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 { ... } }

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

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);
Loading content...

Provided methods

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

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

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

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);
Loading content...

Implementors

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

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>, 
[src]

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

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

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

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

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

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

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

impl<T: ?Sized> Comparator<T> for NaturalOrder<T> where
    T: Ord
[src]

impl<T: ?Sized> Comparator<T> for ReversedNaturalOrder<T> where
    T: Ord
[src]

impl<T: ?Sized, C> Comparator<T> for PartialOrderOr<T, C> where
    T: PartialOrd<T>,
    C: Comparator<T>, 
[src]

Loading content...