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
//! Module with comparison implementations for `U256`.
//!
//! `PartialEq` and `PartialOrd` implementations for `u128` are also provided
//! to allow notation such as:
//!
//! ```
//! # use ethnum::U256;
//! assert_eq!(U256::new(42), 42);
//! assert!(U256::ONE > 0 && U256::ZERO == 0);
//! ```

use crate::U256;
use core::cmp::Ordering;

impl Ord for U256 {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        match self.high().cmp(other.high()) {
            Ordering::Equal => self.low().cmp(other.low()),
            ordering => ordering,
        }
    }
}

impl PartialOrd for U256 {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq<u128> for U256 {
    #[inline]
    fn eq(&self, other: &u128) -> bool {
        *self.high() == 0 && self.low() == other
    }
}

impl PartialOrd<u128> for U256 {
    #[inline]
    fn partial_cmp(&self, rhs: &u128) -> Option<Ordering> {
        Some(if *self.high() == 0 {
            self.low().cmp(rhs)
        } else {
            Ordering::Greater
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::U256;
    use core::cmp::Ordering;

    #[test]
    fn cmp() {
        // 1e38
        let x = U256::from_words(0, 100000000000000000000000000000000000000);
        // 1e48
        let y = U256::from_words(2938735877, 18960114910927365649471927446130393088);
        assert!(x < y);
        assert_eq!(x.cmp(&y), Ordering::Less);
    }
}