1use crate::iter::{
2 FusedIterator, Step, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, TrustedStep,
3};
4use crate::num::NonZero;
5use crate::range::{Range, RangeFrom, RangeInclusive, legacy};
6use crate::{intrinsics, mem};
7
8#[unstable(feature = "new_range_api", issue = "125687")]
10#[derive(Debug, Clone)]
11pub struct RangeIter<A>(legacy::Range<A>);
12
13impl<A> RangeIter<A> {
14 #[unstable(feature = "new_range_remainder", issue = "154458")]
15 pub fn remainder(self) -> Range<A> {
32 Range { start: self.0.start, end: self.0.end }
33 }
34}
35
36macro_rules! unsafe_range_trusted_random_access_impl {
39 ($($t:ty)*) => ($(
40 #[doc(hidden)]
41 #[unstable(feature = "trusted_random_access", issue = "none")]
42 unsafe impl TrustedRandomAccess for RangeIter<$t> {}
43
44 #[doc(hidden)]
45 #[unstable(feature = "trusted_random_access", issue = "none")]
46 unsafe impl TrustedRandomAccessNoCoerce for RangeIter<$t> {
47 const MAY_HAVE_SIDE_EFFECT: bool = false;
48 }
49 )*)
50}
51
52unsafe_range_trusted_random_access_impl! {
53 usize u8 u16
54 isize i8 i16
55}
56
57#[cfg(target_pointer_width = "32")]
58unsafe_range_trusted_random_access_impl! {
59 u32 i32
60}
61
62#[cfg(target_pointer_width = "64")]
63unsafe_range_trusted_random_access_impl! {
64 u32 i32
65 u64 i64
66}
67
68#[unstable(feature = "new_range_api", issue = "125687")]
69impl<A: Step> Iterator for RangeIter<A> {
70 type Item = A;
71
72 #[inline]
73 fn next(&mut self) -> Option<A> {
74 self.0.next()
75 }
76
77 #[inline]
78 fn size_hint(&self) -> (usize, Option<usize>) {
79 self.0.size_hint()
80 }
81
82 #[inline]
83 fn count(self) -> usize {
84 self.0.count()
85 }
86
87 #[inline]
88 fn nth(&mut self, n: usize) -> Option<A> {
89 self.0.nth(n)
90 }
91
92 #[inline]
93 fn last(self) -> Option<A> {
94 self.0.last()
95 }
96
97 #[inline]
98 fn min(self) -> Option<A>
99 where
100 A: Ord,
101 {
102 self.0.min()
103 }
104
105 #[inline]
106 fn max(self) -> Option<A>
107 where
108 A: Ord,
109 {
110 self.0.max()
111 }
112
113 #[inline]
114 fn is_sorted(self) -> bool {
115 true
116 }
117
118 #[inline]
119 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
120 self.0.advance_by(n)
121 }
122
123 #[inline]
124 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
125 where
126 Self: TrustedRandomAccessNoCoerce,
127 {
128 unsafe { Step::forward_unchecked(self.0.start.clone(), idx) }
133 }
134}
135
136#[unstable(feature = "new_range_api", issue = "125687")]
137impl<A: Step> DoubleEndedIterator for RangeIter<A> {
138 #[inline]
139 fn next_back(&mut self) -> Option<A> {
140 self.0.next_back()
141 }
142
143 #[inline]
144 fn nth_back(&mut self, n: usize) -> Option<A> {
145 self.0.nth_back(n)
146 }
147
148 #[inline]
149 fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
150 self.0.advance_back_by(n)
151 }
152}
153
154#[unstable(feature = "trusted_len", issue = "37572")]
155unsafe impl<A: TrustedStep> TrustedLen for RangeIter<A> {}
156
157#[unstable(feature = "new_range_api", issue = "125687")]
158impl<A: Step> FusedIterator for RangeIter<A> {}
159
160#[unstable(feature = "new_range_api", issue = "125687")]
161impl<A: Step> IntoIterator for Range<A> {
162 type Item = A;
163 type IntoIter = RangeIter<A>;
164
165 fn into_iter(self) -> Self::IntoIter {
166 RangeIter(self.into())
167 }
168}
169
170#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
172#[derive(Debug, Clone)]
173pub struct RangeInclusiveIter<A>(legacy::RangeInclusive<A>);
174
175impl<A: Step> RangeInclusiveIter<A> {
176 #[unstable(feature = "new_range_remainder", issue = "154458")]
194 pub fn remainder(self) -> Option<RangeInclusive<A>> {
195 if self.0.is_empty() {
196 return None;
197 }
198
199 Some(RangeInclusive { start: self.0.start, last: self.0.end })
200 }
201}
202
203#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
204impl<A: Step> Iterator for RangeInclusiveIter<A> {
205 type Item = A;
206
207 #[inline]
208 fn next(&mut self) -> Option<A> {
209 self.0.next()
210 }
211
212 #[inline]
213 fn size_hint(&self) -> (usize, Option<usize>) {
214 self.0.size_hint()
215 }
216
217 #[inline]
218 fn count(self) -> usize {
219 self.0.count()
220 }
221
222 #[inline]
223 fn nth(&mut self, n: usize) -> Option<A> {
224 self.0.nth(n)
225 }
226
227 #[inline]
228 fn last(self) -> Option<A> {
229 self.0.last()
230 }
231
232 #[inline]
233 fn min(self) -> Option<A>
234 where
235 A: Ord,
236 {
237 self.0.min()
238 }
239
240 #[inline]
241 fn max(self) -> Option<A>
242 where
243 A: Ord,
244 {
245 self.0.max()
246 }
247
248 #[inline]
249 fn is_sorted(self) -> bool {
250 true
251 }
252
253 #[inline]
254 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
255 self.0.advance_by(n)
256 }
257}
258
259#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
260impl<A: Step> DoubleEndedIterator for RangeInclusiveIter<A> {
261 #[inline]
262 fn next_back(&mut self) -> Option<A> {
263 self.0.next_back()
264 }
265
266 #[inline]
267 fn nth_back(&mut self, n: usize) -> Option<A> {
268 self.0.nth_back(n)
269 }
270
271 #[inline]
272 fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
273 self.0.advance_back_by(n)
274 }
275}
276
277#[unstable(feature = "trusted_len", issue = "37572")]
278unsafe impl<A: TrustedStep> TrustedLen for RangeInclusiveIter<A> {}
279
280#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
281impl<A: Step> FusedIterator for RangeInclusiveIter<A> {}
282
283#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
284impl<A: Step> IntoIterator for RangeInclusive<A> {
285 type Item = A;
286 type IntoIter = RangeInclusiveIter<A>;
287
288 fn into_iter(self) -> Self::IntoIter {
289 RangeInclusiveIter(self.into())
290 }
291}
292
293macro_rules! range_exact_iter_impl {
302 ($($t:ty)*) => ($(
303 #[unstable(feature = "new_range_api", issue = "125687")]
304 impl ExactSizeIterator for RangeIter<$t> { }
305 )*)
306}
307
308macro_rules! range_incl_exact_iter_impl {
309 ($($t:ty)*) => ($(
310 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
311 impl ExactSizeIterator for RangeInclusiveIter<$t> { }
312 )*)
313}
314
315range_exact_iter_impl! {
316 usize u8 u16
317 isize i8 i16
318}
319
320range_incl_exact_iter_impl! {
321 u8
322 i8
323}
324
325#[stable(feature = "new_range_from_api", since = "CURRENT_RUSTC_VERSION")]
327#[derive(Debug, Clone)]
328pub struct RangeFromIter<A> {
329 start: A,
330 exhausted: bool,
333}
334
335impl<A: Step> RangeFromIter<A> {
336 #[inline]
350 #[rustc_inherit_overflow_checks]
351 #[unstable(feature = "new_range_remainder", issue = "154458")]
352 pub fn remainder(self) -> RangeFrom<A> {
353 if self.exhausted {
358 return RangeFrom { start: Step::forward(self.start, 1) };
359 }
360
361 RangeFrom { start: self.start }
362 }
363}
364
365#[stable(feature = "new_range_from_api", since = "CURRENT_RUSTC_VERSION")]
366impl<A: Step> Iterator for RangeFromIter<A> {
367 type Item = A;
368
369 #[inline]
370 #[rustc_inherit_overflow_checks]
371 fn next(&mut self) -> Option<A> {
372 if self.exhausted {
373 self.start = Step::forward(self.start.clone(), 1);
376
377 self.exhausted = false;
388 }
389 if intrinsics::overflow_checks() {
390 let Some(n) = Step::forward_checked(self.start.clone(), 1) else {
391 self.exhausted = true;
392 return Some(self.start.clone());
393 };
394 return Some(mem::replace(&mut self.start, n));
395 }
396
397 let n = Step::forward(self.start.clone(), 1);
398 Some(mem::replace(&mut self.start, n))
399 }
400
401 #[inline]
402 fn size_hint(&self) -> (usize, Option<usize>) {
403 (usize::MAX, None)
404 }
405
406 #[inline]
407 #[rustc_inherit_overflow_checks]
408 fn nth(&mut self, n: usize) -> Option<A> {
409 if self.exhausted {
413 self.start = Step::forward(self.start.clone(), 1);
414 self.exhausted = false;
415 }
416 if intrinsics::overflow_checks() {
417 let plus_n = Step::forward(self.start.clone(), n);
418 if let Some(plus_n1) = Step::forward_checked(plus_n.clone(), 1) {
419 self.start = plus_n1;
420 } else {
421 self.start = plus_n.clone();
422 self.exhausted = true;
423 }
424 return Some(plus_n);
425 }
426
427 let plus_n = Step::forward(self.start.clone(), n);
428 self.start = Step::forward(plus_n.clone(), 1);
429 Some(plus_n)
430 }
431}
432
433#[unstable(feature = "trusted_len", issue = "37572")]
434unsafe impl<A: TrustedStep> TrustedLen for RangeFromIter<A> {}
435
436#[stable(feature = "new_range_from_api", since = "CURRENT_RUSTC_VERSION")]
437impl<A: Step> FusedIterator for RangeFromIter<A> {}
438
439#[stable(feature = "new_range_from_api", since = "CURRENT_RUSTC_VERSION")]
440impl<A: Step> IntoIterator for RangeFrom<A> {
441 type Item = A;
442 type IntoIter = RangeFromIter<A>;
443
444 fn into_iter(self) -> Self::IntoIter {
445 RangeFromIter { start: self.start, exhausted: false }
446 }
447}