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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
mod accum;
mod collect;
mod double_ended;
mod exact_size;
mod ext;
mod fallible_lender;
mod lender;
mod marker;

pub use self::{
    accum::{ProductFallibleLender, ProductLender, SumFallibleLender, SumLender},
    collect::{
        ExtendFallibleLender, ExtendLender, FromFallibleLender, FromLender, IntoFallibleLender,
        IntoLender,
    },
    double_ended::{DoubleEndedFallibleLender, DoubleEndedLender},
    exact_size::{ExactSizeFallibleLender, ExactSizeLender},
    ext::{
        FallibleIteratorExt, FallibleIteratorRefExt, IntoFallibleIteratorExt, IntoIteratorExt,
        IteratorExt, IteratorRefExt,
    },
    fallible_lender::{FallibleLend, FallibleLender, FallibleLending},
    lender::{Lend, Lender, Lending},
    marker::{FusedFallibleLender, FusedLender},
};

/// Trait for lend types that can be destructured into two components, used
/// by [`Lender::unzip()`] and
/// [`FallibleLender::unzip()`](crate::FallibleLender::unzip).
///
/// Implemented for `(A, B)`, `&(A, B)`, and `&mut (A, B)`.
pub trait TupleLend<'a> {
    type First: 'a;
    type Second: 'a;
    fn tuple_lend(self) -> (Self::First, Self::Second);
}

impl<'a, A: 'a, B: 'a> TupleLend<'a> for (A, B) {
    type First = A;
    type Second = B;
    #[inline(always)]
    fn tuple_lend(self) -> (Self::First, Self::Second) {
        (self.0, self.1)
    }
}

impl<'a, A, B> TupleLend<'a> for &'a (A, B) {
    type First = &'a A;
    type Second = &'a B;
    #[inline(always)]
    fn tuple_lend(self) -> (Self::First, Self::Second) {
        (&self.0, &self.1)
    }
}

impl<'a, A, B> TupleLend<'a> for &'a mut (A, B) {
    type First = &'a mut A;
    type Second = &'a mut B;
    #[inline(always)]
    fn tuple_lend(self) -> (Self::First, Self::Second) {
        (&mut self.0, &mut self.1)
    }
}

/// A covariance witness whose private field prevents construction
/// outside this crate, making it impossible to bypass covariance
/// checks without `unsafe`.
#[doc(hidden)]
pub struct CovariantProof<T>(core::marker::PhantomData<fn() -> T>);

impl<T> CovariantProof<T> {
    #[doc(hidden)]
    pub(crate) fn new() -> Self {
        CovariantProof(core::marker::PhantomData)
    }
}

/// Internal struct used to implement [`lend!`], do not use directly.
#[doc(hidden)]
pub struct DynLendShunt<T: ?Sized>(pub T);

impl<'lend, T: ?Sized + for<'all> DynLend<'all>> Lending<'lend> for DynLendShunt<T> {
    type Lend = <T as DynLend<'lend>>::Lend;
}

/// Marker trait certifying that the [`Lend`](Lending::Lend) type is covariant
/// in its lifetime parameter.
///
/// This trait is automatically implemented for types created by [`lend!`] and
/// [`covariant_lend!`]. It is required by [`lend_iter`](crate::lend_iter) and
/// similar functions that perform lifetime transmutes which are only sound when
/// the lend type is covariant.
///
/// Users should not need to implement this trait directly. Use [`lend!`] for
/// simple patterns or [`covariant_lend!`] for complex types.
///
/// The required method
/// [`__check_covariance`](CovariantLending::__check_covariance) enforces
/// covariance: its safe body `{ proof }` only compiles when the
/// [`Lend`](Lending::Lend) type is covariant, so non-covariant types cannot
/// implement this trait without `unsafe` with a returning body. See the
/// documentation of
/// [`Lender::__check_covariance`](crate::Lender::__check_covariance) for
/// details.
#[doc(hidden)]
pub trait CovariantLending: for<'all> Lending<'all> {
    fn __check_covariance<'long: 'short, 'short>(
        proof: CovariantProof<<Self as Lending<'long>>::Lend>,
    ) -> CovariantProof<<Self as Lending<'short>>::Lend>;
}

