]> git.lizzy.rs Git - rust.git/blob - src/libcore/ops/range.rs
Auto merge of #49891 - cuviper:compiletest-crash, r=alexcrichton
[rust.git] / src / libcore / ops / range.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use fmt;
12
13 /// An unbounded range (`..`).
14 ///
15 /// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`.
16 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
17 ///
18 /// # Examples
19 ///
20 /// The `..` syntax is a `RangeFull`:
21 ///
22 /// ```
23 /// assert_eq!((..), std::ops::RangeFull);
24 /// ```
25 ///
26 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
27 /// a `for` loop directly. This won't compile:
28 ///
29 /// ```compile_fail,E0277
30 /// for i in .. {
31 ///    // ...
32 /// }
33 /// ```
34 ///
35 /// Used as a [slicing index], `RangeFull` produces the full array as a slice.
36 ///
37 /// ```
38 /// let arr = [0, 1, 2, 3];
39 /// assert_eq!(arr[ .. ], [0,1,2,3]);  // RangeFull
40 /// assert_eq!(arr[ ..3], [0,1,2  ]);
41 /// assert_eq!(arr[1.. ], [  1,2,3]);
42 /// assert_eq!(arr[1..3], [  1,2  ]);
43 /// ```
44 ///
45 /// [`IntoIterator`]: ../iter/trait.Iterator.html
46 /// [`Iterator`]: ../iter/trait.IntoIterator.html
47 /// [slicing index]: ../slice/trait.SliceIndex.html
48 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub struct RangeFull;
51
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl fmt::Debug for RangeFull {
54     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
55         write!(fmt, "..")
56     }
57 }
58
59 /// A (half-open) range bounded inclusively below and exclusively above
60 /// (`start..end`).
61 ///
62 /// The `Range` `start..end` contains all values with `x >= start` and
63 /// `x < end`.  It is empty unless `start < end`.
64 ///
65 /// # Examples
66 ///
67 /// ```
68 /// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
69 /// assert_eq!(3 + 4 + 5, (3..6).sum());
70 ///
71 /// let arr = ['a', 'b', 'c', 'd'];
72 /// assert_eq!(arr[ .. ], ['a', 'b', 'c', 'd']);
73 /// assert_eq!(arr[ ..3], ['a', 'b', 'c',    ]);
74 /// assert_eq!(arr[1.. ], [     'b', 'c', 'd']);
75 /// assert_eq!(arr[1..3], [     'b', 'c'     ]);  // Range
76 /// ```
77 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
78 #[stable(feature = "rust1", since = "1.0.0")]
79 pub struct Range<Idx> {
80     /// The lower bound of the range (inclusive).
81     #[stable(feature = "rust1", since = "1.0.0")]
82     pub start: Idx,
83     /// The upper bound of the range (exclusive).
84     #[stable(feature = "rust1", since = "1.0.0")]
85     pub end: Idx,
86 }
87
88 #[stable(feature = "rust1", since = "1.0.0")]
89 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
90     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
91         write!(fmt, "{:?}..{:?}", self.start, self.end)
92     }
93 }
94
95 impl<Idx: PartialOrd<Idx>> Range<Idx> {
96     /// Returns `true` if `item` is contained in the range.
97     ///
98     /// # Examples
99     ///
100     /// ```
101     /// #![feature(range_contains)]
102     ///
103     /// use std::f32;
104     ///
105     /// assert!(!(3..5).contains(&2));
106     /// assert!( (3..5).contains(&3));
107     /// assert!( (3..5).contains(&4));
108     /// assert!(!(3..5).contains(&5));
109     ///
110     /// assert!(!(3..3).contains(&3));
111     /// assert!(!(3..2).contains(&3));
112     ///
113     /// assert!( (0.0..1.0).contains(&0.5));
114     /// assert!(!(0.0..1.0).contains(&f32::NAN));
115     /// assert!(!(0.0..f32::NAN).contains(&0.5));
116     /// assert!(!(f32::NAN..1.0).contains(&0.5));
117     /// ```
118     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
119     pub fn contains<U>(&self, item: &U) -> bool
120     where
121         Idx: PartialOrd<U>,
122         U: ?Sized + PartialOrd<Idx>,
123     {
124         <Self as RangeBounds<Idx>>::contains(self, item)
125     }
126
127     /// Returns `true` if the range contains no items.
128     ///
129     /// # Examples
130     ///
131     /// ```
132     /// #![feature(range_is_empty)]
133     ///
134     /// assert!(!(3..5).is_empty());
135     /// assert!( (3..3).is_empty());
136     /// assert!( (3..2).is_empty());
137     /// ```
138     ///
139     /// The range is empty if either side is incomparable:
140     ///
141     /// ```
142     /// #![feature(range_is_empty)]
143     ///
144     /// use std::f32::NAN;
145     /// assert!(!(3.0..5.0).is_empty());
146     /// assert!( (3.0..NAN).is_empty());
147     /// assert!( (NAN..5.0).is_empty());
148     /// ```
149     #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
150     pub fn is_empty(&self) -> bool {
151         !(self.start < self.end)
152     }
153 }
154
155 /// A range only bounded inclusively below (`start..`).
156 ///
157 /// The `RangeFrom` `start..` contains all values with `x >= start`.
158 ///
159 /// *Note*: Currently, no overflow checking is done for the [`Iterator`]
160 /// implementation; if you use an integer range and the integer overflows, it
161 /// might panic in debug mode or create an endless loop in release mode. **This
162 /// overflow behavior might change in the future.**
163 ///
164 /// # Examples
165 ///
166 /// ```
167 /// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
168 /// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
169 ///
170 /// let arr = [0, 1, 2, 3];
171 /// assert_eq!(arr[ .. ], [0,1,2,3]);
172 /// assert_eq!(arr[ ..3], [0,1,2  ]);
173 /// assert_eq!(arr[1.. ], [  1,2,3]);  // RangeFrom
174 /// assert_eq!(arr[1..3], [  1,2  ]);
175 /// ```
176 ///
177 /// [`Iterator`]: ../iter/trait.IntoIterator.html
178 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
179 #[stable(feature = "rust1", since = "1.0.0")]
180 pub struct RangeFrom<Idx> {
181     /// The lower bound of the range (inclusive).
182     #[stable(feature = "rust1", since = "1.0.0")]
183     pub start: Idx,
184 }
185
186 #[stable(feature = "rust1", since = "1.0.0")]
187 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
188     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
189         write!(fmt, "{:?}..", self.start)
190     }
191 }
192
193 impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
194     /// Returns `true` if `item` is contained in the range.
195     ///
196     /// # Examples
197     ///
198     /// ```
199     /// #![feature(range_contains)]
200     ///
201     /// use std::f32;
202     ///
203     /// assert!(!(3..).contains(&2));
204     /// assert!( (3..).contains(&3));
205     /// assert!( (3..).contains(&1_000_000_000));
206     ///
207     /// assert!( (0.0..).contains(&0.5));
208     /// assert!(!(0.0..).contains(&f32::NAN));
209     /// assert!(!(f32::NAN..).contains(&0.5));
210     /// ```
211     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
212     pub fn contains<U>(&self, item: &U) -> bool
213     where
214         Idx: PartialOrd<U>,
215         U: ?Sized + PartialOrd<Idx>,
216     {
217         <Self as RangeBounds<Idx>>::contains(self, item)
218     }
219 }
220
221 /// A range only bounded exclusively above (`..end`).
222 ///
223 /// The `RangeTo` `..end` contains all values with `x < end`.
224 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
225 ///
226 /// # Examples
227 ///
228 /// The `..end` syntax is a `RangeTo`:
229 ///
230 /// ```
231 /// assert_eq!((..5), std::ops::RangeTo { end: 5 });
232 /// ```
233 ///
234 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
235 /// a `for` loop directly. This won't compile:
236 ///
237 /// ```compile_fail,E0277
238 /// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
239 /// // std::iter::Iterator` is not satisfied
240 /// for i in ..5 {
241 ///     // ...
242 /// }
243 /// ```
244 ///
245 /// When used as a [slicing index], `RangeTo` produces a slice of all array
246 /// elements before the index indicated by `end`.
247 ///
248 /// ```
249 /// let arr = [0, 1, 2, 3];
250 /// assert_eq!(arr[ .. ], [0,1,2,3]);
251 /// assert_eq!(arr[ ..3], [0,1,2  ]);  // RangeTo
252 /// assert_eq!(arr[1.. ], [  1,2,3]);
253 /// assert_eq!(arr[1..3], [  1,2  ]);
254 /// ```
255 ///
256 /// [`IntoIterator`]: ../iter/trait.Iterator.html
257 /// [`Iterator`]: ../iter/trait.IntoIterator.html
258 /// [slicing index]: ../slice/trait.SliceIndex.html
259 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
260 #[stable(feature = "rust1", since = "1.0.0")]
261 pub struct RangeTo<Idx> {
262     /// The upper bound of the range (exclusive).
263     #[stable(feature = "rust1", since = "1.0.0")]
264     pub end: Idx,
265 }
266
267 #[stable(feature = "rust1", since = "1.0.0")]
268 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
269     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
270         write!(fmt, "..{:?}", self.end)
271     }
272 }
273
274 impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
275     /// Returns `true` if `item` is contained in the range.
276     ///
277     /// # Examples
278     ///
279     /// ```
280     /// #![feature(range_contains)]
281     ///
282     /// use std::f32;
283     ///
284     /// assert!( (..5).contains(&-1_000_000_000));
285     /// assert!( (..5).contains(&4));
286     /// assert!(!(..5).contains(&5));
287     ///
288     /// assert!( (..1.0).contains(&0.5));
289     /// assert!(!(..1.0).contains(&f32::NAN));
290     /// assert!(!(..f32::NAN).contains(&0.5));
291     /// ```
292     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
293     pub fn contains<U>(&self, item: &U) -> bool
294     where
295         Idx: PartialOrd<U>,
296         U: ?Sized + PartialOrd<Idx>,
297     {
298         <Self as RangeBounds<Idx>>::contains(self, item)
299     }
300 }
301
302 /// An range bounded inclusively below and above (`start..=end`).
303 ///
304 /// The `RangeInclusive` `start..=end` contains all values with `x >= start`
305 /// and `x <= end`.  It is empty unless `start <= end`.
306 ///
307 /// This iterator is [fused], but the specific values of `start` and `end` after
308 /// iteration has finished are **unspecified** other than that [`.is_empty()`]
309 /// will return `true` once no more values will be produced.
310 ///
311 /// [fused]: ../iter/trait.FusedIterator.html
312 /// [`.is_empty()`]: #method.is_empty
313 ///
314 /// # Examples
315 ///
316 /// ```
317 /// #![feature(inclusive_range_fields)]
318 ///
319 /// assert_eq!((3..=5), std::ops::RangeInclusive { start: 3, end: 5 });
320 /// assert_eq!(3 + 4 + 5, (3..=5).sum());
321 ///
322 /// let arr = [0, 1, 2, 3];
323 /// assert_eq!(arr[ ..=2], [0,1,2  ]);
324 /// assert_eq!(arr[1..=2], [  1,2  ]);  // RangeInclusive
325 /// ```
326 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
327 #[stable(feature = "inclusive_range", since = "1.26.0")]
328 pub struct RangeInclusive<Idx> {
329     /// The lower bound of the range (inclusive).
330     #[unstable(feature = "inclusive_range_fields", issue = "49022")]
331     pub start: Idx,
332     /// The upper bound of the range (inclusive).
333     #[unstable(feature = "inclusive_range_fields", issue = "49022")]
334     pub end: Idx,
335 }
336
337 #[stable(feature = "inclusive_range", since = "1.26.0")]
338 impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
339     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
340         write!(fmt, "{:?}..={:?}", self.start, self.end)
341     }
342 }
343
344 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
345     /// Returns `true` if `item` is contained in the range.
346     ///
347     /// # Examples
348     ///
349     /// ```
350     /// #![feature(range_contains)]
351     ///
352     /// use std::f32;
353     ///
354     /// assert!(!(3..=5).contains(&2));
355     /// assert!( (3..=5).contains(&3));
356     /// assert!( (3..=5).contains(&4));
357     /// assert!( (3..=5).contains(&5));
358     /// assert!(!(3..=5).contains(&6));
359     ///
360     /// assert!( (3..=3).contains(&3));
361     /// assert!(!(3..=2).contains(&3));
362     ///
363     /// assert!( (0.0..=1.0).contains(&1.0));
364     /// assert!(!(0.0..=1.0).contains(&f32::NAN));
365     /// assert!(!(0.0..=f32::NAN).contains(&0.0));
366     /// assert!(!(f32::NAN..=1.0).contains(&1.0));
367     /// ```
368     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
369     pub fn contains<U>(&self, item: &U) -> bool
370     where
371         Idx: PartialOrd<U>,
372         U: ?Sized + PartialOrd<Idx>,
373     {
374         <Self as RangeBounds<Idx>>::contains(self, item)
375     }
376
377     /// Returns `true` if the range contains no items.
378     ///
379     /// # Examples
380     ///
381     /// ```
382     /// #![feature(range_is_empty)]
383     ///
384     /// assert!(!(3..=5).is_empty());
385     /// assert!(!(3..=3).is_empty());
386     /// assert!( (3..=2).is_empty());
387     /// ```
388     ///
389     /// The range is empty if either side is incomparable:
390     ///
391     /// ```
392     /// #![feature(range_is_empty)]
393     ///
394     /// use std::f32::NAN;
395     /// assert!(!(3.0..=5.0).is_empty());
396     /// assert!( (3.0..=NAN).is_empty());
397     /// assert!( (NAN..=5.0).is_empty());
398     /// ```
399     ///
400     /// This method returns `true` after iteration has finished:
401     ///
402     /// ```
403     /// #![feature(range_is_empty)]
404     ///
405     /// let mut r = 3..=5;
406     /// for _ in r.by_ref() {}
407     /// // Precise field values are unspecified here
408     /// assert!(r.is_empty());
409     /// ```
410     #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
411     pub fn is_empty(&self) -> bool {
412         !(self.start <= self.end)
413     }
414 }
415
416 /// A range only bounded inclusively above (`..=end`).
417 ///
418 /// The `RangeToInclusive` `..=end` contains all values with `x <= end`.
419 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
420 ///
421 /// # Examples
422 ///
423 /// The `..=end` syntax is a `RangeToInclusive`:
424 ///
425 /// ```
426 /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 });
427 /// ```
428 ///
429 /// It does not have an [`IntoIterator`] implementation, so you can't use it in a
430 /// `for` loop directly. This won't compile:
431 ///
432 /// ```compile_fail,E0277
433 /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
434 /// // std::iter::Iterator` is not satisfied
435 /// for i in ..=5 {
436 ///     // ...
437 /// }
438 /// ```
439 ///
440 /// When used as a [slicing index], `RangeToInclusive` produces a slice of all
441 /// array elements up to and including the index indicated by `end`.
442 ///
443 /// ```
444 /// let arr = [0, 1, 2, 3];
445 /// assert_eq!(arr[ ..=2], [0,1,2  ]);  // RangeToInclusive
446 /// assert_eq!(arr[1..=2], [  1,2  ]);
447 /// ```
448 ///
449 /// [`IntoIterator`]: ../iter/trait.Iterator.html
450 /// [`Iterator`]: ../iter/trait.IntoIterator.html
451 /// [slicing index]: ../slice/trait.SliceIndex.html
452 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
453 #[stable(feature = "inclusive_range", since = "1.26.0")]
454 pub struct RangeToInclusive<Idx> {
455     /// The upper bound of the range (inclusive)
456     #[stable(feature = "inclusive_range", since = "1.26.0")]
457     pub end: Idx,
458 }
459
460 #[stable(feature = "inclusive_range", since = "1.26.0")]
461 impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
462     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
463         write!(fmt, "..={:?}", self.end)
464     }
465 }
466
467 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
468 impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
469     /// Returns `true` if `item` is contained in the range.
470     ///
471     /// # Examples
472     ///
473     /// ```
474     /// #![feature(range_contains)]
475     ///
476     /// use std::f32;
477     ///
478     /// assert!( (..=5).contains(&-1_000_000_000));
479     /// assert!( (..=5).contains(&5));
480     /// assert!(!(..=5).contains(&6));
481     ///
482     /// assert!( (..=1.0).contains(&1.0));
483     /// assert!(!(..=1.0).contains(&f32::NAN));
484     /// assert!(!(..=f32::NAN).contains(&0.5));
485     /// ```
486     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
487     pub fn contains<U>(&self, item: &U) -> bool
488     where
489         Idx: PartialOrd<U>,
490         U: ?Sized + PartialOrd<Idx>,
491     {
492         <Self as RangeBounds<Idx>>::contains(self, item)
493     }
494 }
495
496 // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
497 // because underflow would be possible with (..0).into()
498
499 /// An endpoint of a range of keys.
500 ///
501 /// # Examples
502 ///
503 /// `Bound`s are range endpoints:
504 ///
505 /// ```
506 /// #![feature(collections_range)]
507 ///
508 /// use std::ops::Bound::*;
509 /// use std::ops::RangeBounds;
510 ///
511 /// assert_eq!((..100).start(), Unbounded);
512 /// assert_eq!((1..12).start(), Included(&1));
513 /// assert_eq!((1..12).end(), Excluded(&12));
514 /// ```
515 ///
516 /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
517 /// Note that in most cases, it's better to use range syntax (`1..5`) instead.
518 ///
519 /// ```
520 /// use std::collections::BTreeMap;
521 /// use std::ops::Bound::{Excluded, Included, Unbounded};
522 ///
523 /// let mut map = BTreeMap::new();
524 /// map.insert(3, "a");
525 /// map.insert(5, "b");
526 /// map.insert(8, "c");
527 ///
528 /// for (key, value) in map.range((Excluded(3), Included(8))) {
529 ///     println!("{}: {}", key, value);
530 /// }
531 ///
532 /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
533 /// ```
534 ///
535 /// [`BTreeMap::range`]: ../../std/collections/btree_map/struct.BTreeMap.html#method.range
536 #[stable(feature = "collections_bound", since = "1.17.0")]
537 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
538 pub enum Bound<T> {
539     /// An inclusive bound.
540     #[stable(feature = "collections_bound", since = "1.17.0")]
541     Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
542     /// An exclusive bound.
543     #[stable(feature = "collections_bound", since = "1.17.0")]
544     Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
545     /// An infinite endpoint. Indicates that there is no bound in this direction.
546     #[stable(feature = "collections_bound", since = "1.17.0")]
547     Unbounded,
548 }
549
550 #[unstable(feature = "collections_range",
551            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
552            issue = "30877")]
553 /// `RangeBounds` is implemented by Rust's built-in range types, produced
554 /// by range syntax like `..`, `a..`, `..b` or `c..d`.
555 pub trait RangeBounds<T: ?Sized> {
556     /// Start index bound.
557     ///
558     /// Returns the start value as a `Bound`.
559     ///
560     /// # Examples
561     ///
562     /// ```
563     /// #![feature(collections_range)]
564     ///
565     /// # fn main() {
566     /// use std::ops::Bound::*;
567     /// use std::ops::RangeBounds;
568     ///
569     /// assert_eq!((..10).start(), Unbounded);
570     /// assert_eq!((3..10).start(), Included(&3));
571     /// # }
572     /// ```
573     fn start(&self) -> Bound<&T>;
574
575     /// End index bound.
576     ///
577     /// Returns the end value as a `Bound`.
578     ///
579     /// # Examples
580     ///
581     /// ```
582     /// #![feature(collections_range)]
583     ///
584     /// # fn main() {
585     /// use std::ops::Bound::*;
586     /// use std::ops::RangeBounds;
587     ///
588     /// assert_eq!((3..).end(), Unbounded);
589     /// assert_eq!((3..10).end(), Excluded(&10));
590     /// # }
591     /// ```
592     fn end(&self) -> Bound<&T>;
593
594
595     /// Returns `true` if `item` is contained in the range.
596     ///
597     /// # Examples
598     ///
599     /// ```
600     /// #![feature(range_contains)]
601     ///
602     /// use std::f32;
603     ///
604     /// assert!( (3..5).contains(&4));
605     /// assert!(!(3..5).contains(&2));
606     ///
607     /// assert!( (0.0..1.0).contains(&0.5));
608     /// assert!(!(0.0..1.0).contains(&f32::NAN));
609     /// assert!(!(0.0..f32::NAN).contains(&0.5));
610     /// assert!(!(f32::NAN..1.0).contains(&0.5));
611     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
612     fn contains<U>(&self, item: &U) -> bool
613     where
614         T: PartialOrd<U>,
615         U: ?Sized + PartialOrd<T>,
616     {
617         (match self.start() {
618             Included(ref start) => *start <= item,
619             Excluded(ref start) => *start < item,
620             Unbounded => true,
621         })
622         &&
623         (match self.end() {
624             Included(ref end) => item <= *end,
625             Excluded(ref end) => item < *end,
626             Unbounded => true,
627         })
628     }
629 }
630
631 use self::Bound::{Excluded, Included, Unbounded};
632
633 #[unstable(feature = "collections_range",
634            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
635            issue = "30877")]
636 impl<T: ?Sized> RangeBounds<T> for RangeFull {
637     fn start(&self) -> Bound<&T> {
638         Unbounded
639     }
640     fn end(&self) -> Bound<&T> {
641         Unbounded
642     }
643 }
644
645 #[unstable(feature = "collections_range",
646            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
647            issue = "30877")]
648 impl<T> RangeBounds<T> for RangeFrom<T> {
649     fn start(&self) -> Bound<&T> {
650         Included(&self.start)
651     }
652     fn end(&self) -> Bound<&T> {
653         Unbounded
654     }
655 }
656
657 #[unstable(feature = "collections_range",
658            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
659            issue = "30877")]
660 impl<T> RangeBounds<T> for RangeTo<T> {
661     fn start(&self) -> Bound<&T> {
662         Unbounded
663     }
664     fn end(&self) -> Bound<&T> {
665         Excluded(&self.end)
666     }
667 }
668
669 #[unstable(feature = "collections_range",
670            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
671            issue = "30877")]
672 impl<T> RangeBounds<T> for Range<T> {
673     fn start(&self) -> Bound<&T> {
674         Included(&self.start)
675     }
676     fn end(&self) -> Bound<&T> {
677         Excluded(&self.end)
678     }
679 }
680
681 #[unstable(feature = "collections_range",
682            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
683            issue = "30877")]
684 impl<T> RangeBounds<T> for RangeInclusive<T> {
685     fn start(&self) -> Bound<&T> {
686         Included(&self.start)
687     }
688     fn end(&self) -> Bound<&T> {
689         Included(&self.end)
690     }
691 }
692
693 #[unstable(feature = "collections_range",
694            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
695            issue = "30877")]
696 impl<T> RangeBounds<T> for RangeToInclusive<T> {
697     fn start(&self) -> Bound<&T> {
698         Unbounded
699     }
700     fn end(&self) -> Bound<&T> {
701         Included(&self.end)
702     }
703 }
704
705 #[unstable(feature = "collections_range",
706            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
707            issue = "30877")]
708 impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
709     fn start(&self) -> Bound<&T> {
710         match *self {
711             (Included(ref start), _) => Included(start),
712             (Excluded(ref start), _) => Excluded(start),
713             (Unbounded, _)           => Unbounded,
714         }
715     }
716
717     fn end(&self) -> Bound<&T> {
718         match *self {
719             (_, Included(ref end)) => Included(end),
720             (_, Excluded(ref end)) => Excluded(end),
721             (_, Unbounded)         => Unbounded,
722         }
723     }
724 }
725
726 #[unstable(feature = "collections_range",
727            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
728            issue = "30877")]
729 impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
730     fn start(&self) -> Bound<&T> {
731         self.0
732     }
733
734     fn end(&self) -> Bound<&T> {
735         self.1
736     }
737 }
738
739 #[unstable(feature = "collections_range",
740            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
741            issue = "30877")]
742 impl<'a, T> RangeBounds<T> for RangeFrom<&'a T> {
743     fn start(&self) -> Bound<&T> {
744         Included(self.start)
745     }
746     fn end(&self) -> Bound<&T> {
747         Unbounded
748     }
749 }
750
751 #[unstable(feature = "collections_range",
752            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
753            issue = "30877")]
754 impl<'a, T> RangeBounds<T> for RangeTo<&'a T> {
755     fn start(&self) -> Bound<&T> {
756         Unbounded
757     }
758     fn end(&self) -> Bound<&T> {
759         Excluded(self.end)
760     }
761 }
762
763 #[unstable(feature = "collections_range",
764            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
765            issue = "30877")]
766 impl<'a, T> RangeBounds<T> for Range<&'a T> {
767     fn start(&self) -> Bound<&T> {
768         Included(self.start)
769     }
770     fn end(&self) -> Bound<&T> {
771         Excluded(self.end)
772     }
773 }
774
775 #[unstable(feature = "collections_range",
776            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
777            issue = "30877")]
778 impl<'a, T> RangeBounds<T> for RangeInclusive<&'a T> {
779     fn start(&self) -> Bound<&T> {
780         Included(self.start)
781     }
782     fn end(&self) -> Bound<&T> {
783         Included(self.end)
784     }
785 }
786
787 #[unstable(feature = "collections_range",
788            reason = "might be replaced with `Into<_>` and a type containing two `Bound` values",
789            issue = "30877")]
790 impl<'a, T> RangeBounds<T> for RangeToInclusive<&'a T> {
791     fn start(&self) -> Bound<&T> {
792         Unbounded
793     }
794     fn end(&self) -> Bound<&T> {
795         Included(self.end)
796     }
797 }