infinite-iterator 0.1.0

A trait for iterators that never end
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! This crate provides a trait,
//! `InfiniteIterator`,
//! used to represent an iterator for which [`next`]
//! can never return `None`.
//!
//! It additionally provides a macro, `ifor!`,
//! which is identical a for loop
//! except it supports breaking with a value
//! when used on an infinite iterator.
//!
//! [`next`]: https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#tymethod.next
#![no_std]

#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "alloc")]
extern crate alloc;

use core::iter;

/// An [`Iterator`] that never ends.
///
/// # Invariants
///
/// For this trait to be correctly implemented,
/// the following invariants must be upheld:
/// 1. `Some(iter.next_infinite())` must always give the same result as `iter.next()`.
/// 2. No default-implemented iterator methods may be overriden
///     to have visibly different behaviour
///     than their default implementations.
/// 3. `size_hint().1` must always be `None`.
/// 4. The type must not implement [`ExactSizeIterator`].
///
/// If any of the above invariants are violated,
/// the behaviour of any code that uses the iterator is unspecified
/// (i.e. it may panic, abort or give wrong results) —
/// however, because `InfiniteIterator` is not an `unsafe trait`
/// it still must not invoke Undefined Behaviour.
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub trait InfiniteIterator: Iterator {
    /// Like [`Iterator::next`],
    /// but never returning [`None`] because the iterator never ends.
    fn next_infinite(&mut self) -> Self::Item;

    /// Like [`Iterator::for_each`],
    /// but it never returns because the iterator never ends.
    ///
    /// # Examples
    ///
    /// ```
    /// use infinite_iterator::InfiniteIterator;
    ///
    /// fn run() -> ! {
    ///     (0..).for_each_infinite(|num| {
    ///         println!("{num}");
    ///         std::thread::sleep(std::time::Duration::from_secs(5));
    ///     })
    /// }
    /// ```
    fn for_each_infinite<F: FnMut(Self::Item)>(mut self, mut f: F) -> !
    where
        Self: Sized,
    {
        loop {
            f(self.next_infinite());
        }
    }

    /// Like [`Iterator::find`],
    /// but it is guaranteed to find an item
    /// (or loop forever)
    /// because the iterator is infinite.
    ///
    /// # Examples
    ///
    /// ```
    /// use infinite_iterator::InfiniteIterator;
    ///
    /// assert_eq!((5..).find_infinite(|&num| num > 10), 11);
    /// ```
    fn find_infinite<P>(&mut self, mut predicate: P) -> Self::Item
    where
        Self: Sized,
        P: FnMut(&Self::Item) -> bool,
    {
        loop {
            let item = self.next_infinite();
            if predicate(&item) {
                break item;
            }
        }
    }

    /// Like [`Iterator::find_map`],
    /// but it is guaranteed to find an item
    /// (or loop forever)
    /// because the iterator is infinite.
    ///
    /// # Examples
    ///
    /// ```
    /// use infinite_iterator::InfiniteIterator;
    ///
    /// assert_eq!((5_u32..).step_by(3).find_map_infinite(|num| num.checked_sub(10)), 1);
    /// ```
    fn find_map_infinite<B, F>(&mut self, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(Self::Item) -> Option<B>,
    {
        loop {
            if let Some(mapped) = f(self.next_infinite()) {
                break mapped;
            }
        }
    }

    /// Like [`Iterator::position`],
    /// but it is guaranteed to find an item
    /// (or loop forever)
    /// because the iterator is infinite.
    ///
    /// # Examples
    ///
    /// ```
    /// use infinite_iterator::InfiniteIterator;
    ///
    /// assert_eq!((5..).position_infinite(|num| num > 10), 6);
    /// ```
    fn position_infinite<P>(&mut self, mut predicate: P) -> usize
    where
        Self: Sized,
        P: FnMut(Self::Item) -> bool,
    {
        let mut i = 0;
        loop {
            if predicate(self.next_infinite()) {
                break i;
            }
            i += 1;
        }
    }
}

impl<I: ?Sized + InfiniteIterator> InfiniteIterator for &mut I {
    fn next_infinite(&mut self) -> Self::Item {
        (**self).next_infinite()
    }
}

#[cfg(feature = "alloc")]
impl<I: ?Sized + InfiniteIterator> InfiniteIterator for alloc::boxed::Box<I> {
    fn next_infinite(&mut self) -> Self::Item {
        (**self).next_infinite()
    }
}

impl<'item, I, T> InfiniteIterator for iter::Cloned<I>
where
    T: 'item + Clone,
    I: InfiniteIterator<Item = &'item T>,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<'item, I, T> InfiniteIterator for iter::Copied<I>
where
    T: 'item + Copy,
    I: InfiniteIterator<Item = &'item T>,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<A: Clone> InfiniteIterator for iter::Repeat<A> {
    fn next_infinite(&mut self) -> Self::Item {
        // SAFETY: `Repeat` never ends.
        unsafe { self.next().unwrap_unchecked() }
    }
}