// SAFETY: lend!() only accepts type patterns that are syntactically covariant
// in 'lend (references, slices, tuples of identifiers, etc.)
impl<T: ?Sized + for<'all> DynLend<'all>> CovariantLending for DynLendShunt<T> {
    fn __check_covariance<'long: 'short, 'short>(
        proof: CovariantProof<<Self as Lending<'long>>::Lend>,
    ) -> CovariantProof<<Self as Lending<'short>>::Lend> {
        // SAFETY: lend!() only accepts syntactically covariant patterns
        unsafe { core::mem::transmute(proof) }
    }
}

/// Internal trait used to implement [`lend!`], do not use directly.
#[doc(hidden)]
pub trait DynLend<'lend> {
    type Lend;
}

/// Internal macro used by [`lend!`] and [`fallible_lend!`] to avoid duplication.
/// Do not use directly.
#[doc(hidden)]
#[macro_export]
macro_rules! __lend_impl {
    // Identifier (no lifetime - trivially covariant)
    ($Shunt:ident, $Trait:ident, $T:ident) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = $T>>
    };
    // Reference to identifier
    ($Shunt:ident, $Trait:ident, &$lt:lifetime $T:ident) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt $T>>
    };
    // Mutable reference to identifier
    ($Shunt:ident, $Trait:ident, &$lt:lifetime mut $T:ident) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt mut $T>>
    };
    // Reference to str
    ($Shunt:ident, $Trait:ident, &$lt:lifetime str) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt str>>
    };
    // Mutable reference to str
    ($Shunt:ident, $Trait:ident, &$lt:lifetime mut str) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt mut str>>
    };
    // Reference to slice
    ($Shunt:ident, $Trait:ident, &$lt:lifetime [$T:ident]) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt [$T]>>
    };
    // Mutable reference to slice
    ($Shunt:ident, $Trait:ident, &$lt:lifetime mut [$T:ident]) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt mut [$T]>>
    };
    // Vec<T>
    ($Shunt:ident, $Trait:ident, Vec<$T:ident>) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = Vec<$T>>>
    };
    // Reference to Vec<T>
    ($Shunt:ident, $Trait:ident, &$lt:lifetime Vec<$T:ident>) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt Vec<$T>>>
    };
    // Mutable reference to Vec<T>
    ($Shunt:ident, $Trait:ident, &$lt:lifetime mut Vec<$T:ident>) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt mut Vec<$T>>>
    };
    // Double reference (& &'lend T)
    ($Shunt:ident, $Trait:ident, & &$lt:lifetime $T:ident) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = & &$lt $T>>
    };
    // Double reference (&&'lend T - no space)
    ($Shunt:ident, $Trait:ident, &&$lt:lifetime $T:ident) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &&$lt $T>>
    };
    // Double mutable reference
    ($Shunt:ident, $Trait:ident, & &$lt:lifetime mut $T:ident) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = & &$lt mut $T>>
    };
    // Double mutable reference (no space)
    ($Shunt:ident, $Trait:ident, &&$lt:lifetime mut $T:ident) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &&$lt mut $T>>
    };
    // Reference to tuple (variadic)
    ($Shunt:ident, $Trait:ident, &$lt:lifetime ($($T:ident),+)) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt ($($T),+)>>
    };
    // Mutable reference to tuple (variadic)
    ($Shunt:ident, $Trait:ident, &$lt:lifetime mut ($($T:ident),+)) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = &$lt mut ($($T),+)>>
    };
    // Tuple (variadic)
    ($Shunt:ident, $Trait:ident, ($($T:ident),+)) => {
        $crate::$Shunt<dyn for<'lend> $crate::$Trait<'lend, Lend = ($($T),+)>>
    };
}

