tuples/
capt.rs

1//! Manually capture the variables required by the closure
2
3/// Manually capture the variables required by the closure
4pub fn capt_0<T, R, F: Fn(&T) -> R>(v: T, f: F) -> impl Fn() -> R {
5    move || f(&v)
6}
7
8/// Manually capture the variables required by the closure
9pub fn capt_mut_0<T, R, F: FnMut(&mut T) -> R>(mut v: T, mut f: F) -> impl FnMut() -> R {
10    move || f(&mut v)
11}
12
13/// Manually capture the variables required by the closure
14pub fn capt_once_0<T, R, F: FnOnce(T) -> R>(v: T, f: F) -> impl FnOnce() -> R {
15    move || f(v)
16}
17
18include!("./gen/capt.rs");
19
20#[test]
21fn test1() {
22    let a = capt_0(1, |a| *a);
23    let r = a();
24    assert_eq!(r, 1);
25}
26
27#[test]
28fn test2() {
29    let a = capt_1(2, |a, b| *a * b);
30    let r = a(3);
31    assert_eq!(r, 6);
32}
33
34#[test]
35fn test3() {
36    let mut a = capt_mut_1(2, |a, b| {
37        *a *= b;
38        *a
39    });
40    let r = a(3);
41    assert_eq!(r, 6);
42    let r = a(3);
43    assert_eq!(r, 18);
44}