Skip to main content

typefun/
ord.rs

1use core::cmp::Ordering as StdOrdering;
2
3use crate::{
4    bool::{BoolT, False, True},
5    uninhabited::PhantomUninhabited,
6};
7
8/// The result of comparing two values.
9pub trait Ordering: OrderingT + private_ord::Sealed {
10    const VALUE: StdOrdering;
11}
12
13/// A type representing the comparision of two values.
14pub trait OrderingT {
15    type Type: Ordering;
16}
17
18/// Represents an ordering where a compared value is less than another.
19pub enum Less {}
20impl Ordering for Less {
21    const VALUE: StdOrdering = StdOrdering::Less;
22}
23impl OrderingT for Less {
24    type Type = Self;
25}
26
27/// Represents an ordering where a compared value is equal to another.
28pub enum Equal {}
29impl Ordering for Equal {
30    const VALUE: StdOrdering = StdOrdering::Equal;
31}
32impl OrderingT for Equal {
33    type Type = Self;
34}
35
36/// Represents an ordering where a compared value is greater than another.
37pub enum Greater {}
38impl Ordering for Greater {
39    const VALUE: StdOrdering = StdOrdering::Greater;
40}
41impl OrderingT for Greater {
42    type Type = Self;
43}
44
45/// Reprensents the boolean value `a == b`.
46pub struct OrderingEqBase<A: Ordering, B: Ordering>(PhantomUninhabited<(A, B)>);
47impl BoolT for OrderingEqBase<Less, Less> {
48    type Type = True;
49}
50impl BoolT for OrderingEqBase<Equal, Equal> {
51    type Type = True;
52}
53impl BoolT for OrderingEqBase<Greater, Greater> {
54    type Type = True;
55}
56impl BoolT for OrderingEqBase<Less, Equal> {
57    type Type = False;
58}
59impl BoolT for OrderingEqBase<Less, Greater> {
60    type Type = False;
61}
62impl BoolT for OrderingEqBase<Equal, Less> {
63    type Type = False;
64}
65impl BoolT for OrderingEqBase<Equal, Greater> {
66    type Type = False;
67}
68impl BoolT for OrderingEqBase<Greater, Less> {
69    type Type = False;
70}
71impl BoolT for OrderingEqBase<Greater, Equal> {
72    type Type = False;
73}
74
75pub struct OrderingEq<A: OrderingT, B: OrderingT>(PhantomUninhabited<(A, B)>);
76impl<A: OrderingT, B: OrderingT> BoolT for OrderingEq<A, B>
77where
78    OrderingEqBase<A::Type, B::Type>: BoolT,
79{
80    type Type = <OrderingEqBase<A::Type, B::Type> as BoolT>::Type;
81}
82
83mod private_ord {
84    use super::*;
85
86    pub trait Sealed {}
87    impl Sealed for Less {}
88    impl Sealed for Equal {}
89    impl Sealed for Greater {}
90}