::lending-iterator

Fully generic LendingIterators in stable Rust.

Repository Latest version Documentation MSRV unsafe forbidden License CI

Examples

Click to hide

windows_mut()!

```rust use ::lending_iterator::prelude::*;

let mut array = [0; 15]; array[1] = 1; // Cumulative sums are trivial with a mut sliding window, // so let's showcase that by generating a Fibonacci sequence. let mut iter = array.windowsmut::<3>(); // windowsmut::<_, 3>(&mut array); while let Some(&mut [a, b, ref mut next]) = iter.next() { *next = a + b; } assert_eq!( array, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], ); ```

Rolling your own version of it using the handy from_fn constructor

```rust use ::lending_iterator::prelude::*;

let mut array = [0; 15]; array[1] = 1; // Let's hand-roll our iterator lending &mut sliding windows: let mut iter = { let mut start = 0; lendingiterator::FromFn:: { state: &mut array[..], next: move |slice| { let toyield = slice .getmut(start..)? .getmut(..3)? .tryinto() // &mut [u8] -> &mut [u8; 3] .unwrap() ; start += 1; Some(toyield) }, phantom: <_>::default(), } }; while let Some(&mut [a, b, ref mut next]) = iter.next() { *next = a + b; } asserteq!( array, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], ); ```

LendingIterator adapters

```rust use ::lending_iterator::prelude::*;

let mut array = [0; 15]; array[1] = 1; // Let's hand-roll our iterator lending &mut sliding windows: let mut iter = { ::lendingiterator::repeatmut((0, &mut array)) .filtermaptomut(|[], (start, array)| -> Option<&mut [u16]> { let toyield = array .getmut(*start..)? .getmut(..3)? ; *start += 1; Some(toyield) }) .maptomut::<[u16; 3], _>(|[], slice| slice.tryinto().unwrap()) }; while let Some(&mut [a, b, ref mut next]) = iter.next() { *next = a + b; } // drop(iter); assert_eq!( array, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], ); ```