Skip to content

Commit f5a367c

Browse files
committed
Update based on RangeBounds trait being moved to libcore.
1 parent 74abffe commit f5a367c

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

src/libcore/ops/range.rs

+63-12
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
109109
/// assert!(!(3..2).contains(3));
110110
/// ```
111111
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
112-
pub fn contains(&self, item: Idx) -> bool {
113-
(self.start <= item) && (item < self.end)
112+
pub fn contains<U>(&self, item: &U) -> bool
113+
where
114+
Idx: PartialOrd<U>,
115+
U: ?Sized,
116+
{
117+
<Self as RangeBounds<Idx>>::contains(self, item)
114118
}
115119

116120
/// Returns `true` if the range contains no items.
@@ -179,7 +183,6 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
179183
}
180184
}
181185

182-
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
183186
impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
184187
/// Returns `true` if `item` is contained in the range.
185188
///
@@ -192,8 +195,13 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
192195
/// assert!( (3..).contains(3));
193196
/// assert!( (3..).contains(1_000_000_000));
194197
/// ```
195-
pub fn contains(&self, item: Idx) -> bool {
196-
(self.start <= item)
198+
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
199+
pub fn contains<U>(&self, item: &U) -> bool
200+
where
201+
Idx: PartialOrd<U>,
202+
U: ?Sized,
203+
{
204+
<Self as RangeBounds<Idx>>::contains(self, item)
197205
}
198206
}
199207

@@ -250,7 +258,6 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
250258
}
251259
}
252260

253-
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
254261
impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
255262
/// Returns `true` if `item` is contained in the range.
256263
///
@@ -263,8 +270,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
263270
/// assert!( (..5).contains(4));
264271
/// assert!(!(..5).contains(5));
265272
/// ```
266-
pub fn contains(&self, item: Idx) -> bool {
267-
(item < self.end)
273+
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
274+
pub fn contains<U>(&self, item: &U) -> bool
275+
where
276+
Idx: PartialOrd<U>,
277+
U: ?Sized,
278+
{
279+
<Self as RangeBounds<Idx>>::contains(self, item)
268280
}
269281
}
270282

@@ -328,8 +340,12 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
328340
/// assert!(!(3..=2).contains(3));
329341
/// ```
330342
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
331-
pub fn contains(&self, item: Idx) -> bool {
332-
self.start <= item && item <= self.end
343+
pub fn contains<U>(&self, item: &U) -> bool
344+
where
345+
Idx: PartialOrd<U>,
346+
U: ?Sized,
347+
{
348+
<Self as RangeBounds<Idx>>::contains(self, item)
333349
}
334350

335351
/// Returns `true` if the range contains no items.
@@ -435,8 +451,13 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
435451
/// assert!( (..=5).contains(5));
436452
/// assert!(!(..=5).contains(6));
437453
/// ```
438-
pub fn contains(&self, item: Idx) -> bool {
439-
(item <= self.end)
454+
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
455+
pub fn contains<U>(&self, item: &U) -> bool
456+
where
457+
Idx: PartialOrd<U>,
458+
U: ?Sized,
459+
{
460+
<Self as RangeBounds<Idx>>::contains(self, item)
440461
}
441462
}
442463

@@ -537,6 +558,36 @@ pub trait RangeBounds<T: ?Sized> {
537558
/// # }
538559
/// ```
539560
fn end(&self) -> Bound<&T>;
561+
562+
/// Returns `true` if `item` is contained in the range.
563+
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
564+
fn contains<U>(&self, item: &U) -> bool
565+
where
566+
T: PartialOrd<U>,
567+
U: ?Sized,
568+
{
569+
match self.start() {
570+
Included(ref start) => if *start > item {
571+
return false;
572+
},
573+
Excluded(ref start) => if *start >= item {
574+
return false;
575+
},
576+
Unbounded => (),
577+
};
578+
579+
match self.end() {
580+
Included(ref end) => if *end < item {
581+
return false;
582+
},
583+
Excluded(ref end) => if *end <= item {
584+
return false;
585+
},
586+
Unbounded => (),
587+
}
588+
589+
true
590+
}
540591
}
541592

542593
use self::Bound::{Excluded, Included, Unbounded};

src/librustc_errors/emitter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1389,8 +1389,8 @@ fn num_overlap(a_start: usize, a_end: usize, b_start: usize, b_end:usize, inclus
13891389
} else {
13901390
0
13911391
};
1392-
(b_start..b_end + extra).contains(a_start) ||
1393-
(a_start..a_end + extra).contains(b_start)
1392+
(b_start..b_end + extra).contains(&a_start) ||
1393+
(a_start..a_end + extra).contains(&b_start)
13941394
}
13951395
fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
13961396
num_overlap(a1.start_col, a1.end_col + padding, a2.start_col, a2.end_col, false)

src/librustc_mir/borrow_check/nll/universal_regions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,18 @@ impl<'tcx> UniversalRegions<'tcx> {
259259

260260
/// True if `r` is a member of this set of universal regions.
261261
pub fn is_universal_region(&self, r: RegionVid) -> bool {
262-
(FIRST_GLOBAL_INDEX..self.num_universals).contains(r.index())
262+
(FIRST_GLOBAL_INDEX..self.num_universals).contains(&r.index())
263263
}
264264

265265
/// Classifies `r` as a universal region, returning `None` if this
266266
/// is not a member of this set of universal regions.
267267
pub fn region_classification(&self, r: RegionVid) -> Option<RegionClassification> {
268268
let index = r.index();
269-
if (FIRST_GLOBAL_INDEX..self.first_extern_index).contains(index) {
269+
if (FIRST_GLOBAL_INDEX..self.first_extern_index).contains(&index) {
270270
Some(RegionClassification::Global)
271-
} else if (self.first_extern_index..self.first_local_index).contains(index) {
271+
} else if (self.first_extern_index..self.first_local_index).contains(&index) {
272272
Some(RegionClassification::External)
273-
} else if (self.first_local_index..self.num_universals).contains(index) {
273+
} else if (self.first_local_index..self.num_universals).contains(&index) {
274274
Some(RegionClassification::Local)
275275
} else {
276276
None

0 commit comments

Comments
 (0)