lender 0.6.2

A lending-iterator trait based on higher-rank trait bounds, with full std::iter::Iterator functionality
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! # Types and Traits for Higher-Order Types with Covariance Checks
//!
//! - Flexible function signatures, to work around function lifetime
//!   signature restrictions.
//!
//! - Higher-Rank Closures, with macro checking covariance of return
//!   types with respect to bound lifetimes.
//!
//! - [`Covar`] transparent wrapper to mark covariance-checked
//!   closures.
//!
//! Use the [`covar!`](`crate::covar`),
//! [`covar_mut!`](`crate::covar_mut`), or
//! [`covar_once!`](`crate::covar_once`) macros to create a
//! covariance-checked higher-rank closure wrapped in [`Covar`].
//!
//! These macros are modified versions of the
//! [`higher_order_closure!`](https://crates.io/crates/higher-order-closure)
//! macro, adding a covariance check and the [`Covar`] wrapper.
//! They share the same underlying mechanism: a "funnel" helper
//! function whose `Fn` trait bounds enforce the desired higher-order
//! signature on the closure.
//!
//! # Lifetime elision
//!
//! When the closure has a return type, an explicit `for<'a>` clause
//! is **required** so that the macro can name the lifetime it
//! checks for covariance:
//!
//! ```rust
//! # use lender::prelude::*;
//! let mut data = [1, 2, 3];
//! let mut lender = lender::windows_mut(&mut data, 2)
//!     .map(covar_mut!(
//!         for<'lend> |w: &'lend mut [i32]| -> &'lend mut i32 {
//!             &mut w[0]
//!         }
//!     ));
//! assert_eq!(lender.next(), Some(&mut 1));
//! ```
//!
//! When the closure has **no** return type (i.e., it returns `()`),
//! the `for<>` clause can be omitted. In that case, [lifetime
//! elision rules for function
//! signatures](https://doc.rust-lang.org/reference/lifetime-elision.html#lifetime-elision-in-functions)
//! apply inside the funnel's `Fn` trait bound, making the
//! closure higher-order automatically. No covariance check is
//! needed because `()` is trivially covariant.
//!
//! ```rust
//! # use lender::prelude::*;
//! let mut data = [0, 1, 0, 0, 0, 0, 0, 0, 0];
//! lender::windows_mut(&mut data, 3)
//!     .for_each(|w| {
//!         w[2] = w[0] + w[1];
//!     });
//! assert_eq!(data, [0, 1, 1, 2, 3, 5, 8, 13, 21]);
//! ```
//!
//! # Outer generic parameters: `#![with<…>]`
//!
//! The macros internally generate a helper function, so generic
//! parameters from the enclosing scope are not directly available
//! in the closure signature (similar to how nested `fn` items
//! cannot use outer generics). The `#![with<…>]` attribute
//! re-introduces them:
//!
//! ```rust
//! # use lender::prelude::*;
//! fn display_first<T: core::fmt::Display>(data: &mut [T]) {
//!     let mut lender = lender::windows_mut(data, 1)
//!         .map(covar_mut!(
//!             #![with<T: core::fmt::Display>]
//!             for<'lend> |w: &'lend mut [T]|
//!                 -> &'lend dyn core::fmt::Display
//!             {
//!                 &w[0]
//!             }
//!         ));
//!     if let Some(d) = lender.next() {
//!         println!("{d}");
//!     }
//! }
//! # display_first(&mut [1, 2, 3]);
//! ```
//!
//! The generics inside `#![with<…>]` support a restricted syntax:
//!
//! - **Lifetime parameters** with at most one super-lifetime bound:
//!   `'a`, `'b: 'a`.
//!
//! - **Type parameters** with an optional `?Sized`, followed by an
//!   optional lifetime bound, followed by an optional trait bound:
//!   `T`, `U: ?Sized + 'a + core::fmt::Debug`.
//!
//! For bounds that exceed this "simple shape", use a `where` clause
//! after the generics:
//!
//! ```rust
//! # use lender::prelude::*;
//! fn display_debug<T>(data: &mut [T])
//! where
//!     T: core::fmt::Display + core::fmt::Debug,
//! {
//!     let mut lender = lender::windows_mut(data, 1)
//!         .map(covar_mut!(
//!             #![with<T>
//!                 where T: core::fmt::Display
//!                        + core::fmt::Debug
//!             ]
//!             for<'lend> |w: &'lend mut [T]|
//!                 -> &'lend dyn core::fmt::Display
//!             {
//!                 &w[0]
//!             }
//!         ));
//!     if let Some(d) = lender.next() {
//!         println!("{d}");
//!     }
//! }
//! # display_debug(&mut [1, 2, 3]);
//! ```
//!
//! In practice, bounds inside `#![with<…>]` are seldom needed
//! because the generics are only used for the *signature* of the
//! closure, not its body.