/// Uses lifetime `'lend` within type `$T` to create an `impl for<'lend>
/// Lending<'lend, Lend = $T>`.
///
/// Uses a known borrow-checker bug which allows `dyn` objects to implement
/// impossible traits.
///
/// This macro only accepts type patterns that are guaranteed to be
/// covariant in `'lend`:
/// - Identifiers: `i32`, `String`, etc.
/// - References: `&'lend T`, `&'lend mut T`
/// - String slices: `&'lend str`, `&'lend mut str`
/// - Slices: `&'lend [T]`, `&'lend mut [T]`
/// - Vec: `Vec<T>`, `&'lend Vec<T>`, `&'lend mut Vec<T>`
/// - Double references: `& &'lend T`, `&&'lend T`
/// - Tuples of identifiers: `(T0,)`, `(T0, T1)`, `(T0, T1, T2)`, etc.
///   (any number of elements)
/// - References to tuples: `&'lend (T0,)`, `&'lend (T0, T1)`,
///   `&'lend mut (T0,)`, `&'lend mut (T0, T1)`, etc.
///
/// For types that are not covered by this macro, please use
/// [`covariant_lend!`](crate::covariant_lend!), which performs a compile-time
/// covariance check.
///
/// # Examples
/// ```rust
/// # use lender::prelude::*;
/// let mut empty = lender::empty::<lend!(&'lend mut [i32])>();
/// let _: Option<&mut [i32]> = empty.next(); // => None
/// ```
#[macro_export]
macro_rules! lend {
    // Identifier
    ($T:ident) => { $crate::__lend_impl!(DynLendShunt, DynLend, $T) };
    // Reference patterns
    (&$lt:lifetime $T:ident) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt $T) };
    (&$lt:lifetime mut $T:ident) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt mut $T) };
    // String slices
    (&$lt:lifetime str) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt str) };
    (&$lt:lifetime mut str) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt mut str) };
    // Slices
    (&$lt:lifetime [$T:ident]) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt [$T]) };
    (&$lt:lifetime mut [$T:ident]) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt mut [$T]) };
    // Vec
    (Vec<$T:ident>) => { $crate::__lend_impl!(DynLendShunt, DynLend, Vec<$T>) };
    (&$lt:lifetime Vec<$T:ident>) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt Vec<$T>) };
    (&$lt:lifetime mut Vec<$T:ident>) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt mut Vec<$T>) };
    // Double references
    (& &$lt:lifetime $T:ident) => { $crate::__lend_impl!(DynLendShunt, DynLend, & &$lt $T) };
    (&&$lt:lifetime $T:ident) => { $crate::__lend_impl!(DynLendShunt, DynLend, &&$lt $T) };
    (& &$lt:lifetime mut $T:ident) => { $crate::__lend_impl!(DynLendShunt, DynLend, & &$lt mut $T) };
    (&&$lt:lifetime mut $T:ident) => { $crate::__lend_impl!(DynLendShunt, DynLend, &&$lt mut $T) };
    // References to tuples
    (&$lt:lifetime ($($T:ident),+ $(,)?)) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt ($($T),+)) };
    (&$lt:lifetime mut ($($T:ident),+ $(,)?)) => { $crate::__lend_impl!(DynLendShunt, DynLend, &$lt mut ($($T),+)) };
    // Tuples
    (($($T:ident),+ $(,)?)) => { $crate::__lend_impl!(DynLendShunt, DynLend, ($($T),+)) };
    // Fallback - reject with helpful error
    ($($tt:tt)*) => {
        compile_error!(concat!(
            "lend!() only accepts simple covariant patterns. ",
            "For complex types, use covariant_lend!(Name = YourType) instead."
        ))
    };
}

/// Implement the covariance check method for a [`Lender`] impl with a concrete
/// [`Lend`](Lending::Lend) type.
///
/// This macro must be invoked inside `impl Lender for T` blocks where the
/// [`Lend`](Lending::Lend) type is concrete (not defined in terms of another
/// lender's [`Lend`](Lending::Lend) type). It expands to the
/// `__check_covariance` method implementation with body `{ proof }`, which
/// only compiles if the [`Lend`](Lending::Lend) type is covariant in its
/// lifetime.
///
/// For adapters that delegate to underlying lenders, use
/// [`unsafe_assume_covariance!`](crate::unsafe_assume_covariance!) instead.
///
/// # Examples
/// ```rust
/// # use lender::prelude::*;
/// struct RefLender<'a, T>(&'a [T], usize);
///
/// impl<'lend, T> Lending<'lend> for RefLender<'_, T> {
///     type Lend = &'lend T;  // Concrete covariant type
/// }
///
/// impl<T> Lender for RefLender<'_, T> {
///     check_covariance!();
///
///     fn next(&mut self) -> Option<Lend<'_, Self>> {
///         if self.1 < self.0.len() {
///             let i = self.1;
///             self.1 += 1;
///             Some(&self.0[i])
///         } else {
///             None
///         }
///     }
/// }
/// ```
#[macro_export]
macro_rules! check_covariance {
    () => {
        fn __check_covariance<'long: 'short, 'short>(
            proof: $crate::CovariantProof<<Self as $crate::Lending<'long>>::Lend>,
        ) -> $crate::CovariantProof<<Self as $crate::Lending<'short>>::Lend> {
            proof
        }
    };
}

