Commit bf3fab48 authored by Konrad Borowski's avatar Konrad Borowski

Merge branch 'master' into 'remove-generic-type-from-natural-order-comparator'

# Conflicts:
#   src/combinators.rs
parents 96c892a6 b8cf2238
Pipeline #43832743 passed with stage
in 1 minute and 12 seconds
......@@ -11,6 +11,7 @@ pub struct Reversed<T>(pub(crate) T);
impl<T, U> Comparator<U> for Reversed<T>
where
T: Comparator<U>,
U: ?Sized,
{
fn compare(&self, a: &U, b: &U) -> Ordering {
self.0.compare(a, b).reverse()
......@@ -25,6 +26,7 @@ impl<T, U, W> Comparator<W> for ThenComparing<T, U>
where
T: Comparator<W>,
U: Comparator<W>,
W: ?Sized,
{
fn compare(&self, a: &W, b: &W) -> Ordering {
self.0.compare(a, b).then_with(|| self.1.compare(a, b))
......@@ -53,6 +55,7 @@ impl<T, F, U, W> Comparator<U> for ThenComparingByKey<T, F>
where
T: Comparator<U>,
F: Fn(&U) -> W,
U: ?Sized,
W: Ord,
{
fn compare(&self, a: &U, b: &U) -> Ordering {
......@@ -66,6 +69,7 @@ where
pub fn comparing<F, T, U>(f: F) -> Comparing<F>
where
F: Fn(&T) -> U,
T: ?Sized,
U: Ord,
{
Comparing { f }
......@@ -86,6 +90,7 @@ impl<F> fmt::Debug for Comparing<F> {
impl<F, T, U> Comparator<T> for Comparing<F>
where
F: Fn(&T) -> U,
T: ?Sized,
U: Ord,
{
fn compare(&self, a: &T, b: &T) -> Ordering {
......@@ -112,7 +117,7 @@ impl fmt::Debug for NaturalOrder {
impl<T> Comparator<T> for NaturalOrder
where
T: Ord,
T: ?Sized + Ord,
{
fn compare(&self, a: &T, b: &T) -> Ordering {
a.cmp(b)
......@@ -138,7 +143,7 @@ impl fmt::Debug for ReverseOrder {
impl<T> Comparator<T> for ReverseOrder
where
T: Ord,
T: ?Sized + Ord,
{
fn compare(&self, a: &T, b: &T) -> Ordering {
b.cmp(a)
......
......@@ -15,7 +15,10 @@ use combinators::{Reversed, ThenComparing, ThenComparingByKey};
use core::cmp::Ordering;
/// An interface for dealing with comparators.
pub trait Comparator<T> {
pub trait Comparator<T>
where
T: ?Sized,
{
/// Compares its two arguments for order.
fn compare(&self, a: &T, b: &T) -> Ordering;
......@@ -69,6 +72,7 @@ pub trait Comparator<T> {
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)
......@@ -90,6 +94,9 @@ where
/// 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 {
pub fn as_fn<T>(comparator: impl Comparator<T>) -> impl Fn(&T, &T) -> Ordering
where
T: ?Sized,
{
move |a, b| comparator.compare(a, b)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment