@@ -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" ) ]
183186impl < 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" ) ]
254261impl < 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
542593use self :: Bound :: { Excluded , Included , Unbounded } ;
0 commit comments