/// Skips the covariance check for [`Lender`] impls.
///
/// Use this macro for adapters whose [`Lend`](Lending::Lend) type is defined in
/// terms of another lender's [`Lend`](Lending::Lend) type (e.g., `type Lend =
/// Lend<'lend, L>`). The macro expands to the `__check_covariance` method
/// implementation with body `{ unsafe { core::mem::transmute(proof) } }`,
/// which skips the covariance check, as it always compiles.
///
/// For lenders with concrete [`Lend`](Lending::Lend) types, use
/// [`check_covariance!`] instead.
///
/// # Examples
///
/// ```rust,ignore
/// impl<L: Lender> Lender for MyAdapter<L> {
///     // SAFETY: the lend is that of L
///     unsafe_assume_covariance!();
///     // ...
/// }
/// ```
///
/// # Safety
///
/// This macro disables the covariance check using unsafe code. It is the
/// caller's responsibility to ensure that the adapter's [`Lend`](Lending::Lend)
/// type is indeed covariant in its lifetime. Please write a brief SAFETY
/// comment explaining why covariance holds, as you would for any other unsafe
/// code.
#[macro_export]
macro_rules! unsafe_assume_covariance {
    () => {
        fn __check_covariance<'long: 'short, 'short>(
            proof: $crate::CovariantProof<<Self as $crate::Lending<'long>>::Lend>,
        ) -> $crate::CovariantProof<<Self as $crate::Lending<'short>>::Lend> {
            // SAFETY: Covariance is assumed by the caller of this macro
            unsafe { core::mem::transmute(proof) }
        }
    };
}

/// Defines a lending type with compile-time covariance checking.
///
/// This macro creates a struct that implements [`Lending`] and includes a
/// compile-time assertion that the lend type is covariant in its lifetime.
///
/// An optional visibility modifier can be provided to make the type reusable
/// across modules.
///
/// # Examples
/// ```rust
/// # use lender::prelude::*;
/// // Define a covariance-checked lending type for &'lend Vec<i32>.
/// // This type cannot be expressed with lend!() because Vec<i32>
/// // has generics.
/// covariant_lend!(RefVec = &'lend Vec<i32>);
///
/// let data = [vec![1, 2], vec![3, 4]];
/// let mut lender = lender::lend_iter::<'_, RefVec, _>(data.iter());
/// let item: &Vec<i32> = lender.next().unwrap();
/// assert_eq!(item, &vec![1, 2]);
/// ```
///
/// With visibility modifier:
/// ```rust
/// # use lender::prelude::*;
/// // Define a public covariance-checked lending type.
/// covariant_lend!(pub MyLend = &'lend Vec<i32>);
///
/// // MyLend can be used outside the module.
/// let data = [vec![1, 2]];
/// let mut lender = lender::lend_iter::<'_, MyLend, _>(data.iter());
/// assert_eq!(lender.next(), Some(&vec![1, 2]));
/// ```
///
/// # Compile-time Error for Invariant Types
///
/// The following will fail to compile because
/// `Cell<Option<&'lend String>>` is invariant in `'lend`:
///
/// ```rust,compile_fail
/// # use lender::prelude::*;
/// # use std::cell::Cell;
/// // This fails to compile - Cell makes the type invariant!
/// covariant_lend!(
///     InvariantLend = &'lend Cell<Option<&'lend String>>
/// );
/// ```
#[macro_export]
macro_rules! covariant_lend {
    ($vis:vis $name:ident = $T:ty) => {
        /// A lending type with compile-time covariance checking.
        #[derive(Clone, Copy, Debug, Default)]
        $vis struct $name;

        impl<'lend> $crate::Lending<'lend> for $name {
            type Lend = $T;
        }

        // Covariance is enforced by the required _check_covariance method:
        // its body `{ proof }` only compiles if $T is covariant in 'lend.
        impl $crate::CovariantLending for $name {
            fn __check_covariance<'long: 'short, 'short>(
                proof: $crate::CovariantProof<<Self as $crate::Lending<'long>>::Lend>,
            ) -> $crate::CovariantProof<<Self as $crate::Lending<'short>>::Lend> {
                proof
            }
        }
    };
}

