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
//! [`LendingIterator`] adapters.
//!
//! # Example
//!
//! <details open class="custom"><summary><span class="summary-box"><span>Click to hide</span></span></summary>
//!
/*! - ```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 = {
// initial state:
::lending_iterator::repeat_mut((&mut array, 0))
// main logic (lending _slices_):
.filter_map::<HKT!(&mut [u16]), _>(|[], (array, start)| {
let to_yield =
array
.get_mut(*start..)?
.get_mut(..3)?
;
*start += 1;
Some(to_yield)
})
// tiny type adaptor (lending _arrays_):
.map_to_mut(|[], slice| <&mut [u16; 3]>::try_from(slice).unwrap())
// ⬑convenience (no need to turbofish an HKT) that is equivalent to:
// .map::<HKT!(&mut [u16; 3]), _>(|[], slice| <_>::try_from(slice).unwrap())
};
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],
);
``` */
//!
//! Notice how any adapters that return a lending / dependent type (such as
//! `&mut …` for `.{filter_,}map()`) are required to take an extra `[]`
//! "dummy" parameter for the closure input. This is due to a technical
//! limitation of the language, and having to add `[], ` was the least
//! cumbersome way that I could find to work around it 😔
//!
//! - See [`LendingIterator::map()`] for more info about it.
//!
//! </details>
use super::*;
match_! {(
filter,
filter_map,
fuse,
into_iter,
map,
skip,
take,
) {(
$(
$(#[$attrs:meta])*
$module:ident
),* $(,)?
) => (
$(
$(#[$attrs])*
pub use self::$module::*;
$(#[$attrs])*
mod $module {
use super::*;
include!(concat!(stringify!($module), ".rs"));
}
)*
)}}