-
Notifications
You must be signed in to change notification settings - Fork 499
/
range_ext.rs
83 lines (73 loc) · 2.04 KB
/
range_ext.rs
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
use super::*;
pub(crate) trait RangeExt<T> {
fn range_contains(&self, i: &T) -> bool;
fn display(&self) -> DisplayRange<&Self> {
DisplayRange(self)
}
}
pub(crate) struct DisplayRange<T>(T);
impl Display for DisplayRange<&RangeInclusive<usize>> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.0.start() == self.0.end() {
write!(f, "{}", self.0.start())?;
} else if *self.0.end() == usize::MAX {
write!(f, "{} or more", self.0.start())?;
} else {
write!(f, "{} to {}", self.0.start(), self.0.end())?;
}
Ok(())
}
}
impl<T> RangeExt<T> for Range<T>
where
T: PartialOrd,
{
fn range_contains(&self, i: &T) -> bool {
i >= &self.start && i < &self.end
}
}
impl<T> RangeExt<T> for RangeInclusive<T>
where
T: PartialOrd,
{
fn range_contains(&self, i: &T) -> bool {
i >= self.start() && i <= self.end()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exclusive() {
assert!(!(0..0).range_contains(&0));
assert!(!(1..10).range_contains(&0));
assert!(!(1..10).range_contains(&10));
assert!(!(1..10).range_contains(&0));
assert!((0..1).range_contains(&0));
assert!((10..20).range_contains(&15));
}
#[test]
fn inclusive() {
assert!(!(0..=10).range_contains(&11));
assert!(!(1..=10).range_contains(&0));
assert!(!(5..=10).range_contains(&4));
assert!((0..=0).range_contains(&0));
assert!((0..=1).range_contains(&0));
assert!((0..=10).range_contains(&0));
assert!((0..=10).range_contains(&10));
assert!((0..=10).range_contains(&7));
assert!((1..=10).range_contains(&10));
assert!((10..=20).range_contains(&15));
}
#[test]
fn display() {
assert!(!(1..1).contains(&1));
assert!((1..1).is_empty());
assert!((5..5).is_empty());
assert_eq!((0..=0).display().to_string(), "0");
assert_eq!((1..=1).display().to_string(), "1");
assert_eq!((5..=5).display().to_string(), "5");
assert_eq!((5..=9).display().to_string(), "5 to 9");
assert_eq!((1..=usize::MAX).display().to_string(), "1 or more");
}
}