lending_iterator/lending_iterator/adapters/_mod.rs
1//! [`LendingIterator`] adapters.
2//!
3//! # Example
4//!
5//! <details open class="custom"><summary><span class="summary-box"><span>Click to hide</span></span></summary>
6//!
7/*! - ```rust
8 use ::lending_iterator::prelude::*;
9
10 let mut array = [0; 15];
11 array[1] = 1;
12 // Let's hand-roll our iterator lending `&mut` sliding windows:
13 let mut iter = {
14 // initial state:
15 ::lending_iterator::repeat_mut((&mut array, 0))
16 // main logic (lending _slices_):
17 .filter_map::<HKT!(&mut [u16]), _>(|[], (array, start)| {
18 let to_yield =
19 array
20 .get_mut(*start..)?
21 .get_mut(..3)?
22 ;
23 *start += 1;
24 Some(to_yield)
25 })
26 // tiny type adaptor (lending _arrays_):
27 .map_to_mut(|[], slice| <&mut [u16; 3]>::try_from(slice).unwrap())
28 // ⬑convenience (no need to turbofish an HKT) that is equivalent to:
29 // .map::<HKT!(&mut [u16; 3]), _>(|[], slice| <_>::try_from(slice).unwrap())
30 };
31 while let Some(&mut [a, b, ref mut next]) = iter.next() {
32 *next = a + b;
33 }
34 assert_eq!(
35 array,
36 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377],
37 );
38 ``` */
39//!
40//! Notice how any adapters that return a lending / dependent type (such as
41//! `&mut …` for `.{filter_,}map()`) are required to take an extra `[]`
42//! "dummy" parameter for the closure input. This is due to a technical
43//! limitation of the language, and having to add `[], ` was the least
44//! cumbersome way that I could find to work around it 😔
45//!
46//! - See [`LendingIterator::map()`] for more info about it.
47//!
48//! </details>
49
50use super::*;
51
52match_! {(
53 filter,
54 filter_map,
55 fuse,
56 into_iter,
57 map,
58 skip,
59 take,
60) {(
61 $(
62 $(#[$attrs:meta])*
63 $module:ident
64 ),* $(,)?
65) => (
66 $(
67 $(#[$attrs])*
68 pub use self::$module::*;
69 $(#[$attrs])*
70 mod $module {
71 use super::*;
72
73 include!(concat!(stringify!($module), ".rs"));
74 }
75 )*
76)}}