/// A transparent wrapper that seals a closure whose covariance has been
/// verified at construction time by the [`covar!`](crate::covar),
/// [`covar_mut!`](crate::covar_mut), or [`covar_once!`](crate::covar_once)
/// macros.
///
/// Adapter structs like [`Map`](crate::Map) store `Covar<F>` and call the
/// inner closure through [`as_inner`](Covar::as_inner) and
/// [`as_inner_mut`](Covar::as_inner_mut), or
/// [`into_inner`](Covar::into_inner).
///
/// `Covar<F>` cannot be constructed safely by user code (the
/// constructor is unsafe, and also hidden to discourage usage).
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Covar<F>(F);

impl<F> Covar<F> {
    /// Creates a new `Covar<F>`.
    ///
    /// Please don't use this constructor unless you really need to. The
    /// main usage is creation of covariance-checked closures inside
    /// adapters for which covariance is known in advance, such
    /// as [`Map`](crate::Map).
    ///
    /// # Safety
    ///
    /// The caller must ensure that `f` produces covariant output with
    /// respect to any lifetime parameters in its signature. This is
    /// guaranteed by the [`covar!`](crate::covar),
    /// [`covar_mut!`](crate::covar_mut), and
    /// [`covar_once!`](crate::covar_once) macros (for one parameter).
    #[doc(hidden)]
    #[inline(always)]
    pub unsafe fn __new(f: F) -> Self {
        Covar(f)
    }

    /// Returns a reference to the inner closure.
    #[inline(always)]
    pub fn as_inner(&self) -> &F {
        &self.0
    }

    /// Returns a mutable reference to the inner closure.
    #[inline(always)]
    pub fn as_inner_mut(&mut self) -> &mut F {
        &mut self.0
    }

    /// Unwraps and returns the inner closure.
    #[inline(always)]
    pub fn into_inner(self) -> F {
        self.0
    }
}

/// Higher-Kinded Associated Output [`FnOnce`], where `Output`
/// (`B`) has lifetime `'b`.
pub trait FnOnceHKA<'b, A>: FnOnce(A) -> <Self as FnOnceHKA<'b, A>>::B {
    type B: 'b;
}

impl<'b, A, B: 'b, F: FnOnce(A) -> B> FnOnceHKA<'b, A> for F {
    type B = B;
}

/// Higher-Kinded Associated Output [`FnMut`], where `Output`
/// (`B`) has lifetime `'b`.
pub trait FnMutHKA<'b, A>: FnMut(A) -> <Self as FnMutHKA<'b, A>>::B {
    type B: 'b;
}

impl<'b, A, B: 'b, F: FnMut(A) -> B> FnMutHKA<'b, A> for F {
    type B = B;
}

/// Higher-Kinded Associated Output [`FnMut`], where `Output`
/// ([`Option<B>`](Option)) has lifetime `'b`.
pub trait FnMutHKAOpt<'b, A>: FnMut(A) -> Option<<Self as FnMutHKAOpt<'b, A>>::B> {
    type B: 'b;
}

impl<'b, A, B: 'b, F: FnMut(A) -> Option<B>> FnMutHKAOpt<'b, A> for F {
    type B = B;
}

