1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
//! A Java-like Comparator type.

#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
use std as core;

#[cfg(feature = "std")]
pub mod collections;
pub mod combinators;

pub use combinators::{comparing, natural_order, reverse_order};
use combinators::{Reversed, ThenComparing, ThenComparingByKey};
use core::cmp::Ordering;

/// An interface for dealing with comparators.
pub trait Comparator<T>
where
    T: ?Sized,
{
    /// Compares its two arguments for order.
    fn compare(&self, a: &T, b: &T) -> Ordering;

    /// Reverses the `Comparator`.
    fn reversed(self) -> Reversed<Self>
    where
        Self: Sized,
    {
        Reversed(self)
    }

    /// Chains two comparators.
    ///
    /// Returns the result of the first comparator when it's not `Equal`.
    /// Otherwise returns the result of the second comparator.
    ///
    /// # Examples
    ///
    /// ```
    /// use comparator::{as_fn, comparing, natural_order, Comparator};
    /// let mut v = [4, 2, 6, 3, 1, 5, 8, 7];
    /// // Groups numbers by even-ness and sorts them.
    /// v.sort_by(as_fn(comparing(|i| i % 2).then_comparing(natural_order())));
    /// assert_eq!(v, [2, 4, 6, 8, 1, 3, 5, 7]);
    /// ```
    fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U>
    where
        Self: Sized,
        U: Comparator<T>,
    {
        ThenComparing(self, other)
    }

    /// Chains a comparator with a key function.
    ///
    /// Returns the result of the first comparator when it's not `Equal`.
    /// Otherwise calls `f` and returns the result.
    fn then_comparing_by_key<F, U>(self, f: F) -> ThenComparingByKey<Self, F>
    where
        Self: Sized,
        F: Fn(&T) -> U,
        U: Ord,
    {
        ThenComparingByKey {
            comparator: self,
            f,
        }
    }
}

impl<F, T> Comparator<T> for F
where
    F: Fn(&T, &T) -> Ordering,
    T: ?Sized,
{
    fn compare(&self, a: &T, b: &T) -> Ordering {
        self(a, b)
    }
}

/// Convert a comparator to a comparator function.
///
/// This is not a member function, as in stable Rust it's not possible
/// to implement `Fn` trait manually, forcing usage of `impl Trait`
/// return type. Currently it is not allowed for trait methods to return
/// `impl Trait`s.
///
/// # Examples
///
/// ```
/// use comparator::{as_fn, reverse_order};
/// let mut v = [5, 4, 1, 3, 2];
/// v.sort_by(as_fn(reverse_order()));
/// assert_eq!(v, [5, 4, 3, 2, 1]);
/// ```
pub fn as_fn<T>(comparator: impl Comparator<T>) -> impl Fn(&T, &T) -> Ordering
where
    T: ?Sized,
{
    move |a, b| comparator.compare(a, b)
}