Skip to main content

lend

Macro lend 

Source
macro_rules! lend {
    ($T:ident) => { ... };
    (&$lt:lifetime $T:ident) => { ... };
    (&$lt:lifetime mut $T:ident) => { ... };
    (&$lt:lifetime str) => { ... };
    (&$lt:lifetime mut str) => { ... };
    (&$lt:lifetime [$T:ident]) => { ... };
    (&$lt:lifetime mut [$T:ident]) => { ... };
    (Vec<$T:ident>) => { ... };
    (&$lt:lifetime Vec<$T:ident>) => { ... };
    (&$lt:lifetime mut Vec<$T:ident>) => { ... };
    (& &$lt:lifetime $T:ident) => { ... };
    (&&$lt:lifetime $T:ident) => { ... };
    (& &$lt:lifetime mut $T:ident) => { ... };
    (&&$lt:lifetime mut $T:ident) => { ... };
    (&$lt:lifetime ($($T:ident),+ $(,)?)) => { ... };
    (&$lt:lifetime mut ($($T:ident),+ $(,)?)) => { ... };
    (($($T:ident),+ $(,)?)) => { ... };
    ($($tt:tt)*) => { ... };
}
Expand description

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!, which performs a compile-time covariance check.

ยงExamples

let mut empty = lender::empty::<lend!(&'lend mut [i32])>();
let _: Option<&mut [i32]> = empty.next(); // => None