/// Higher-Kinded Associated Output [`FnOnce`], where `Output`
/// ([`Result<B, E>`](Result)) has output type `B` with lifetime `'b`.
pub trait FnOnceHKARes<'b, A, E>:
    FnOnce(A) -> Result<<Self as FnOnceHKARes<'b, A, E>>::B, E>
{
    type B: 'b;
}

impl<'b, A, B: 'b, E, F: FnOnce(A) -> Result<B, E>> FnOnceHKARes<'b, A, E> for F {
    type B = B;
}

/// Higher-Kinded Associated Output [`FnMut`], where `Output`
/// ([`Result<B, E>`](Result)) has output type `B` with lifetime `'b`.
pub trait FnMutHKARes<'b, A, E>: FnMut(A) -> Result<<Self as FnMutHKARes<'b, A, E>>::B, E> {
    type B: 'b;
}

impl<'b, A, B: 'b, E, F: FnMut(A) -> Result<B, E>> FnMutHKARes<'b, A, E> for F {
    type B = B;
}

/// Higher-Kinded Associated Output [`FnMut`], where `Output`
/// (`Result<Option<B>, E>`) has output type `B` with lifetime `'b`.
pub trait FnMutHKAResOpt<'b, A, E>:
    FnMut(A) -> Result<Option<<Self as FnMutHKAResOpt<'b, A, E>>::B>, E>
{
    type B: 'b;
}

impl<'b, A, B: 'b, E, F: FnMut(A) -> Result<Option<B>, E>> FnMutHKAResOpt<'b, A, E> for F {
    type B = B;
}