/// Defines a fallible lending type with compile-time covariance checking.
///
/// This macro creates a struct that implements [`FallibleLending`] and
/// includes a compile-time assertion that the lend type is covariant in its
/// lifetime.
///
/// An optional visibility modifier can be provided to make the type reusable
/// across modules.
///
/// # Examples
/// ```rust
/// # use lender::prelude::*;
/// # use fallible_iterator::IteratorExt;
/// // Define a covariance-checked fallible lending type for
/// // Option<&'lend str>. This type cannot be expressed with
/// // fallible_lend!() because Option has generics.
/// covariant_fallible_lend!(OptStr = Option<&'lend str>);
///
/// let data = [Some("hello"), None, Some("world")];
/// let mut lender = lender::lend_fallible_iter::<'_, OptStr, _>(
///     data.iter().copied().into_fallible()
/// );
/// let item: Option<&str> = lender.next().unwrap().unwrap();
/// assert_eq!(item, Some("hello"));
/// ```
///
/// With visibility modifier:
/// ```rust
/// # use lender::prelude::*;
/// # use fallible_iterator::IteratorExt;
/// // Define a public covariance-checked fallible lending type.
/// covariant_fallible_lend!(pub MyLend = Option<&'lend str>);
///
/// // MyLend can be used outside the module.
/// let data = [Some("hello")];
/// let mut lender = lender::lend_fallible_iter::<'_, MyLend, _>(
///     data.iter().copied().into_fallible()
/// );
/// assert_eq!(lender.next().unwrap(), Some(Some("hello")));
/// ```
///
/// # Compile-time Error for Invariant Types
///
/// The following will fail to compile because
/// `Cell<Option<&'lend String>>` is invariant in `'lend`:
///
/// ```rust,compile_fail
/// # use lender::prelude::*;
/// # use std::cell::Cell;
/// // This fails to compile - Cell makes the type invariant!
/// covariant_fallible_lend!(
///     InvariantLend = &'lend Cell<Option<&'lend String>>
/// );
/// ```
#[macro_export]
macro_rules! covariant_fallible_lend {
    ($vis:vis $name:ident = $T:ty) => {
        /// A fallible lending type with compile-time covariance checking.
        #[derive(Clone, Copy, Debug, Default)]
        $vis struct $name;

        impl<'lend> $crate::FallibleLending<'lend> for $name {
            type Lend = $T;
        }

        // Covariance is enforced by the required _check_covariance method:
        // its body `{ proof }` only compiles if $T is covariant in 'lend.
        impl $crate::CovariantFallibleLending for $name {
            fn __check_covariance<'long: 'short, 'short>(
                proof: $crate::CovariantProof<&'short <Self as $crate::FallibleLending<'long>>::Lend>,
            ) -> $crate::CovariantProof<&'short <Self as $crate::FallibleLending<'short>>::Lend> {
                proof
            }
        }
    };
}

/// Implement the covariance check method for a [`FallibleLender`] impl with a
/// concrete [`Lend`](FallibleLending::Lend) type.
///
/// See [`check_covariance!`](crate::check_covariance!) for details.
#[macro_export]
macro_rules! check_covariance_fallible {
    () => {
        fn __check_covariance<'long: 'short, 'short>(
            proof: $crate::CovariantProof<&'short <Self as $crate::FallibleLending<'long>>::Lend>,
        ) -> $crate::CovariantProof<&'short <Self as $crate::FallibleLending<'short>>::Lend> {
            proof
        }
    };
}

/// Skips the covariance check for [`FallibleLender`] impls.
///
/// This is the fallible counterpart to [`unsafe_assume_covariance!`].
///
/// See [`unsafe_assume_covariance!`](crate::unsafe_assume_covariance!) for
/// more details.
#[macro_export]
macro_rules! unsafe_assume_covariance_fallible {
    () => {
        fn __check_covariance<'long: 'short, 'short>(
            proof: $crate::CovariantProof<&'short <Self as $crate::FallibleLending<'long>>::Lend>,
        ) -> $crate::CovariantProof<&'short <Self as $crate::FallibleLending<'short>>::Lend> {
            // SAFETY: Covariance is assumed by the caller of this macro
            unsafe { core::mem::transmute(proof) }
        }
    };
}

/// Internal struct used to implement [`fallible_lend!`], do not use directly.
#[doc(hidden)]
pub struct DynFallibleLendShunt<T: ?Sized>(pub T);

impl<'lend, T: ?Sized + for<'all> DynFallibleLend<'all>> FallibleLending<'lend>
    for DynFallibleLendShunt<T>
{
    type Lend = <T as DynFallibleLend<'lend>>::Lend;
}

/// Fallible counterpart to [`CovariantLending`].
///
/// See [`CovariantLending`] for details on the covariance enforcement mechanism.
#[doc(hidden)]
pub trait CovariantFallibleLending: for<'all> FallibleLending<'all> {
    fn __check_covariance<'long: 'short, 'short>(
        proof: CovariantProof<&'short <Self as FallibleLending<'long>>::Lend>,
    ) -> CovariantProof<&'short <Self as FallibleLending<'short>>::Lend>;
}

