rangemap/
range_wrapper.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Wrappers to allow storing (and sorting/searching)
// ranges as the keys of a `BTreeMap`.
//
// This wraps the range in two layers: one that lets us
// order ranges by their start (`RangeStartWrapper`),
// and then within that, one that lets us order them by
// their end (`RangeEndWrapper`). Ordinarily we'll use
// the former, but there are a couple of cases where we
// want to be able to do the latter for performance/convenience.
//
// This is made possible by a sneaky `Borrow` implementation
// which skirts the law about the borrowed representation
// having identical implementations of `Ord` etc., but shouldn't
// be a problem in practice because users of the crate can't
// access these special wrappers, and we are careful to uphold
// invariants that prevent observing any states where the
// differing implementations would produce different results.
//
// Specifically, we maintain the invariants
// that the order of range starts is the same as the order
// of range ends, and that no two stored ranges have the
// same start or end as each other.
//
// NOTE: Be very careful not to accidentally use these
// if you really do want to compare equality of the
// inner range!

use core::cmp::Ordering;
use core::ops::{Deref, Range, RangeInclusive};

//
// Range start wrapper
//

#[derive(Debug, Clone)]
pub struct RangeStartWrapper<T> {
    pub end_wrapper: RangeEndWrapper<T>,
}

impl<T> RangeStartWrapper<T> {
    pub fn new(range: Range<T>) -> RangeStartWrapper<T> {
        RangeStartWrapper {
            end_wrapper: RangeEndWrapper::new(range),
        }
    }
}

impl<T> PartialEq for RangeStartWrapper<T>
where
    T: PartialEq,
{
    fn eq(&self, other: &RangeStartWrapper<T>) -> bool {
        self.start == other.start
    }
}

impl<T> Eq for RangeStartWrapper<T> where T: Eq {}

impl<T> Ord for RangeStartWrapper<T>
where
    T: Ord,
{
    fn cmp(&self, other: &RangeStartWrapper<T>) -> Ordering {
        self.start.cmp(&other.start)
    }
}

impl<T> PartialOrd for RangeStartWrapper<T>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, other: &RangeStartWrapper<T>) -> Option<Ordering> {
        self.start.partial_cmp(&other.start)
    }
}

impl<T> core::borrow::Borrow<RangeEndWrapper<T>> for RangeStartWrapper<T> {
    fn borrow(&self) -> &RangeEndWrapper<T> {
        &self.end_wrapper
    }
}

// Avoid the need to tediously plumb through the layers of wrapper structs
// when you're just trying to access members of the inner range itself.
impl<T> Deref for RangeStartWrapper<T> {
    type Target = RangeEndWrapper<T>;

    fn deref(&self) -> &Self::Target {
        &self.end_wrapper
    }
}

//
// Range end wrapper
//

#[derive(Debug, Clone)]
pub struct RangeEndWrapper<T> {
    pub range: Range<T>,
}

impl<T> RangeEndWrapper<T> {
    pub fn new(range: Range<T>) -> RangeEndWrapper<T> {
        RangeEndWrapper { range }
    }
}

impl<T> PartialEq for RangeEndWrapper<T>
where
    T: PartialEq,
{
    fn eq(&self, other: &RangeEndWrapper<T>) -> bool {
        self.end == other.end
    }
}

impl<T> Eq for RangeEndWrapper<T> where T: Eq {}

impl<T> Ord for RangeEndWrapper<T>
where
    T: Ord,
{
    fn cmp(&self, other: &RangeEndWrapper<T>) -> Ordering {
        self.end.cmp(&other.end)
    }
}

impl<T> PartialOrd for RangeEndWrapper<T>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, other: &RangeEndWrapper<T>) -> Option<Ordering> {
        self.end.partial_cmp(&other.end)
    }
}

// Avoid the need to tediously plumb through the layers of wrapper structs
// when you're just trying to access members of the inner range itself.
impl<T> Deref for RangeEndWrapper<T> {
    type Target = Range<T>;

    fn deref(&self) -> &Self::Target {
        &self.range
    }
}

//
// RangeInclusive start wrapper
//

#[derive(Eq, Debug, Clone)]
pub struct RangeInclusiveStartWrapper<T> {
    pub end_wrapper: RangeInclusiveEndWrapper<T>,
}

impl<T> RangeInclusiveStartWrapper<T> {
    pub fn new(range: RangeInclusive<T>) -> RangeInclusiveStartWrapper<T> {
        RangeInclusiveStartWrapper {
            end_wrapper: RangeInclusiveEndWrapper::new(range),
        }
    }
}

impl<T> PartialEq for RangeInclusiveStartWrapper<T>
where
    T: PartialEq,
{
    fn eq(&self, other: &RangeInclusiveStartWrapper<T>) -> bool {
        self.start() == other.start()
    }
}

impl<T> Ord for RangeInclusiveStartWrapper<T>
where
    T: Ord,
{
    fn cmp(&self, other: &RangeInclusiveStartWrapper<T>) -> Ordering {
        self.start().cmp(other.start())
    }
}

impl<T> PartialOrd for RangeInclusiveStartWrapper<T>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, other: &RangeInclusiveStartWrapper<T>) -> Option<Ordering> {
        self.start().partial_cmp(other.start())
    }
}

impl<T> core::borrow::Borrow<RangeInclusiveEndWrapper<T>> for RangeInclusiveStartWrapper<T> {
    fn borrow(&self) -> &RangeInclusiveEndWrapper<T> {
        &self.end_wrapper
    }
}

// Avoid the need to tediously plumb through the layers of wrapper structs
// when you're just trying to access members of the inner range itself.
impl<T> Deref for RangeInclusiveStartWrapper<T> {
    type Target = RangeInclusiveEndWrapper<T>;

    fn deref(&self) -> &Self::Target {
        &self.end_wrapper
    }
}

//
// RangeInclusive end wrapper
//

#[derive(Eq, Debug, Clone)]
pub struct RangeInclusiveEndWrapper<T> {
    pub range: RangeInclusive<T>,
}

impl<T> RangeInclusiveEndWrapper<T> {
    pub fn new(range: RangeInclusive<T>) -> RangeInclusiveEndWrapper<T> {
        RangeInclusiveEndWrapper { range }
    }
}

impl<T> PartialEq for RangeInclusiveEndWrapper<T>
where
    T: PartialEq,
{
    fn eq(&self, other: &RangeInclusiveEndWrapper<T>) -> bool {
        self.end() == other.end()
    }
}

impl<T> Ord for RangeInclusiveEndWrapper<T>
where
    T: Ord,
{
    fn cmp(&self, other: &RangeInclusiveEndWrapper<T>) -> Ordering {
        self.end().cmp(other.end())
    }
}

impl<T> PartialOrd for RangeInclusiveEndWrapper<T>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, other: &RangeInclusiveEndWrapper<T>) -> Option<Ordering> {
        self.end().partial_cmp(other.end())
    }
}

// Avoid the need to tediously plumb through the layers of wrapper structs
// when you're just trying to access members of the inner range itself.
impl<T> Deref for RangeInclusiveEndWrapper<T> {
    type Target = RangeInclusive<T>;

    fn deref(&self) -> &Self::Target {
        &self.range
    }
}