如何创建一个宏,它接受一个带有多个参数的函数并为该函数提供第一个参数?
How do I create a macro that takes a function with multiple parameters and supplies the first argument for that function?
我希望能够创建一个接受函数(称为 f
)的高阶函数(称为 g
)。 g
应将第一个参数传递给 f
和 return 一个新函数。
用例是我想在g
中启动一个数据库连接,并向它传递接受数据库连接的函数。
fn f1(a: i32, b: String) -> String {
b
}
fn f2(a: i32, c: i64, d: i16) -> i32 {
1000
}
fn g<T>(f: fn(a: i32, arbitrary_arguments_type) -> T) -> fn(arbitrary_arguments_type) -> T {
move |arbitrary_arguments| f(1, arbitrary_arguments)
}
fn main() {
g(f1)("hello".to_string());
g(f2)(10, 11);
}
如何创建一个宏,将具有多个参数的函数作为参数,其中第一个参数属于特定类型,并为第一个函数提供该参数?
The specific question I'm having is how do I create a macro that takes in as an argument a function with a more than 1 parameter, where first parameter is of a certain type, supplies that argument for that first function.
宏(甚至过程宏)在语法树上运行,因此它们无法根据语义更改其行为,包括类型和函数数量。这意味着您必须为每个可能的参数数量设置不同的宏。例如:
macro_rules! curry1 {
($func: ident, $($arg: expr),*) => {
|a| $func($($arg),*, a)
}
}
macro_rules! curry2 {
($func: ident, $($arg: expr),*) => {
|a, b| $func($($arg),*, a, b)
}
}
macro_rules! curry3 {
($func: ident, $($arg: expr),*) => {
|a, b, c| $func($($arg),*, a, b, c)
}
}
可以这样使用:
fn f(a: i32, b: i32, c: i32) -> i32 {
a + b + c
}
fn main() {
// requires 2 extra args
let f_2 = curry2!(f, 2);
// requires 1 extra arg
let f_2_1 = curry1!(f, 2, 1);
println!("{}", f(2, 1, 3)); // 6
println!("{}", f_2(1, 3)); // 6
println!("{}", f_2_1(3)); // 6
}
我希望能够创建一个接受函数(称为 f
)的高阶函数(称为 g
)。 g
应将第一个参数传递给 f
和 return 一个新函数。
用例是我想在g
中启动一个数据库连接,并向它传递接受数据库连接的函数。
fn f1(a: i32, b: String) -> String {
b
}
fn f2(a: i32, c: i64, d: i16) -> i32 {
1000
}
fn g<T>(f: fn(a: i32, arbitrary_arguments_type) -> T) -> fn(arbitrary_arguments_type) -> T {
move |arbitrary_arguments| f(1, arbitrary_arguments)
}
fn main() {
g(f1)("hello".to_string());
g(f2)(10, 11);
}
如何创建一个宏,将具有多个参数的函数作为参数,其中第一个参数属于特定类型,并为第一个函数提供该参数?
The specific question I'm having is how do I create a macro that takes in as an argument a function with a more than 1 parameter, where first parameter is of a certain type, supplies that argument for that first function.
宏(甚至过程宏)在语法树上运行,因此它们无法根据语义更改其行为,包括类型和函数数量。这意味着您必须为每个可能的参数数量设置不同的宏。例如:
macro_rules! curry1 {
($func: ident, $($arg: expr),*) => {
|a| $func($($arg),*, a)
}
}
macro_rules! curry2 {
($func: ident, $($arg: expr),*) => {
|a, b| $func($($arg),*, a, b)
}
}
macro_rules! curry3 {
($func: ident, $($arg: expr),*) => {
|a, b, c| $func($($arg),*, a, b, c)
}
}
可以这样使用:
fn f(a: i32, b: i32, c: i32) -> i32 {
a + b + c
}
fn main() {
// requires 2 extra args
let f_2 = curry2!(f, 2);
// requires 1 extra arg
let f_2_1 = curry1!(f, 2, 1);
println!("{}", f(2, 1, 3)); // 6
println!("{}", f_2(1, 3)); // 6
println!("{}", f_2_1(3)); // 6
}