@@ -109,8 +109,12 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
109
109
/// assert!(!(3..2).contains(3));
110
110
/// ```
111
111
#[ 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)
114
118
}
115
119
116
120
/// Returns `true` if the range contains no items.
@@ -179,7 +183,6 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
179
183
}
180
184
}
181
185
182
- #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
183
186
impl < Idx : PartialOrd < Idx > > RangeFrom < Idx > {
184
187
/// Returns `true` if `item` is contained in the range.
185
188
///
@@ -192,8 +195,13 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
192
195
/// assert!( (3..).contains(3));
193
196
/// assert!( (3..).contains(1_000_000_000));
194
197
/// ```
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)
197
205
}
198
206
}
199
207
@@ -250,7 +258,6 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
250
258
}
251
259
}
252
260
253
- #[ unstable( feature = "range_contains" , reason = "recently added as per RFC" , issue = "32311" ) ]
254
261
impl < Idx : PartialOrd < Idx > > RangeTo < Idx > {
255
262
/// Returns `true` if `item` is contained in the range.
256
263
///
@@ -263,8 +270,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
263
270
/// assert!( (..5).contains(4));
264
271
/// assert!(!(..5).contains(5));
265
272
/// ```
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)
268
280
}
269
281
}
270
282
@@ -328,8 +340,12 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
328
340
/// assert!(!(3..=2).contains(3));
329
341
/// ```
330
342
#[ 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)
333
349
}
334
350
335
351
/// Returns `true` if the range contains no items.
@@ -435,8 +451,13 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
435
451
/// assert!( (..=5).contains(5));
436
452
/// assert!(!(..=5).contains(6));
437
453
/// ```
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)
440
461
}
441
462
}
442
463
@@ -537,6 +558,36 @@ pub trait RangeBounds<T: ?Sized> {
537
558
/// # }
538
559
/// ```
539
560
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
+ }
540
591
}
541
592
542
593
use self :: Bound :: { Excluded , Included , Unbounded } ;
0 commit comments