]>
Commit | Line | Data |
---|---|---|
48663c56 | 1 | use crate::fmt; |
ba9703b0 | 2 | use crate::hash::Hash; |
041b39d2 | 3 | |
3b2f2976 | 4 | /// An unbounded range (`..`). |
041b39d2 | 5 | /// |
3b2f2976 XL |
6 | /// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`. |
7 | /// It cannot serve as an [`Iterator`] because it doesn't have a starting point. | |
041b39d2 XL |
8 | /// |
9 | /// # Examples | |
10 | /// | |
11 | /// The `..` syntax is a `RangeFull`: | |
12 | /// | |
13 | /// ``` | |
14 | /// assert_eq!((..), std::ops::RangeFull); | |
15 | /// ``` | |
16 | /// | |
3b2f2976 XL |
17 | /// It does not have an [`IntoIterator`] implementation, so you can't use it in |
18 | /// a `for` loop directly. This won't compile: | |
041b39d2 XL |
19 | /// |
20 | /// ```compile_fail,E0277 | |
21 | /// for i in .. { | |
29967ef6 | 22 | /// // ... |
041b39d2 XL |
23 | /// } |
24 | /// ``` | |
25 | /// | |
3b2f2976 | 26 | /// Used as a [slicing index], `RangeFull` produces the full array as a slice. |
041b39d2 XL |
27 | /// |
28 | /// ``` | |
532ac7d7 | 29 | /// let arr = [0, 1, 2, 3, 4]; |
29967ef6 XL |
30 | /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); // This is the `RangeFull` |
31 | /// assert_eq!(arr[ .. 3], [0, 1, 2 ]); | |
32 | /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); | |
33 | /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); | |
34 | /// assert_eq!(arr[1.. 3], [ 1, 2 ]); | |
35 | /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); | |
041b39d2 | 36 | /// ``` |
3b2f2976 | 37 | /// |
3dfed10e | 38 | /// [slicing index]: crate::slice::SliceIndex |
1b1a35ee | 39 | #[lang = "RangeFull"] |
83c7162d | 40 | #[doc(alias = "..")] |
3dfed10e | 41 | #[derive(Copy, Clone, Default, PartialEq, Eq, Hash)] |
041b39d2 XL |
42 | #[stable(feature = "rust1", since = "1.0.0")] |
43 | pub struct RangeFull; | |
44 | ||
45 | #[stable(feature = "rust1", since = "1.0.0")] | |
46 | impl fmt::Debug for RangeFull { | |
48663c56 | 47 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
041b39d2 XL |
48 | write!(fmt, "..") |
49 | } | |
50 | } | |
51 | ||
3b2f2976 XL |
52 | /// A (half-open) range bounded inclusively below and exclusively above |
53 | /// (`start..end`). | |
041b39d2 | 54 | /// |
29967ef6 XL |
55 | /// The range `start..end` contains all values with `start <= x < end`. |
56 | /// It is empty if `start >= end`. | |
041b39d2 XL |
57 | /// |
58 | /// # Examples | |
59 | /// | |
29967ef6 XL |
60 | /// The `start..end` syntax is a `Range`: |
61 | /// | |
041b39d2 | 62 | /// ``` |
3b2f2976 XL |
63 | /// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 }); |
64 | /// assert_eq!(3 + 4 + 5, (3..6).sum()); | |
29967ef6 | 65 | /// ``` |
3b2f2976 | 66 | /// |
29967ef6 | 67 | /// ``` |
532ac7d7 | 68 | /// let arr = [0, 1, 2, 3, 4]; |
29967ef6 XL |
69 | /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); |
70 | /// assert_eq!(arr[ .. 3], [0, 1, 2 ]); | |
71 | /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); | |
72 | /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); | |
73 | /// assert_eq!(arr[1.. 3], [ 1, 2 ]); // This is a `Range` | |
74 | /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); | |
041b39d2 | 75 | /// ``` |
1b1a35ee | 76 | #[lang = "Range"] |
83c7162d | 77 | #[doc(alias = "..")] |
3dfed10e | 78 | #[derive(Clone, Default, PartialEq, Eq, Hash)] // not Copy -- see #27186 |
041b39d2 XL |
79 | #[stable(feature = "rust1", since = "1.0.0")] |
80 | pub struct Range<Idx> { | |
81 | /// The lower bound of the range (inclusive). | |
82 | #[stable(feature = "rust1", since = "1.0.0")] | |
83 | pub start: Idx, | |
84 | /// The upper bound of the range (exclusive). | |
85 | #[stable(feature = "rust1", since = "1.0.0")] | |
86 | pub end: Idx, | |
87 | } | |
88 | ||
89 | #[stable(feature = "rust1", since = "1.0.0")] | |
90 | impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> { | |
48663c56 | 91 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
532ac7d7 XL |
92 | self.start.fmt(fmt)?; |
93 | write!(fmt, "..")?; | |
94 | self.end.fmt(fmt)?; | |
95 | Ok(()) | |
041b39d2 XL |
96 | } |
97 | } | |
98 | ||
9ffffee4 | 99 | impl<Idx: ~const PartialOrd<Idx>> Range<Idx> { |
3b2f2976 XL |
100 | /// Returns `true` if `item` is contained in the range. |
101 | /// | |
041b39d2 XL |
102 | /// # Examples |
103 | /// | |
104 | /// ``` | |
83c7162d XL |
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)); | |
041b39d2 | 117 | /// ``` |
532ac7d7 | 118 | #[stable(feature = "range_contains", since = "1.35.0")] |
9ffffee4 FG |
119 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
120 | pub const fn contains<U>(&self, item: &U) -> bool | |
83c7162d | 121 | where |
9ffffee4 FG |
122 | Idx: ~const PartialOrd<U>, |
123 | U: ?Sized + ~const PartialOrd<Idx>, | |
83c7162d XL |
124 | { |
125 | <Self as RangeBounds<Idx>>::contains(self, item) | |
041b39d2 | 126 | } |
0531ce1d XL |
127 | |
128 | /// Returns `true` if the range contains no items. | |
129 | /// | |
130 | /// # Examples | |
131 | /// | |
132 | /// ``` | |
0531ce1d XL |
133 | /// assert!(!(3..5).is_empty()); |
134 | /// assert!( (3..3).is_empty()); | |
135 | /// assert!( (3..2).is_empty()); | |
136 | /// ``` | |
137 | /// | |
138 | /// The range is empty if either side is incomparable: | |
139 | /// | |
140 | /// ``` | |
0531ce1d | 141 | /// assert!(!(3.0..5.0).is_empty()); |
ba9703b0 XL |
142 | /// assert!( (3.0..f32::NAN).is_empty()); |
143 | /// assert!( (f32::NAN..5.0).is_empty()); | |
0531ce1d | 144 | /// ``` |
3dfed10e | 145 | #[stable(feature = "range_is_empty", since = "1.47.0")] |
9ffffee4 FG |
146 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
147 | pub const fn is_empty(&self) -> bool { | |
0531ce1d XL |
148 | !(self.start < self.end) |
149 | } | |
041b39d2 XL |
150 | } |
151 | ||
3b2f2976 | 152 | /// A range only bounded inclusively below (`start..`). |
041b39d2 | 153 | /// |
3b2f2976 | 154 | /// The `RangeFrom` `start..` contains all values with `x >= start`. |
041b39d2 | 155 | /// |
f9f354fc XL |
156 | /// *Note*: Overflow in the [`Iterator`] implementation (when the contained |
157 | /// data type reaches its numerical limit) is allowed to panic, wrap, or | |
158 | /// saturate. This behavior is defined by the implementation of the [`Step`] | |
159 | /// trait. For primitive integers, this follows the normal rules, and respects | |
160 | /// the overflow checks profile (panic in debug, wrap in release). Note also | |
161 | /// that overflow happens earlier than you might assume: the overflow happens | |
162 | /// in the call to `next` that yields the maximum value, as the range must be | |
163 | /// set to a state to yield the next value. | |
164 | /// | |
165 | /// [`Step`]: crate::iter::Step | |
041b39d2 XL |
166 | /// |
167 | /// # Examples | |
168 | /// | |
29967ef6 XL |
169 | /// The `start..` syntax is a `RangeFrom`: |
170 | /// | |
041b39d2 | 171 | /// ``` |
3b2f2976 XL |
172 | /// assert_eq!((2..), std::ops::RangeFrom { start: 2 }); |
173 | /// assert_eq!(2 + 3 + 4, (2..).take(3).sum()); | |
29967ef6 | 174 | /// ``` |
3b2f2976 | 175 | /// |
29967ef6 | 176 | /// ``` |
532ac7d7 | 177 | /// let arr = [0, 1, 2, 3, 4]; |
29967ef6 XL |
178 | /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); |
179 | /// assert_eq!(arr[ .. 3], [0, 1, 2 ]); | |
180 | /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); | |
181 | /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); // This is a `RangeFrom` | |
182 | /// assert_eq!(arr[1.. 3], [ 1, 2 ]); | |
183 | /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); | |
041b39d2 | 184 | /// ``` |
1b1a35ee | 185 | #[lang = "RangeFrom"] |
83c7162d | 186 | #[doc(alias = "..")] |
532ac7d7 | 187 | #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186 |
041b39d2 XL |
188 | #[stable(feature = "rust1", since = "1.0.0")] |
189 | pub struct RangeFrom<Idx> { | |
190 | /// The lower bound of the range (inclusive). | |
191 | #[stable(feature = "rust1", since = "1.0.0")] | |
192 | pub start: Idx, | |
193 | } | |
194 | ||
195 | #[stable(feature = "rust1", since = "1.0.0")] | |
196 | impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> { | |
48663c56 | 197 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
532ac7d7 XL |
198 | self.start.fmt(fmt)?; |
199 | write!(fmt, "..")?; | |
200 | Ok(()) | |
041b39d2 XL |
201 | } |
202 | } | |
203 | ||
9ffffee4 | 204 | impl<Idx: ~const PartialOrd<Idx>> RangeFrom<Idx> { |
3b2f2976 XL |
205 | /// Returns `true` if `item` is contained in the range. |
206 | /// | |
041b39d2 XL |
207 | /// # Examples |
208 | /// | |
209 | /// ``` | |
83c7162d XL |
210 | /// assert!(!(3..).contains(&2)); |
211 | /// assert!( (3..).contains(&3)); | |
212 | /// assert!( (3..).contains(&1_000_000_000)); | |
213 | /// | |
214 | /// assert!( (0.0..).contains(&0.5)); | |
215 | /// assert!(!(0.0..).contains(&f32::NAN)); | |
216 | /// assert!(!(f32::NAN..).contains(&0.5)); | |
041b39d2 | 217 | /// ``` |
532ac7d7 | 218 | #[stable(feature = "range_contains", since = "1.35.0")] |
9ffffee4 FG |
219 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
220 | pub const fn contains<U>(&self, item: &U) -> bool | |
83c7162d | 221 | where |
9ffffee4 FG |
222 | Idx: ~const PartialOrd<U>, |
223 | U: ?Sized + ~const PartialOrd<Idx>, | |
83c7162d XL |
224 | { |
225 | <Self as RangeBounds<Idx>>::contains(self, item) | |
041b39d2 XL |
226 | } |
227 | } | |
228 | ||
3b2f2976 | 229 | /// A range only bounded exclusively above (`..end`). |
041b39d2 | 230 | /// |
3b2f2976 XL |
231 | /// The `RangeTo` `..end` contains all values with `x < end`. |
232 | /// It cannot serve as an [`Iterator`] because it doesn't have a starting point. | |
041b39d2 XL |
233 | /// |
234 | /// # Examples | |
235 | /// | |
3b2f2976 | 236 | /// The `..end` syntax is a `RangeTo`: |
041b39d2 XL |
237 | /// |
238 | /// ``` | |
3b2f2976 | 239 | /// assert_eq!((..5), std::ops::RangeTo { end: 5 }); |
041b39d2 XL |
240 | /// ``` |
241 | /// | |
3b2f2976 XL |
242 | /// It does not have an [`IntoIterator`] implementation, so you can't use it in |
243 | /// a `for` loop directly. This won't compile: | |
041b39d2 XL |
244 | /// |
245 | /// ```compile_fail,E0277 | |
3b2f2976 XL |
246 | /// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>: |
247 | /// // std::iter::Iterator` is not satisfied | |
041b39d2 XL |
248 | /// for i in ..5 { |
249 | /// // ... | |
250 | /// } | |
251 | /// ``` | |
252 | /// | |
3b2f2976 | 253 | /// When used as a [slicing index], `RangeTo` produces a slice of all array |
041b39d2 XL |
254 | /// elements before the index indicated by `end`. |
255 | /// | |
256 | /// ``` | |
532ac7d7 | 257 | /// let arr = [0, 1, 2, 3, 4]; |
29967ef6 XL |
258 | /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); |
259 | /// assert_eq!(arr[ .. 3], [0, 1, 2 ]); // This is a `RangeTo` | |
260 | /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); | |
261 | /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); | |
262 | /// assert_eq!(arr[1.. 3], [ 1, 2 ]); | |
263 | /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); | |
041b39d2 | 264 | /// ``` |
3b2f2976 | 265 | /// |
3dfed10e | 266 | /// [slicing index]: crate::slice::SliceIndex |
1b1a35ee | 267 | #[lang = "RangeTo"] |
83c7162d | 268 | #[doc(alias = "..")] |
041b39d2 XL |
269 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] |
270 | #[stable(feature = "rust1", since = "1.0.0")] | |
271 | pub struct RangeTo<Idx> { | |
272 | /// The upper bound of the range (exclusive). | |
273 | #[stable(feature = "rust1", since = "1.0.0")] | |
274 | pub end: Idx, | |
275 | } | |
276 | ||
277 | #[stable(feature = "rust1", since = "1.0.0")] | |
278 | impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> { | |
48663c56 | 279 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
532ac7d7 XL |
280 | write!(fmt, "..")?; |
281 | self.end.fmt(fmt)?; | |
282 | Ok(()) | |
041b39d2 XL |
283 | } |
284 | } | |
285 | ||
9ffffee4 | 286 | impl<Idx: ~const PartialOrd<Idx>> RangeTo<Idx> { |
3b2f2976 XL |
287 | /// Returns `true` if `item` is contained in the range. |
288 | /// | |
041b39d2 XL |
289 | /// # Examples |
290 | /// | |
291 | /// ``` | |
83c7162d XL |
292 | /// assert!( (..5).contains(&-1_000_000_000)); |
293 | /// assert!( (..5).contains(&4)); | |
294 | /// assert!(!(..5).contains(&5)); | |
295 | /// | |
296 | /// assert!( (..1.0).contains(&0.5)); | |
297 | /// assert!(!(..1.0).contains(&f32::NAN)); | |
298 | /// assert!(!(..f32::NAN).contains(&0.5)); | |
041b39d2 | 299 | /// ``` |
532ac7d7 | 300 | #[stable(feature = "range_contains", since = "1.35.0")] |
9ffffee4 FG |
301 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
302 | pub const fn contains<U>(&self, item: &U) -> bool | |
83c7162d | 303 | where |
9ffffee4 FG |
304 | Idx: ~const PartialOrd<U>, |
305 | U: ?Sized + ~const PartialOrd<Idx>, | |
83c7162d XL |
306 | { |
307 | <Self as RangeBounds<Idx>>::contains(self, item) | |
041b39d2 XL |
308 | } |
309 | } | |
310 | ||
0bf4aa26 | 311 | /// A range bounded inclusively below and above (`start..=end`). |
041b39d2 | 312 | /// |
ea8adc8c | 313 | /// The `RangeInclusive` `start..=end` contains all values with `x >= start` |
9fa01778 | 314 | /// and `x <= end`. It is empty unless `start <= end`. |
0531ce1d XL |
315 | /// |
316 | /// This iterator is [fused], but the specific values of `start` and `end` after | |
317 | /// iteration has finished are **unspecified** other than that [`.is_empty()`] | |
318 | /// will return `true` once no more values will be produced. | |
319 | /// | |
3dfed10e XL |
320 | /// [fused]: crate::iter::FusedIterator |
321 | /// [`.is_empty()`]: RangeInclusive::is_empty | |
041b39d2 XL |
322 | /// |
323 | /// # Examples | |
324 | /// | |
29967ef6 XL |
325 | /// The `start..=end` syntax is a `RangeInclusive`: |
326 | /// | |
041b39d2 | 327 | /// ``` |
83c7162d | 328 | /// assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5)); |
ea8adc8c | 329 | /// assert_eq!(3 + 4 + 5, (3..=5).sum()); |
29967ef6 | 330 | /// ``` |
3b2f2976 | 331 | /// |
29967ef6 | 332 | /// ``` |
532ac7d7 | 333 | /// let arr = [0, 1, 2, 3, 4]; |
29967ef6 XL |
334 | /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); |
335 | /// assert_eq!(arr[ .. 3], [0, 1, 2 ]); | |
336 | /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); | |
337 | /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); | |
338 | /// assert_eq!(arr[1.. 3], [ 1, 2 ]); | |
339 | /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); // This is a `RangeInclusive` | |
041b39d2 | 340 | /// ``` |
1b1a35ee | 341 | #[lang = "RangeInclusive"] |
83c7162d | 342 | #[doc(alias = "..=")] |
ba9703b0 | 343 | #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186 |
0531ce1d | 344 | #[stable(feature = "inclusive_range", since = "1.26.0")] |
041b39d2 | 345 | pub struct RangeInclusive<Idx> { |
dfeec247 XL |
346 | // Note that the fields here are not public to allow changing the |
347 | // representation in the future; in particular, while we could plausibly | |
348 | // expose start/end, modifying them without changing (future/current) | |
349 | // private fields may lead to incorrect behavior, so we don't want to | |
350 | // support that mode. | |
83c7162d | 351 | pub(crate) start: Idx, |
83c7162d | 352 | pub(crate) end: Idx, |
9fa01778 | 353 | |
74b04a01 XL |
354 | // This field is: |
355 | // - `false` upon construction | |
356 | // - `false` when iteration has yielded an element and the iterator is not exhausted | |
357 | // - `true` when iteration has been used to exhaust the iterator | |
358 | // | |
359 | // This is required to support PartialEq and Hash without a PartialOrd bound or specialization. | |
360 | pub(crate) exhausted: bool, | |
8faf50e0 XL |
361 | } |
362 | ||
83c7162d XL |
363 | impl<Idx> RangeInclusive<Idx> { |
364 | /// Creates a new inclusive range. Equivalent to writing `start..=end`. | |
365 | /// | |
366 | /// # Examples | |
367 | /// | |
368 | /// ``` | |
369 | /// use std::ops::RangeInclusive; | |
370 | /// | |
371 | /// assert_eq!(3..=5, RangeInclusive::new(3, 5)); | |
372 | /// ``` | |
1b1a35ee | 373 | #[lang = "range_inclusive_new"] |
83c7162d XL |
374 | #[stable(feature = "inclusive_range_methods", since = "1.27.0")] |
375 | #[inline] | |
a1dfa0c6 | 376 | #[rustc_promotable] |
dfeec247 | 377 | #[rustc_const_stable(feature = "const_range_new", since = "1.32.0")] |
83c7162d | 378 | pub const fn new(start: Idx, end: Idx) -> Self { |
74b04a01 | 379 | Self { start, end, exhausted: false } |
83c7162d XL |
380 | } |
381 | ||
382 | /// Returns the lower bound of the range (inclusive). | |
383 | /// | |
384 | /// When using an inclusive range for iteration, the values of `start()` and | |
385 | /// [`end()`] are unspecified after the iteration ended. To determine | |
386 | /// whether the inclusive range is empty, use the [`is_empty()`] method | |
387 | /// instead of comparing `start() > end()`. | |
388 | /// | |
389 | /// Note: the value returned by this method is unspecified after the range | |
390 | /// has been iterated to exhaustion. | |
391 | /// | |
3dfed10e XL |
392 | /// [`end()`]: RangeInclusive::end |
393 | /// [`is_empty()`]: RangeInclusive::is_empty | |
83c7162d XL |
394 | /// |
395 | /// # Examples | |
396 | /// | |
397 | /// ``` | |
398 | /// assert_eq!((3..=5).start(), &3); | |
399 | /// ``` | |
400 | #[stable(feature = "inclusive_range_methods", since = "1.27.0")] | |
dfeec247 | 401 | #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")] |
83c7162d | 402 | #[inline] |
a1dfa0c6 | 403 | pub const fn start(&self) -> &Idx { |
83c7162d XL |
404 | &self.start |
405 | } | |
406 | ||
407 | /// Returns the upper bound of the range (inclusive). | |
408 | /// | |
409 | /// When using an inclusive range for iteration, the values of [`start()`] | |
410 | /// and `end()` are unspecified after the iteration ended. To determine | |
411 | /// whether the inclusive range is empty, use the [`is_empty()`] method | |
412 | /// instead of comparing `start() > end()`. | |
413 | /// | |
414 | /// Note: the value returned by this method is unspecified after the range | |
415 | /// has been iterated to exhaustion. | |
416 | /// | |
3dfed10e XL |
417 | /// [`start()`]: RangeInclusive::start |
418 | /// [`is_empty()`]: RangeInclusive::is_empty | |
83c7162d XL |
419 | /// |
420 | /// # Examples | |
421 | /// | |
422 | /// ``` | |
423 | /// assert_eq!((3..=5).end(), &5); | |
424 | /// ``` | |
425 | #[stable(feature = "inclusive_range_methods", since = "1.27.0")] | |
dfeec247 | 426 | #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")] |
83c7162d | 427 | #[inline] |
a1dfa0c6 | 428 | pub const fn end(&self) -> &Idx { |
83c7162d XL |
429 | &self.end |
430 | } | |
431 | ||
432 | /// Destructures the `RangeInclusive` into (lower bound, upper (inclusive) bound). | |
433 | /// | |
434 | /// Note: the value returned by this method is unspecified after the range | |
435 | /// has been iterated to exhaustion. | |
436 | /// | |
437 | /// # Examples | |
438 | /// | |
439 | /// ``` | |
440 | /// assert_eq!((3..=5).into_inner(), (3, 5)); | |
441 | /// ``` | |
442 | #[stable(feature = "inclusive_range_methods", since = "1.27.0")] | |
443 | #[inline] | |
9ffffee4 FG |
444 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
445 | pub const fn into_inner(self) -> (Idx, Idx) { | |
83c7162d XL |
446 | (self.start, self.end) |
447 | } | |
448 | } | |
449 | ||
29967ef6 XL |
450 | impl RangeInclusive<usize> { |
451 | /// Converts to an exclusive `Range` for `SliceIndex` implementations. | |
452 | /// The caller is responsible for dealing with `end == usize::MAX`. | |
453 | #[inline] | |
5e7ed085 | 454 | pub(crate) const fn into_slice_range(self) -> Range<usize> { |
29967ef6 XL |
455 | // If we're not exhausted, we want to simply slice `start..end + 1`. |
456 | // If we are exhausted, then slicing with `end + 1..end + 1` gives us an | |
457 | // empty range that is still subject to bounds-checks for that endpoint. | |
458 | let exclusive_end = self.end + 1; | |
459 | let start = if self.exhausted { exclusive_end } else { self.start }; | |
460 | start..exclusive_end | |
461 | } | |
462 | } | |
463 | ||
0531ce1d | 464 | #[stable(feature = "inclusive_range", since = "1.26.0")] |
041b39d2 | 465 | impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> { |
48663c56 | 466 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
532ac7d7 XL |
467 | self.start.fmt(fmt)?; |
468 | write!(fmt, "..=")?; | |
469 | self.end.fmt(fmt)?; | |
74b04a01 XL |
470 | if self.exhausted { |
471 | write!(fmt, " (exhausted)")?; | |
472 | } | |
532ac7d7 | 473 | Ok(()) |
041b39d2 XL |
474 | } |
475 | } | |
476 | ||
9ffffee4 | 477 | impl<Idx: ~const PartialOrd<Idx>> RangeInclusive<Idx> { |
3b2f2976 XL |
478 | /// Returns `true` if `item` is contained in the range. |
479 | /// | |
041b39d2 XL |
480 | /// # Examples |
481 | /// | |
482 | /// ``` | |
83c7162d XL |
483 | /// assert!(!(3..=5).contains(&2)); |
484 | /// assert!( (3..=5).contains(&3)); | |
485 | /// assert!( (3..=5).contains(&4)); | |
486 | /// assert!( (3..=5).contains(&5)); | |
487 | /// assert!(!(3..=5).contains(&6)); | |
488 | /// | |
489 | /// assert!( (3..=3).contains(&3)); | |
490 | /// assert!(!(3..=2).contains(&3)); | |
3b2f2976 | 491 | /// |
83c7162d XL |
492 | /// assert!( (0.0..=1.0).contains(&1.0)); |
493 | /// assert!(!(0.0..=1.0).contains(&f32::NAN)); | |
494 | /// assert!(!(0.0..=f32::NAN).contains(&0.0)); | |
495 | /// assert!(!(f32::NAN..=1.0).contains(&1.0)); | |
041b39d2 | 496 | /// ``` |
29967ef6 XL |
497 | /// |
498 | /// This method always returns `false` after iteration has finished: | |
499 | /// | |
500 | /// ``` | |
501 | /// let mut r = 3..=5; | |
502 | /// assert!(r.contains(&3) && r.contains(&5)); | |
503 | /// for _ in r.by_ref() {} | |
504 | /// // Precise field values are unspecified here | |
505 | /// assert!(!r.contains(&3) && !r.contains(&5)); | |
506 | /// ``` | |
532ac7d7 | 507 | #[stable(feature = "range_contains", since = "1.35.0")] |
9ffffee4 FG |
508 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
509 | pub const fn contains<U>(&self, item: &U) -> bool | |
83c7162d | 510 | where |
9ffffee4 FG |
511 | Idx: ~const PartialOrd<U>, |
512 | U: ?Sized + ~const PartialOrd<Idx>, | |
83c7162d XL |
513 | { |
514 | <Self as RangeBounds<Idx>>::contains(self, item) | |
041b39d2 | 515 | } |
0531ce1d XL |
516 | |
517 | /// Returns `true` if the range contains no items. | |
518 | /// | |
519 | /// # Examples | |
520 | /// | |
521 | /// ``` | |
0531ce1d XL |
522 | /// assert!(!(3..=5).is_empty()); |
523 | /// assert!(!(3..=3).is_empty()); | |
524 | /// assert!( (3..=2).is_empty()); | |
525 | /// ``` | |
526 | /// | |
527 | /// The range is empty if either side is incomparable: | |
528 | /// | |
529 | /// ``` | |
0531ce1d | 530 | /// assert!(!(3.0..=5.0).is_empty()); |
ba9703b0 XL |
531 | /// assert!( (3.0..=f32::NAN).is_empty()); |
532 | /// assert!( (f32::NAN..=5.0).is_empty()); | |
0531ce1d XL |
533 | /// ``` |
534 | /// | |
535 | /// This method returns `true` after iteration has finished: | |
536 | /// | |
537 | /// ``` | |
0531ce1d XL |
538 | /// let mut r = 3..=5; |
539 | /// for _ in r.by_ref() {} | |
540 | /// // Precise field values are unspecified here | |
541 | /// assert!(r.is_empty()); | |
542 | /// ``` | |
3dfed10e | 543 | #[stable(feature = "range_is_empty", since = "1.47.0")] |
9ffffee4 | 544 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
8faf50e0 | 545 | #[inline] |
9ffffee4 | 546 | pub const fn is_empty(&self) -> bool { |
74b04a01 | 547 | self.exhausted || !(self.start <= self.end) |
0531ce1d | 548 | } |
041b39d2 XL |
549 | } |
550 | ||
ea8adc8c | 551 | /// A range only bounded inclusively above (`..=end`). |
041b39d2 | 552 | /// |
ea8adc8c | 553 | /// The `RangeToInclusive` `..=end` contains all values with `x <= end`. |
3b2f2976 | 554 | /// It cannot serve as an [`Iterator`] because it doesn't have a starting point. |
041b39d2 XL |
555 | /// |
556 | /// # Examples | |
557 | /// | |
ea8adc8c | 558 | /// The `..=end` syntax is a `RangeToInclusive`: |
041b39d2 XL |
559 | /// |
560 | /// ``` | |
ea8adc8c | 561 | /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 }); |
041b39d2 XL |
562 | /// ``` |
563 | /// | |
3b2f2976 | 564 | /// It does not have an [`IntoIterator`] implementation, so you can't use it in a |
041b39d2 XL |
565 | /// `for` loop directly. This won't compile: |
566 | /// | |
567 | /// ```compile_fail,E0277 | |
3b2f2976 XL |
568 | /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>: |
569 | /// // std::iter::Iterator` is not satisfied | |
ea8adc8c | 570 | /// for i in ..=5 { |
041b39d2 XL |
571 | /// // ... |
572 | /// } | |
573 | /// ``` | |
574 | /// | |
3b2f2976 | 575 | /// When used as a [slicing index], `RangeToInclusive` produces a slice of all |
041b39d2 XL |
576 | /// array elements up to and including the index indicated by `end`. |
577 | /// | |
578 | /// ``` | |
532ac7d7 | 579 | /// let arr = [0, 1, 2, 3, 4]; |
29967ef6 XL |
580 | /// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); |
581 | /// assert_eq!(arr[ .. 3], [0, 1, 2 ]); | |
582 | /// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); // This is a `RangeToInclusive` | |
583 | /// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); | |
584 | /// assert_eq!(arr[1.. 3], [ 1, 2 ]); | |
585 | /// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); | |
041b39d2 | 586 | /// ``` |
3b2f2976 | 587 | /// |
3dfed10e | 588 | /// [slicing index]: crate::slice::SliceIndex |
1b1a35ee | 589 | #[lang = "RangeToInclusive"] |
83c7162d | 590 | #[doc(alias = "..=")] |
041b39d2 | 591 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] |
0531ce1d | 592 | #[stable(feature = "inclusive_range", since = "1.26.0")] |
041b39d2 XL |
593 | pub struct RangeToInclusive<Idx> { |
594 | /// The upper bound of the range (inclusive) | |
0531ce1d | 595 | #[stable(feature = "inclusive_range", since = "1.26.0")] |
041b39d2 XL |
596 | pub end: Idx, |
597 | } | |
598 | ||
0531ce1d | 599 | #[stable(feature = "inclusive_range", since = "1.26.0")] |
041b39d2 | 600 | impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> { |
48663c56 | 601 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
532ac7d7 XL |
602 | write!(fmt, "..=")?; |
603 | self.end.fmt(fmt)?; | |
604 | Ok(()) | |
041b39d2 XL |
605 | } |
606 | } | |
607 | ||
9ffffee4 | 608 | impl<Idx: ~const PartialOrd<Idx>> RangeToInclusive<Idx> { |
3b2f2976 XL |
609 | /// Returns `true` if `item` is contained in the range. |
610 | /// | |
041b39d2 XL |
611 | /// # Examples |
612 | /// | |
613 | /// ``` | |
83c7162d XL |
614 | /// assert!( (..=5).contains(&-1_000_000_000)); |
615 | /// assert!( (..=5).contains(&5)); | |
616 | /// assert!(!(..=5).contains(&6)); | |
617 | /// | |
618 | /// assert!( (..=1.0).contains(&1.0)); | |
619 | /// assert!(!(..=1.0).contains(&f32::NAN)); | |
620 | /// assert!(!(..=f32::NAN).contains(&0.5)); | |
041b39d2 | 621 | /// ``` |
532ac7d7 | 622 | #[stable(feature = "range_contains", since = "1.35.0")] |
9ffffee4 FG |
623 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
624 | pub const fn contains<U>(&self, item: &U) -> bool | |
83c7162d | 625 | where |
9ffffee4 FG |
626 | Idx: ~const PartialOrd<U>, |
627 | U: ?Sized + ~const PartialOrd<Idx>, | |
83c7162d XL |
628 | { |
629 | <Self as RangeBounds<Idx>>::contains(self, item) | |
041b39d2 XL |
630 | } |
631 | } | |
632 | ||
633 | // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>> | |
634 | // because underflow would be possible with (..0).into() | |
0531ce1d XL |
635 | |
636 | /// An endpoint of a range of keys. | |
637 | /// | |
638 | /// # Examples | |
639 | /// | |
640 | /// `Bound`s are range endpoints: | |
641 | /// | |
642 | /// ``` | |
0531ce1d XL |
643 | /// use std::ops::Bound::*; |
644 | /// use std::ops::RangeBounds; | |
645 | /// | |
94b46f34 XL |
646 | /// assert_eq!((..100).start_bound(), Unbounded); |
647 | /// assert_eq!((1..12).start_bound(), Included(&1)); | |
648 | /// assert_eq!((1..12).end_bound(), Excluded(&12)); | |
0531ce1d XL |
649 | /// ``` |
650 | /// | |
651 | /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`]. | |
652 | /// Note that in most cases, it's better to use range syntax (`1..5`) instead. | |
653 | /// | |
654 | /// ``` | |
655 | /// use std::collections::BTreeMap; | |
656 | /// use std::ops::Bound::{Excluded, Included, Unbounded}; | |
657 | /// | |
658 | /// let mut map = BTreeMap::new(); | |
659 | /// map.insert(3, "a"); | |
660 | /// map.insert(5, "b"); | |
661 | /// map.insert(8, "c"); | |
662 | /// | |
663 | /// for (key, value) in map.range((Excluded(3), Included(8))) { | |
5e7ed085 | 664 | /// println!("{key}: {value}"); |
0531ce1d XL |
665 | /// } |
666 | /// | |
667 | /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next()); | |
668 | /// ``` | |
669 | /// | |
670 | /// [`BTreeMap::range`]: ../../std/collections/btree_map/struct.BTreeMap.html#method.range | |
671 | #[stable(feature = "collections_bound", since = "1.17.0")] | |
672 | #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] | |
673 | pub enum Bound<T> { | |
674 | /// An inclusive bound. | |
675 | #[stable(feature = "collections_bound", since = "1.17.0")] | |
676 | Included(#[stable(feature = "collections_bound", since = "1.17.0")] T), | |
677 | /// An exclusive bound. | |
678 | #[stable(feature = "collections_bound", since = "1.17.0")] | |
679 | Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T), | |
680 | /// An infinite endpoint. Indicates that there is no bound in this direction. | |
681 | #[stable(feature = "collections_bound", since = "1.17.0")] | |
682 | Unbounded, | |
683 | } | |
684 | ||
5869c6ff XL |
685 | impl<T> Bound<T> { |
686 | /// Converts from `&Bound<T>` to `Bound<&T>`. | |
687 | #[inline] | |
f2b60f7d | 688 | #[stable(feature = "bound_as_ref_shared", since = "1.65.0")] |
5869c6ff XL |
689 | pub fn as_ref(&self) -> Bound<&T> { |
690 | match *self { | |
691 | Included(ref x) => Included(x), | |
692 | Excluded(ref x) => Excluded(x), | |
693 | Unbounded => Unbounded, | |
694 | } | |
695 | } | |
696 | ||
17df50a5 | 697 | /// Converts from `&mut Bound<T>` to `Bound<&mut T>`. |
5869c6ff | 698 | #[inline] |
17df50a5 | 699 | #[unstable(feature = "bound_as_ref", issue = "80996")] |
5869c6ff XL |
700 | pub fn as_mut(&mut self) -> Bound<&mut T> { |
701 | match *self { | |
702 | Included(ref mut x) => Included(x), | |
703 | Excluded(ref mut x) => Excluded(x), | |
704 | Unbounded => Unbounded, | |
705 | } | |
706 | } | |
17df50a5 XL |
707 | |
708 | /// Maps a `Bound<T>` to a `Bound<U>` by applying a function to the contained value (including | |
709 | /// both `Included` and `Excluded`), returning a `Bound` of the same kind. | |
710 | /// | |
711 | /// # Examples | |
712 | /// | |
713 | /// ``` | |
714 | /// #![feature(bound_map)] | |
715 | /// use std::ops::Bound::*; | |
716 | /// | |
717 | /// let bound_string = Included("Hello, World!"); | |
718 | /// | |
719 | /// assert_eq!(bound_string.map(|s| s.len()), Included(13)); | |
720 | /// ``` | |
721 | /// | |
722 | /// ``` | |
723 | /// #![feature(bound_map)] | |
724 | /// use std::ops::Bound; | |
725 | /// use Bound::*; | |
726 | /// | |
727 | /// let unbounded_string: Bound<String> = Unbounded; | |
728 | /// | |
729 | /// assert_eq!(unbounded_string.map(|s| s.len()), Unbounded); | |
730 | /// ``` | |
731 | #[inline] | |
732 | #[unstable(feature = "bound_map", issue = "86026")] | |
733 | pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Bound<U> { | |
734 | match self { | |
735 | Unbounded => Unbounded, | |
736 | Included(x) => Included(f(x)), | |
737 | Excluded(x) => Excluded(f(x)), | |
738 | } | |
739 | } | |
5869c6ff XL |
740 | } |
741 | ||
dc9dc135 XL |
742 | impl<T: Clone> Bound<&T> { |
743 | /// Map a `Bound<&T>` to a `Bound<T>` by cloning the contents of the bound. | |
744 | /// | |
745 | /// # Examples | |
746 | /// | |
747 | /// ``` | |
dc9dc135 XL |
748 | /// use std::ops::Bound::*; |
749 | /// use std::ops::RangeBounds; | |
750 | /// | |
751 | /// assert_eq!((1..12).start_bound(), Included(&1)); | |
752 | /// assert_eq!((1..12).start_bound().cloned(), Included(1)); | |
753 | /// ``` | |
3c0e092e | 754 | #[must_use = "`self` will be dropped if the result is not used"] |
136023e0 | 755 | #[stable(feature = "bound_cloned", since = "1.55.0")] |
dc9dc135 XL |
756 | pub fn cloned(self) -> Bound<T> { |
757 | match self { | |
758 | Bound::Unbounded => Bound::Unbounded, | |
759 | Bound::Included(x) => Bound::Included(x.clone()), | |
760 | Bound::Excluded(x) => Bound::Excluded(x.clone()), | |
761 | } | |
762 | } | |
763 | } | |
764 | ||
0531ce1d | 765 | /// `RangeBounds` is implemented by Rust's built-in range types, produced |
532ac7d7 | 766 | /// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`. |
29967ef6 | 767 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 | 768 | #[const_trait] |
0531ce1d XL |
769 | pub trait RangeBounds<T: ?Sized> { |
770 | /// Start index bound. | |
771 | /// | |
772 | /// Returns the start value as a `Bound`. | |
773 | /// | |
774 | /// # Examples | |
775 | /// | |
776 | /// ``` | |
0531ce1d XL |
777 | /// # fn main() { |
778 | /// use std::ops::Bound::*; | |
779 | /// use std::ops::RangeBounds; | |
780 | /// | |
94b46f34 XL |
781 | /// assert_eq!((..10).start_bound(), Unbounded); |
782 | /// assert_eq!((3..10).start_bound(), Included(&3)); | |
0531ce1d XL |
783 | /// # } |
784 | /// ``` | |
94b46f34 XL |
785 | #[stable(feature = "collections_range", since = "1.28.0")] |
786 | fn start_bound(&self) -> Bound<&T>; | |
0531ce1d XL |
787 | |
788 | /// End index bound. | |
789 | /// | |
790 | /// Returns the end value as a `Bound`. | |
791 | /// | |
792 | /// # Examples | |
793 | /// | |
794 | /// ``` | |
0531ce1d XL |
795 | /// # fn main() { |
796 | /// use std::ops::Bound::*; | |
797 | /// use std::ops::RangeBounds; | |
798 | /// | |
94b46f34 XL |
799 | /// assert_eq!((3..).end_bound(), Unbounded); |
800 | /// assert_eq!((3..10).end_bound(), Excluded(&10)); | |
0531ce1d XL |
801 | /// # } |
802 | /// ``` | |
94b46f34 XL |
803 | #[stable(feature = "collections_range", since = "1.28.0")] |
804 | fn end_bound(&self) -> Bound<&T>; | |
83c7162d | 805 | |
83c7162d XL |
806 | /// Returns `true` if `item` is contained in the range. |
807 | /// | |
808 | /// # Examples | |
809 | /// | |
810 | /// ``` | |
83c7162d XL |
811 | /// assert!( (3..5).contains(&4)); |
812 | /// assert!(!(3..5).contains(&2)); | |
813 | /// | |
814 | /// assert!( (0.0..1.0).contains(&0.5)); | |
815 | /// assert!(!(0.0..1.0).contains(&f32::NAN)); | |
816 | /// assert!(!(0.0..f32::NAN).contains(&0.5)); | |
817 | /// assert!(!(f32::NAN..1.0).contains(&0.5)); | |
532ac7d7 | 818 | #[stable(feature = "range_contains", since = "1.35.0")] |
83c7162d XL |
819 | fn contains<U>(&self, item: &U) -> bool |
820 | where | |
9ffffee4 FG |
821 | T: ~const PartialOrd<U>, |
822 | U: ?Sized + ~const PartialOrd<T>, | |
83c7162d | 823 | { |
94b46f34 | 824 | (match self.start_bound() { |
94222f64 XL |
825 | Included(start) => start <= item, |
826 | Excluded(start) => start < item, | |
83c7162d | 827 | Unbounded => true, |
532ac7d7 | 828 | }) && (match self.end_bound() { |
94222f64 XL |
829 | Included(end) => item <= end, |
830 | Excluded(end) => item < end, | |
83c7162d XL |
831 | Unbounded => true, |
832 | }) | |
833 | } | |
0531ce1d XL |
834 | } |
835 | ||
836 | use self::Bound::{Excluded, Included, Unbounded}; | |
837 | ||
94b46f34 | 838 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
839 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
840 | impl<T: ?Sized> const RangeBounds<T> for RangeFull { | |
94b46f34 | 841 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
842 | Unbounded |
843 | } | |
94b46f34 | 844 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
845 | Unbounded |
846 | } | |
847 | } | |
848 | ||
94b46f34 | 849 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
850 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
851 | impl<T> const RangeBounds<T> for RangeFrom<T> { | |
94b46f34 | 852 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
853 | Included(&self.start) |
854 | } | |
94b46f34 | 855 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
856 | Unbounded |
857 | } | |
858 | } | |
859 | ||
94b46f34 | 860 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
861 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
862 | impl<T> const RangeBounds<T> for RangeTo<T> { | |
94b46f34 | 863 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
864 | Unbounded |
865 | } | |
94b46f34 | 866 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
867 | Excluded(&self.end) |
868 | } | |
869 | } | |
870 | ||
94b46f34 | 871 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
872 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
873 | impl<T> const RangeBounds<T> for Range<T> { | |
94b46f34 | 874 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
875 | Included(&self.start) |
876 | } | |
94b46f34 | 877 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
878 | Excluded(&self.end) |
879 | } | |
880 | } | |
881 | ||
94b46f34 | 882 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
883 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
884 | impl<T> const RangeBounds<T> for RangeInclusive<T> { | |
94b46f34 | 885 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
886 | Included(&self.start) |
887 | } | |
94b46f34 | 888 | fn end_bound(&self) -> Bound<&T> { |
29967ef6 XL |
889 | if self.exhausted { |
890 | // When the iterator is exhausted, we usually have start == end, | |
891 | // but we want the range to appear empty, containing nothing. | |
892 | Excluded(&self.end) | |
893 | } else { | |
894 | Included(&self.end) | |
895 | } | |
0531ce1d XL |
896 | } |
897 | } | |
898 | ||
94b46f34 | 899 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
900 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
901 | impl<T> const RangeBounds<T> for RangeToInclusive<T> { | |
94b46f34 | 902 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
903 | Unbounded |
904 | } | |
94b46f34 | 905 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
906 | Included(&self.end) |
907 | } | |
908 | } | |
909 | ||
94b46f34 | 910 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
911 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
912 | impl<T> const RangeBounds<T> for (Bound<T>, Bound<T>) { | |
94b46f34 | 913 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
914 | match *self { |
915 | (Included(ref start), _) => Included(start), | |
916 | (Excluded(ref start), _) => Excluded(start), | |
532ac7d7 | 917 | (Unbounded, _) => Unbounded, |
0531ce1d XL |
918 | } |
919 | } | |
920 | ||
94b46f34 | 921 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
922 | match *self { |
923 | (_, Included(ref end)) => Included(end), | |
924 | (_, Excluded(ref end)) => Excluded(end), | |
532ac7d7 | 925 | (_, Unbounded) => Unbounded, |
0531ce1d XL |
926 | } |
927 | } | |
928 | } | |
929 | ||
94b46f34 | 930 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
931 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
932 | impl<'a, T: ?Sized + 'a> const RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) { | |
94b46f34 | 933 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
934 | self.0 |
935 | } | |
936 | ||
94b46f34 | 937 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
938 | self.1 |
939 | } | |
940 | } | |
941 | ||
94b46f34 | 942 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
943 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
944 | impl<T> const RangeBounds<T> for RangeFrom<&T> { | |
94b46f34 | 945 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
946 | Included(self.start) |
947 | } | |
94b46f34 | 948 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
949 | Unbounded |
950 | } | |
951 | } | |
952 | ||
94b46f34 | 953 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
954 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
955 | impl<T> const RangeBounds<T> for RangeTo<&T> { | |
94b46f34 | 956 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
957 | Unbounded |
958 | } | |
94b46f34 | 959 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
960 | Excluded(self.end) |
961 | } | |
962 | } | |
963 | ||
94b46f34 | 964 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
965 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
966 | impl<T> const RangeBounds<T> for Range<&T> { | |
94b46f34 | 967 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
968 | Included(self.start) |
969 | } | |
94b46f34 | 970 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
971 | Excluded(self.end) |
972 | } | |
973 | } | |
974 | ||
94b46f34 | 975 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
976 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
977 | impl<T> const RangeBounds<T> for RangeInclusive<&T> { | |
94b46f34 | 978 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
979 | Included(self.start) |
980 | } | |
94b46f34 | 981 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
982 | Included(self.end) |
983 | } | |
984 | } | |
985 | ||
94b46f34 | 986 | #[stable(feature = "collections_range", since = "1.28.0")] |
9ffffee4 FG |
987 | #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")] |
988 | impl<T> const RangeBounds<T> for RangeToInclusive<&T> { | |
94b46f34 | 989 | fn start_bound(&self) -> Bound<&T> { |
0531ce1d XL |
990 | Unbounded |
991 | } | |
94b46f34 | 992 | fn end_bound(&self) -> Bound<&T> { |
0531ce1d XL |
993 | Included(self.end) |
994 | } | |
995 | } | |
a2a8927a XL |
996 | |
997 | /// `OneSidedRange` is implemented for built-in range types that are unbounded | |
998 | /// on one side. For example, `a..`, `..b` and `..=c` implement `OneSidedRange`, | |
999 | /// but `..`, `d..e`, and `f..=g` do not. | |
1000 | /// | |
1001 | /// Types that implement `OneSidedRange<T>` must return `Bound::Unbounded` | |
1002 | /// from one of `RangeBounds::start_bound` or `RangeBounds::end_bound`. | |
1003 | #[unstable(feature = "one_sided_range", issue = "69780")] | |
1004 | pub trait OneSidedRange<T: ?Sized>: RangeBounds<T> {} | |
1005 | ||
1006 | #[unstable(feature = "one_sided_range", issue = "69780")] | |
1007 | impl<T> OneSidedRange<T> for RangeTo<T> where Self: RangeBounds<T> {} | |
1008 | ||
1009 | #[unstable(feature = "one_sided_range", issue = "69780")] | |
1010 | impl<T> OneSidedRange<T> for RangeFrom<T> where Self: RangeBounds<T> {} | |
1011 | ||
1012 | #[unstable(feature = "one_sided_range", issue = "69780")] | |
1013 | impl<T> OneSidedRange<T> for RangeToInclusive<T> where Self: RangeBounds<T> {} |