-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
759 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"rust-analyzer.linkedProjects": [ | ||
".\\tuples\\Cargo.toml", | ||
".\\tuples\\Cargo.toml", | ||
".\\tuples\\Cargo.toml" | ||
], | ||
"rust-analyzer.showUnlinkedFileNotification": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,44 @@ | ||
//! Manually capture the variables required by the closure | ||
/// Manually capture the variables required by the closure | ||
pub fn capt0<T, R, F: Fn(&T) -> R>(v: T, f: F) -> impl Fn() -> R { | ||
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 capt1<T, A, R, F: Fn(&T, A) -> R>(v: T, f: F) -> impl Fn(A) -> R { | ||
move |a| f(&v, a) | ||
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); | ||
} |
Oops, something went wrong.