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
#![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)
}