/// Not meant to be called directly. A modified version of
/// [`higher-order-closure`](https://crates.io/crates/higher-order-closure)
/// `higher_order_closure` macro to use any [`Fn`] trait.
///
/// Performs a covariance check when a return type is specified with a
/// `for<>` bound.
#[doc(hidden)]
#[macro_export]
macro_rules! __covar__ {
    // Case 1: With for<'lifetime> and return type - includes covariance check
    //
    // $hr ("higher-rank") is the universally quantified lifetime
    // from the `for<'hr>` clause.  It is the lifetime whose
    // covariance in $Ret the macro checks.
    //
    // $lt ("lifetime") are optional *outer* lifetime parameters
    // introduced via `#![with<'a, 'b, …>]`.  They let $Ret
    // reference lifetimes from the enclosing scope that are NOT
    // higher-rank.  They appear in __CovarCheck and
    // __check_covariance but are passed through unchanged — only
    // $hr is tested for covariance.
    (
        $F:ident,
        $(#![
            with<
                $($(
                    $lt:lifetime $(: $super_lt:lifetime)?
                ),+ $(,)?)?
                $($(
                    $T:ident $(:
                        $(
                            ?$Sized:ident $(+)?
                        )?
                        $(
                            $super:lifetime $(+)?
                        )?
                        $(
                            $Trait:path
                        )?
                    )?
                ),+ $(,)?)?
            >
            $(where
                $($wc:tt)*
            )?
        ])?

        for<$hr:lifetime>
        $( move $(@$move:tt)?)?
        | $($arg:tt : $Arg:ty),* $(,)?|
        -> $Ret:ty
        $body:block
    ) => (
        // SAFETY: the covariance check inside __funnel__ guarantees
        // that the return type is covariant in the bound lifetime.
        unsafe { $crate::higher_order::Covar::__new(
        ({
            fn __funnel__<
                $(
                    $($(
                        $lt $(: $super_lt)?
                        ,
                    )+)?
                    $($(
                        $T
                        $(:
                            $(?$Sized +)?
                            $($super +)?
                            $($Trait)?
                        )?
                        ,
                    )+)?
                )?
                    __Closure,
                >
            (
                f: __Closure,
            ) -> __Closure
            where
                __Closure : for<$hr> ::core::ops::$F($($Arg),*) -> $Ret,
                $($($($wc)*)?)?
            {
                // Covariance check: this struct has the same variance as $Ret
                // with respect to the bound lifetime. The PhantomData<&'a ()>
                // ensures the lifetime is used even if $Ret doesn't contain it.
                #[allow(dead_code)]
                struct __CovarCheck<
                    $(
                        $($(
                            $lt,
                        )+)?
                    )?
                    $hr
                >(
                    ::core::marker::PhantomData<
                        (
                            fn() -> ($Ret, &$hr ()),
                            $(
                                $($(
                                    &$lt (),
                                )+)?
                            )?
                        )
                    >
                );

                // This function only compiles if __CovarCheck (and thus $Ret)
                // is covariant in the lifetime parameter. See the documentation of
                // Lender::__check_covariance for details.
                #[allow(dead_code)]
                fn __check_covariance<
                    $(
                        $($(
                            $lt,
                        )+)?
                    )?
                    '__long: '__short, '__short
                >(
                    x: *const __CovarCheck<
                        $(
                            $($(
                                $lt,
                            )+)?
                        )?
                        '__long
                    >,
                ) -> *const __CovarCheck<
                    $(
                        $($(
                            $lt,
                        )+)?
                    )?
                    '__short
                > {
                    x
                }

                f
            }

            __funnel__::<$($($($T ,)+)?)? _>
        })(
            $(move $($move)?)? |$($arg),*| $body
        )
        ) }
    );

    // Case 2: No return type - no covariance check needed
    //
    // Without a return type the closure returns `()`, which is
    // trivially covariant, so no check is required.
    //
    // A return type WITHOUT a `for<>` clause intentionally fails to
    // match either case, producing a compile error: the user must
    // provide an explicit `for<'a>` so that the covariance check in
    // Case 1 can name the lifetime it checks.
    (
        $F:ident,
        $(#![
            with<
                $($(
                    $lt:lifetime $(: $super_lt:lifetime)?
                ),+ $(,)?)?
                $($(
                    $T:ident $(:
                        $(
                            ?$Sized:ident $(+)?
                        )?
                        $(
                            $super:lifetime $(+)?
                        )?
                        $(
                            $Trait:path
                        )?
                    )?
                ),+ $(,)?)?
            >
            $(where
                $($wc:tt)*
            )?
        ])?

        $( for<$hr:lifetime> )?
        $( move $(@$move:tt)?)?
        | $($arg:tt : $Arg:ty),* $(,)?|
        $body:block
    ) => (
        // SAFETY: no return type means the closure returns `()`,
        // which is covariant in every lifetime.
        unsafe { $crate::higher_order::Covar::__new(
        ({
            fn __funnel__<
                $(
                    $($(
                        $lt $(: $super_lt)?
                        ,
                    )+)?
                    $($(
                        $T
                        $(:
                            $(?$Sized +)?
                            $($super +)?
                            $($Trait)?
                        )?
                        ,
                    )+)?
                )?
                    __Closure,
                >
            (
                f: __Closure,
            ) -> __Closure
            where
                __Closure : $(for<$hr>)? ::core::ops::$F($($Arg),*),
                $($($($wc)*)?)?
            {
                f
            }

            __funnel__::<$($($($T ,)+)?)? _>
        })(
            $(move $($move)?)? |$($arg),*| $body
        )
        ) }
    );
}

