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
use std::cmp::Ordering;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum IntegerOrInfinity {
PositiveInfinity,
Integer(i64),
NegativeInfinity,
}
impl IntegerOrInfinity {
pub fn clamp_finite(self, min: i64, max: i64) -> i64 {
assert!(min <= max);
match self {
Self::Integer(i) => i.clamp(min, max),
Self::PositiveInfinity => max,
Self::NegativeInfinity => min,
}
}
pub fn as_integer(self) -> Option<i64> {
match self {
Self::Integer(i) => Some(i),
_ => None,
}
}
}
impl PartialEq<i64> for IntegerOrInfinity {
fn eq(&self, other: &i64) -> bool {
match self {
Self::Integer(i) => i == other,
_ => false,
}
}
}
impl PartialEq<IntegerOrInfinity> for i64 {
fn eq(&self, other: &IntegerOrInfinity) -> bool {
match other {
IntegerOrInfinity::Integer(i) => i == other,
_ => false,
}
}
}
impl PartialOrd<i64> for IntegerOrInfinity {
fn partial_cmp(&self, other: &i64) -> Option<Ordering> {
match self {
Self::PositiveInfinity => Some(Ordering::Greater),
Self::Integer(i) => i.partial_cmp(other),
Self::NegativeInfinity => Some(Ordering::Less),
}
}
}
impl PartialOrd<IntegerOrInfinity> for i64 {
fn partial_cmp(&self, other: &IntegerOrInfinity) -> Option<Ordering> {
match other {
IntegerOrInfinity::PositiveInfinity => Some(Ordering::Less),
IntegerOrInfinity::Integer(i) => self.partial_cmp(i),
IntegerOrInfinity::NegativeInfinity => Some(Ordering::Greater),
}
}
}