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
/// Main _ad-hoc_ / closure-based constructor of `LendingIterator`s.
///
/// It expects the both necessary and sufficient elements for such an impl:
///
/// - a `State`, which will play a role akin to that of `Self` in a manual
/// impl;
///
/// - a `fn next` "method" on it. There is actually a certain level of
/// flexibility gained from this being a closure rather than a stateless
/// associated function.
///
/// For instance, non-lent state (such as an internal mutable counter) can
/// be implicity captured by such a closure, without having to funnel it
/// through the lendable `State`.
///
/// ## Example
///
/** - ```rust
use ::lending_iterator::prelude::*;
struct Person {
name: String,
age: u8,
}
fn example (person: &mut Person)
-> impl '_ + LendingIterator
/* or:
-> impl '_ + LendingIteratorDyn<Item = HKT!(&str)>
*/
{
lending_iterator::from_fn::<HKT!(&str), _, _>(
person,
|p| if p.age > 0 {
Some(&p.name)
} else {
None
},
)
}
``` */
///
/// ## Remarks
///
/// It can also be viewed as a convenience layer over:
///
/// <code>::lending_iterator::[repeat_mut]\(state\)<br/> [.filter_map]::\<Item, _\>\(move |\[\], it| next\(it\)\)</code>
///
/** - ```rust
use ::lending_iterator::prelude::*;
struct Person {
name: String,
age: u8,
}
fn example (person: &mut Person)
-> impl '_ + LendingIterator
/* or:
-> impl '_ + LendingIteratorDyn<Item = HKT!(&str)>
*/
{
lending_iterator::repeat_mut(person)
.filter_map::<HKT!(&str), _>(
|[], p| if p.age > 0 {
Some(&p.name)
} else {
None
},
)
}
``` */
///
/// [repeat_mut]: crate::repeat_mut()
/// [.filter_map]: crate::LendingIterator::filter_map
///
/// ### `FromFn`
///
/// The returned `struct` —[`FromFn`]— can also be used directly, to benefit
/// from "named arguments", at the cost of having to provide a `PhantomData`
/// parameter.
pub
fn from_fn<Item, State, Next> (
state: State,
next: Next,
) -> FromFn<Item, State, Next>
where
Item : HKT,
Next : FnMut(&'_ mut State) -> Option< A!(Item<'_>) >,
{
FromFn { state, next, _phantom: <_>::default() }
}
/// The <code>impl [LendingIterator]</code> returned by [`from_fn()`].
///
/// ## Example
///
/** - ```rust
use ::lending_iterator::prelude::*;
struct Person {
name: String,
age: u8,
}
fn example (person: &mut Person)
-> impl '_ + LendingIterator
/* or:
-> impl '_ + LendingIteratorDyn<Item = HKT!(&str)>
*/
{
lending_iterator::FromFn::<HKT!(&str), _, _> {
state: person,
next: |p| if p.age > 0 {
Some(&p.name)
} else {
None
},
_phantom: <_>::default(),
}
}
``` */
pub
struct FromFn<Item, State, Next>
where
Item : HKT,
Next : FnMut(&'_ mut State) -> Option< A!(Item<'_>) >,
{
/// The state owned by this [`LendingIterator`].
///
/// - Think of `Self` within a manual implementation of the trait;
///
/// - Think of [`repeat_mut()`].
pub
state: State,
/// The "`fn next()`" of a "manual implementation of the trait".
///
/// Trick: since it's only required to be a closure, this `Next` closure
/// can capture state on its own, provided it does not need to lend from it.
///
/// This can lead to slightly more lightweight `FromFn` / `from_fn` calls:
/// - put the lent / borrowed state inside `.state`,
/// - let the rest of the state be implicitly `move`-captured by this closure.
pub
next: Next,
/// The signature of `fn next` in a `PhantomData`.
pub
_phantom: PhantomData<
fn(&mut State) -> Option<A!(Item<'_>)>,
>,
}
#[gat]
impl<Item, State, Next>
LendingIterator
for
FromFn<Item, State, Next>
where
Item : HKT,
Next : FnMut(&'_ mut State) -> Option< A!(Item<'_>) >,
{
type Item<'next>
where
Self : 'next,
=
A!(Item<'next>)
;
fn next (self: &'_ mut FromFn<Item, State, Next>)
-> Option< A!(Item<'_>) >
{
let Self { state, next, .. } = self;
next(state)
}
}