Skip to main content

covar_mut

Macro covar_mut 

Source
macro_rules! covar_mut {
    ($($t:tt)+) => { ... };
}
Expand description

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, Lender::for_each, Lender::filter_map, and Lender::scan.

§Syntax

covar_mut!(for<'a> |arg: Type<'a>| -> ReturnType<'a> { body })
covar_mut!(for<'a> move |arg: Type<'a>| -> ReturnType<'a> { body })

§Examples

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));
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]);