#![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;
pub trait Comparator<T>
where
T: ?Sized,
{
fn compare(&self, a: &T, b: &T) -> Ordering;
fn reversed(self) -> Reversed<Self>
where
Self: Sized,
{
Reversed(self)
}
fn then_comparing<U>(self, other: U) -> ThenComparing<Self, U>
where
Self: Sized,
U: Comparator<T>,
{
ThenComparing(self, other)
}
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)
}
}
pub fn as_fn<T>(comparator: impl Comparator<T>) -> impl Fn(&T, &T) -> Ordering
where
T: ?Sized,
{
move |a, b| comparator.compare(a, b)
}