// SAFETY: fallible_lend!() only accepts type patterns that are syntactically
// covariant in 'lend
impl<T: ?Sized + for<'all> DynFallibleLend<'all>> CovariantFallibleLending
    for DynFallibleLendShunt<T>
{
    fn __check_covariance<'long: 'short, 'short>(
        proof: CovariantProof<&'short <Self as FallibleLending<'long>>::Lend>,
    ) -> CovariantProof<&'short <Self as FallibleLending<'short>>::Lend> {
        // SAFETY: fallible_lend!() only accepts syntactically covariant patterns
        unsafe { core::mem::transmute(proof) }
    }
}

/// Internal trait used to implement [`fallible_lend!`], do not use directly.
#[doc(hidden)]
pub trait DynFallibleLend<'lend> {
    type Lend;
}

/// Uses lifetime `'lend` within type `$T` to create an `impl for<'lend>
/// FallibleLending<'lend, Lend = $T>`.
///
/// Uses a known borrow-checker bug which allows `dyn` objects to implement
/// impossible traits.
///
/// This macro only accepts type patterns that are guaranteed to be
/// covariant in `'lend`:
/// - Identifiers: `i32`, `String`, etc.
/// - References: `&'lend T`, `&'lend mut T`
/// - String slices: `&'lend str`, `&'lend mut str`
/// - Slices: `&'lend [T]`, `&'lend mut [T]`
/// - Vec: `Vec<T>`, `&'lend Vec<T>`, `&'lend mut Vec<T>`
/// - Double references: `& &'lend T`, `&&'lend T`
/// - Tuples of identifiers: `(T0,)`, `(T0, T1)`, `(T0, T1, T2)`, etc.
///   (any number of elements)
/// - References to tuples: `&'lend (T0,)`, `&'lend (T0, T1)`,
///   `&'lend mut (T0,)`, `&'lend mut (T0, T1)`, etc.
///
/// For types that are not covered by this macro, please use
/// [`covariant_fallible_lend!`](crate::covariant_fallible_lend!), which
/// performs a compile-time covariance check.
///
/// # Examples
/// ```rust
/// # use lender::prelude::*;
/// # use std::convert::Infallible;
/// let mut empty = lender::fallible_empty::<
///     fallible_lend!(&'lend mut [i32]),
///     Infallible
/// >();
/// let _: Result<Option<&mut [i32]>, Infallible> = empty.next(); // => Ok(None)
/// ```
#[macro_export]
macro_rules! fallible_lend {
    // Identifier
    ($T:ident) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, $T) };
    // Reference patterns
    (&$lt:lifetime $T:ident) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt $T) };
    (&$lt:lifetime mut $T:ident) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt mut $T) };
    // String slices
    (&$lt:lifetime str) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt str) };
    (&$lt:lifetime mut str) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt mut str) };
    // Slices
    (&$lt:lifetime [$T:ident]) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt [$T]) };
    (&$lt:lifetime mut [$T:ident]) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt mut [$T]) };
    // Vec
    (Vec<$T:ident>) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, Vec<$T>) };
    (&$lt:lifetime Vec<$T:ident>) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt Vec<$T>) };
    (&$lt:lifetime mut Vec<$T:ident>) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt mut Vec<$T>) };
    // Double references
    (& &$lt:lifetime $T:ident) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, & &$lt $T) };
    (&&$lt:lifetime $T:ident) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &&$lt $T) };
    (& &$lt:lifetime mut $T:ident) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, & &$lt mut $T) };
    (&&$lt:lifetime mut $T:ident) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &&$lt mut $T) };
    // References to tuples
    (&$lt:lifetime ($($T:ident),+ $(,)?)) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt ($($T),+)) };
    (&$lt:lifetime mut ($($T:ident),+ $(,)?)) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, &$lt mut ($($T),+)) };
    // Tuples
    (($($T:ident),+ $(,)?)) => { $crate::__lend_impl!(DynFallibleLendShunt, DynFallibleLend, ($($T),+)) };
    // Fallback - reject with helpful error
    ($($tt:tt)*) => {
        compile_error!(concat!(
            "fallible_lend!() only accepts simple covariant patterns. ",
            "For complex types, use covariant_fallible_lend!(Name = YourType) instead."
        ))
    };
}