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
//! Manually capture the variables required by the closure

/// Manually capture the variables required by the closure
pub fn capt_0<T, R, F: Fn(&T) -> R>(v: T, f: F) -> impl Fn() -> R {
    move || f(&v)
}

/// Manually capture the variables required by the closure
pub fn capt_mut_0<T, R, F: FnMut(&mut T) -> R>(mut v: T, mut f: F) -> impl FnMut() -> R {
    move || f(&mut v)
}

/// Manually capture the variables required by the closure
pub fn capt_once_0<T, R, F: FnOnce(T) -> R>(v: T, f: F) -> impl FnOnce() -> R {
    move || f(v)
}

include!("./gen/capt.rs");

#[test]
fn test1() {
    let a = capt_0(1, |a| *a);
    let r = a();
    assert_eq!(r, 1);
}

#[test]
fn test2() {
    let a = capt_1(2, |a, b| *a * b);
    let r = a(3);
    assert_eq!(r, 6);
}

#[test]
fn test3() {
    let mut a = capt_mut_1(2, |a, b| {
        *a *= b;
        *a
    });
    let r = a(3);
    assert_eq!(r, 6);
    let r = a(3);
    assert_eq!(r, 18);
}