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
84
85
86
87
88
89
90
91
92
93
94
95
96
use core::ops::{Bound, RangeBounds};
use super::*;
/// Range compatibilty type that support `Copy`. Will be replaced by the next `core::range::Range` type when stable.
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Range<Idx>
{
pub start: Idx,
pub end: Idx,
}
impl<Idx: Debug> Debug for Range<Idx> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
self.start.fmt(fmt)?;
write!(fmt, "..")?;
self.end.fmt(fmt)?;
Ok(())
}
}
impl<Idx> From<core::ops::Range<Idx>> for Range<Idx>
{
fn from(value: core::ops::Range<Idx>) -> Self {
Self { start: value.start, end: value.end }
}
}
impl<Idx> From<Range<Idx>> for core::ops::Range<Idx>
{
fn from(value: Range<Idx>) -> Self {
Self { start: value.start, end: value.end }
}
}
impl<Idx: PartialOrd<Idx>> Range<Idx> {
pub fn as_std(self) -> core::ops::Range<Idx> { self.into() }
/// Returns `true` if `item` is contained in the range.
///
/// # Examples
///
/// ```
/// assert!(!(3..5).contains(&2));
/// assert!( (3..5).contains(&3));
/// assert!( (3..5).contains(&4));
/// assert!(!(3..5).contains(&5));
///
/// assert!(!(3..3).contains(&3));
/// assert!(!(3..2).contains(&3));
///
/// assert!( (0.0..1.0).contains(&0.5));
/// assert!(!(0.0..1.0).contains(&f32::NAN));
/// assert!(!(0.0..f32::NAN).contains(&0.5));
/// assert!(!(f32::NAN..1.0).contains(&0.5));
/// ```
#[inline]
pub fn contains<U>(&self, item: &U) -> bool
where
Idx: PartialOrd<U>,
U: ?Sized + PartialOrd<Idx>,
{
<Self as RangeBounds<Idx>>::contains(self, item)
}
/// Returns `true` if the range contains no items.
///
/// # Examples
///
/// ```
/// assert!(!(3..5).is_empty());
/// assert!( (3..3).is_empty());
/// assert!( (3..2).is_empty());
/// ```
///
/// The range is empty if either side is incomparable:
///
/// ```
/// assert!(!(3.0..5.0).is_empty());
/// assert!( (3.0..f32::NAN).is_empty());
/// assert!( (f32::NAN..5.0).is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool
where
Idx: PartialOrd<Idx>,
{
!(self.start < self.end)
}
}
impl<T> RangeBounds<T> for Range<T> {
fn start_bound(&self) -> Bound<&T> {
Bound::Included(&self.start)
}
fn end_bound(&self) -> Bound<&T> {
Bound::Excluded(&self.end)
}
}