/// Covariance-checked [`FnOnce`] closure macro.
///
/// Creates a [`Covar`]-wrapped closure with explicit lifetime bounds and a
/// compile-time covariance check (when a return type is specified with
/// `for<'a>` bounds). This ensures that the return type is covariant in the
/// bound lifetime, which is required for soundness with lending iterators.
///
/// Use `covar_once!` when the closure will only be called once
/// (e.g., with [`Lender::fold`](crate::Lender::fold)). For
/// closures that may be called multiple times, use
/// [`covar_mut!`](crate::covar_mut) or
/// [`covar!`](crate::covar).
///
/// # Syntax
///
/// ```text
/// covar_once!(for<'a> |arg: Type<'a>| -> ReturnType<'a> { body })
/// covar_once!(for<'a> move |arg: Type<'a>| -> ReturnType<'a> { body })
/// ```
///
/// # Examples
///
/// ```rust
/// # use lender::prelude::*;
/// let mut lender = lender::once_with(42,
///     covar_once!(for<'lend> |state: &'lend mut i32| -> &'lend mut i32 {
///         *state += 1;
///         state
///     })
/// );
/// assert_eq!(lender.next(), Some(&mut 43));
/// assert_eq!(lender.next(), None);
/// ```
#[macro_export]
macro_rules! covar_once {($($t:tt)+) => ($crate::__covar__!(FnOnce, $($t)+))}

/// Covariance-checked [`FnMut`] closure macro.
///
/// Creates a [`Covar`]-wrapped closure with explicit lifetime bounds and a
/// compile-time covariance check (when a return type is specified with
/// `for<'a>` bounds). This ensures that the return type is covariant in the
/// bound lifetime, which is required for soundness with lending iterators.
///
/// Use `covar_mut!` when the closure may be called multiple times and
/// captures mutable state or needs `&mut self` semantics. This is the most
/// commonly used variant for lender methods like
/// [`Lender::map`](crate::Lender::map),
/// [`Lender::for_each`](crate::Lender::for_each),
/// [`Lender::filter_map`](crate::Lender::filter_map), and
/// [`Lender::scan`](crate::Lender::scan).
///
/// # Syntax
///
/// ```text
/// covar_mut!(for<'a> |arg: Type<'a>| -> ReturnType<'a> { body })
/// covar_mut!(for<'a> move |arg: Type<'a>| -> ReturnType<'a> { body })
/// ```
///
/// # Examples
///
/// ```rust
/// # use lender::prelude::*;
/// let mut data = [1, 2, 3, 4];
/// let mut lender = lender::windows_mut(&mut data, 2)
///     .map(covar_mut!(for<'lend> |w: &'lend mut [i32]| -> &'lend mut i32 {
///         &mut w[0]
///     }));
/// assert_eq!(lender.next(), Some(&mut 1));
/// ```
///
/// ```rust
/// # use lender::prelude::*;
/// let mut data = [0, 1, 0, 0, 0, 0, 0, 0, 0];
/// lender::windows_mut(&mut data, 3)
///     .for_each(|w| {
///         w[2] = w[0] + w[1];  // Compute Fibonacci
///     });
/// assert_eq!(data, [0, 1, 1, 2, 3, 5, 8, 13, 21]);
/// ```
#[macro_export]
macro_rules! covar_mut {($($t:tt)+) => ($crate::__covar__!(FnMut, $($t)+))}

/// Covariance-checked [`Fn`] closure macro.
///
/// Creates a [`Covar`]-wrapped closure with explicit lifetime bounds and a
/// compile-time covariance check (when a return type is specified with
/// `for<'a>` bounds). This ensures that the return type is covariant in the
/// bound lifetime, which is required for soundness with lending iterators.
///
/// Use `covar!` when the closure only needs shared access to its captures
/// (`&self` semantics) and may be called multiple times. In practice,
/// [`covar_mut!`](crate::covar_mut) is more commonly used since most lender
/// methods require [`FnMut`].
///
/// # Syntax
///
/// ```text
/// covar!(for<'a> |arg: Type<'a>| -> ReturnType<'a> { body })
/// covar!(for<'a> move |arg: Type<'a>| -> ReturnType<'a> { body })
/// ```
///
/// # Examples
///
/// ```rust
/// # use lender::prelude::*;
/// let data = [1, 2, 3];
/// let lender = data.iter().into_lender();
/// let mapped = lender.map(
///     covar!(for<'lend> |x: &'lend i32| -> i32 { *x * 2 }),
/// );
/// ```
#[macro_export]
macro_rules! covar {($($t:tt)+) => ($crate::__covar__!(Fn, $($t)+))}