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
//! Ordering relations

use core::cmp::Ordering;
use core::cmp::Ordering::*;

use eq::*;

pub trait PartialOrderRelation<A: ?Sized>: PartialEquivalenceRelation<A> {
    fn partial_cmp(&self, &A, &A) -> Option<Ordering>;

    #[inline]
    fn less(&self, x: &A, y: &A) -> bool { Some(Less) == self.partial_cmp(x, y) }

    #[inline]
    fn less_or_equal(&self, x: &A, y: &A) -> bool {
        match self.partial_cmp(x, y) {
            Some(Less) => true,
            Some(Equal) => true,
            _ => false,
        }
    }
}

pub trait TotalOrderRelation<A: ?Sized>: PartialOrderRelation<A> + EquivalenceRelation<A> {
    fn cmp(&self, &A, &A) -> Ordering;
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct Dual<Rel>(Rel);

impl<A: ?Sized + PartialOrd> PartialOrderRelation<A> for ::Core {
    fn partial_cmp(&self, x: &A, y: &A) -> Option<Ordering> { PartialOrd::partial_cmp(x, y) }
}

impl<A: ?Sized + Ord> TotalOrderRelation<A> for ::Core {
    fn cmp(&self, x: &A, y: &A) -> Ordering { Ord::cmp(x, y) }
}

impl<A: ?Sized, Rel: PartialEquivalenceRelation<A>> PartialEquivalenceRelation<A> for Dual<Rel> {
    fn equal(&self, x: &A, y: &A) -> bool { self.0.equal(x, y) }
    fn inequal(&self, x: &A, y: &A) -> bool { self.0.inequal(x, y) }
}

impl<A: ?Sized, Rel: EquivalenceRelation<A>> EquivalenceRelation<A> for Dual<Rel> {}

impl<A: ?Sized, Rel: PartialOrderRelation<A>> PartialOrderRelation<A> for Dual<Rel> {
    fn partial_cmp(&self, x: &A, y: &A) -> Option<Ordering> { self.0.partial_cmp(y, x) }
}

impl<A: ?Sized, Rel: TotalOrderRelation<A>> TotalOrderRelation<A> for Dual<Rel> {
    fn cmp(&self, x: &A, y: &A) -> Ordering { self.0.cmp(y, x) }
}