impl<F: FnMut() -> A, A> InfiniteIterator for iter::RepeatWith<F> {
    fn next_infinite(&mut self) -> Self::Item {
        unsafe { self.next().unwrap_unchecked() }
    }
}

impl<A, B> InfiniteIterator for iter::Chain<A, B>
where
    A: Iterator,
    B: InfiniteIterator<Item = A::Item>,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<A, B> InfiniteIterator for iter::Zip<A, B>
where
    A: InfiniteIterator,
    B: InfiniteIterator,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I, P> InfiniteIterator for iter::Filter<I, P>
where
    I: InfiniteIterator,
    P: FnMut(&I::Item) -> bool,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<B, I, F> InfiniteIterator for iter::FilterMap<I, F>
where
    I: InfiniteIterator,
    F: FnMut(I::Item) -> Option<B>,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<B, I, F> InfiniteIterator for iter::Map<I, F>
where
    I: InfiniteIterator,
    F: FnMut(I::Item) -> Option<B>,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

// Require `InfiniteIterator` to prevent empty iterators
impl<I: Clone + InfiniteIterator> InfiniteIterator for iter::Cycle<I> {
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I: InfiniteIterator> InfiniteIterator for iter::Enumerate<I> {
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I: InfiniteIterator> InfiniteIterator for iter::Fuse<I> {
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I: InfiniteIterator> InfiniteIterator for iter::Peekable<I> {
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

/// An extension trait providing extra methods to [`iter::Peekable`]
/// when the underlying iterator never ends.
///
/// This trait is sealed;
/// it can only be implemented on [`iter::Peekable`].
pub trait PeekableExt: peekable_ext::Sealed {
    /// Like [`iter::Peekable::peek`],
    /// but always returning a reference
    /// because the underlying iterator never ends.
    fn peek_infinite(&mut self) -> &Self::Item;

    /// Like [`iter::Peekable::peek_mut`],
    /// but always returning a unique reference
    /// because the underlying iterator never ends.
    fn peek_infinite_mut(&mut self) -> &mut Self::Item;
}

mod peekable_ext {
    pub trait Sealed: Sized + Iterator {}
}

impl<I: InfiniteIterator> peekable_ext::Sealed for iter::Peekable<I> {}
impl<I: InfiniteIterator> PeekableExt for iter::Peekable<I> {
    fn peek_infinite(&mut self) -> &Self::Item {
        self.peek().unwrap()
    }

    fn peek_infinite_mut(&mut self) -> &mut Self::Item {
        self.peek_mut().unwrap()
    }
}

impl<I: InfiniteIterator> InfiniteIterator for iter::Skip<I> {
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I, P> InfiniteIterator for iter::SkipWhile<I, P>
where
    I: InfiniteIterator,
    P: FnMut(&I::Item) -> bool,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I: InfiniteIterator> InfiniteIterator for iter::StepBy<I> {
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I: InfiniteIterator, F> InfiniteIterator for iter::Inspect<I, F>
where
    I: InfiniteIterator,
    F: FnMut(&I::Item),
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I> InfiniteIterator for iter::Flatten<I>
where
    I: InfiniteIterator,
    I::Item: IntoIterator,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<I, U, F> InfiniteIterator for iter::FlatMap<I, U, F>
where
    I: InfiniteIterator,
    U: IntoIterator,
    F: FnMut(I::Item) -> U,
{
    fn next_infinite(&mut self) -> Self::Item {
        self.next().unwrap()
    }
}

impl<A> InfiniteIterator for core::ops::RangeFrom<A>
where
    core::ops::RangeFrom<A>: Iterator,
{
    fn next_infinite(&mut self) -> Self::Item {
        // SAFETY: The `RangeFrom` iterator never ends
        unsafe { self.next().unwrap_unchecked() }
    }
}

#[cfg(feature = "std")]
impl InfiniteIterator for std::net::Incoming<'_> {
    fn next_infinite(&mut self) -> Self::Item {
        // SAFETY: The Incoming iterator never ends.
        unsafe { self.next().unwrap_unchecked() }
    }
}

#[cfg(all(feature = "std", unix))]
impl InfiniteIterator for std::os::unix::net::Incoming<'_> {
    fn next_infinite(&mut self) -> Self::Item {
        // SAFETY: The Incoming iterator never ends
        unsafe { self.next().unwrap_unchecked() }
    }
}

/// An extension of `for in` loops with better support for infinite iterators.
///
/// This macro presents a _superset_ of regular `for` loops:
/// it works both with finite and infinite iterators.
///
/// # Examples
///
/// Use with a finite iterator:
///
/// ```
/// use infinite_iterator::ifor;
///
/// ifor!(item in [1, 2, 3] {
///     println!("{item}");
/// });
/// ```
///
/// Use with an infinite iterator:
///
/// ```
/// use infinite_iterator::ifor;
///
/// # fn run() -> ! {
/// ifor!(item in 0.. {
///     println!("{item}");
///     std::thread::sleep(std::time::Duration::from_secs(5));
/// })
/// # }
/// ```
///
/// Infinite iterators additionally support breaking with a value:
///
/// ```
/// use infinite_iterator::ifor;
///
/// let item = ifor!(item in 0.. {
///     if item > 10 {
///         break item;
///     }
/// });
///
/// assert_eq!(item, 11);
/// ```
///
/// You can use loop labels with an `ifor!` too,
/// as long as you write out the keyword `for`:
///
/// ```
/// use infinite_iterator::ifor;
///
/// ifor!('outer: for a in 0..10 {
///     ifor!('inner: for b in 0..10 {
///         println!("{a}, {b}");
///         if a + b > 16 {
///             break 'outer;
///         }
///     });
/// });
/// ```
#[macro_export]
macro_rules! ifor {
    ($($label:lifetime:)? for $pat:pat in $($rest:tt)*) => {
        $crate::__ifor_inner!($($label:)? for $pat in () $($rest)*)
    };
    ($pat:pat in $($rest:tt)*) => {
        $crate::__ifor_inner!(for $pat in () $($rest)*)
    };
}

// Not public API.
#[doc(hidden)]
#[macro_export]
macro_rules! __ifor_inner {
    ($($label:lifetime:)? for $pat:pat in ($expr:expr) $block:block) => {
        match $crate::__private::IntoIterator::into_iter($expr) {
            iter => {
                let mut iter = $crate::__private::MaybeInfinite(iter);
                $($label:)? loop {
                    let $pat = {
                        use $crate::__private::TryNextFallback;
                        match iter.try_next() {
                            $crate::__private::Ok(item) => item,
                            $crate::__private::Err(breakable) => {
                                break breakable.into_break()
                            },
                        }
                    };
                    $block
                }
            }
        }
    };
    ($($label:lifetime:)? for $pat:pat in () $block:block) => {
        $crate::__private::compile_error!("no expression provided to `ifor!`")
    };
    ($($label:lifetime:)? for $pat:pat in ($($expr:tt)*) $first:tt $($rest:tt)*) => {
        $crate::__ifor_inner!($($label:)? for $pat in ($($expr)* $first) $($rest)*)
    };
}

// Not public API.
#[doc(hidden)]
pub mod __private {
    use crate::InfiniteIterator;

    pub use core::compile_error;
    pub use Err;
    pub use IntoIterator;
    pub use Ok;

    pub struct MaybeInfinite<I>(pub I);

    impl<I: InfiniteIterator> MaybeInfinite<I> {
        pub fn try_next(&mut self) -> Result<I::Item, NeverBreak> {
            Ok(self.0.next_infinite())
        }
    }

    pub trait TryNextFallback: Sized {
        type Item;
        fn try_next(&mut self) -> Result<Self::Item, CanBreak>;
    }
    impl<I: Iterator> TryNextFallback for MaybeInfinite<I> {
        type Item = I::Item;
        fn try_next(&mut self) -> Result<Self::Item, CanBreak> {
            self.0.next().ok_or(CanBreak)
        }
    }

    pub enum NeverBreak {}
    impl NeverBreak {
        pub fn into_break(self) -> ! {
            match self {}
        }
    }

    pub struct CanBreak;
    impl CanBreak {
        pub fn into_break(self) {}
    }
}