是否可以编写一个宏来生成一个函数,其中该函数的参数数量由宏确定?例如,我想写一些东西来使在 Cassandra 驱动程序中使用准备好的语句更容易。

let prepared = prepare!(session, "insert into blah (id, name, reading ) values (?, ?, ?)", int, string, float);
let stmt = prepared(1, "test".to_string(), 3.1);
session.execute(stmt);
prepare! 需要生成类似的东西(为简洁起见,仅在此处展开​​):
fn some_func(arg1, arg2, arg3) -> Statement {
    let mut statement = Statement::new("insert into blah (id, name, reading ) values (?, ?, ?)", 3);
    statement.bind_int(0, arg1).unwrap()
        .bind_string(1, arg2).unwrap()
        .bind_float(2, arg3).unwrap()
}

最佳答案

Rust 宏中有两个难点:计数和唯一标识符。你两者都有。再说一次,我是写答案的人,所以我想现在是我的问题。至少您没有询问解析字符串(如果没有编译器插件,这是完全不可能的)。

另一个不可能的事情是将类型映射到不同的方法。你就是不能。相反,我将假设存在执行此映射的辅助特征。

此外,Rust 没有 intstringfloat 。我假设您的意思是 i32Stringf32

最后,您编写调用和扩展的方式并没有真正融合。我不明白为什么涉及 session;它不用于扩展。所以我冒昧地假装你不需要它;如果你这样做了,你将不得不重新破解它。

所以,有了这个,这就是我想出的。

// Some dummy types so the following will type-check.

struct Statement;

impl Statement {
    fn new(stmt: &str, args: usize) -> Self { Statement }
    fn bind_int(self, pos: usize, value: i32) -> Result<Self, ()> { Ok(self) }
    fn bind_float(self, pos: usize, value: f32) -> Result<Self, ()> { Ok(self) }
    fn bind_string(self, pos: usize, value: String) -> Result<Self, ()> { Ok(self) }
}

struct Session;

impl Session {
    fn execute(&self, stmt: Statement) {}
}

// The supporting `BindArgument` trait.

trait BindArgument {
    fn bind(stmt: Statement, pos: usize, value: Self) -> Statement;
}

impl BindArgument for i32 {
    fn bind(stmt: Statement, pos: usize, value: Self) -> Statement {
        stmt.bind_int(pos, value).unwrap()
    }
}

impl BindArgument for f32 {
    fn bind(stmt: Statement, pos: usize, value: Self) -> Statement {
        stmt.bind_float(pos, value).unwrap()
    }
}

impl BindArgument for String {
    fn bind(stmt: Statement, pos: usize, value: Self) -> Statement {
        stmt.bind_string(pos, value).unwrap()
    }
}

// The macro itself.

macro_rules! prepare {
    // These three are taken straight from
    // https://danielkeep.github.io/tlborm/book/
    (@as_expr $e:expr) => {$e};

    (@count_tts $($tts:tt)*) => {
        <[()]>::len(&[$(prepare!(@replace_tt $tts ())),*])
    };

    (@replace_tt $_tt:tt $e:expr) => {$e};

    // This is how we bind *one* argument.

    (@bind_arg $stmt:expr, $args:expr, $pos:tt, $t:ty) => {
        prepare!(@as_expr <$t as BindArgument>::bind($stmt, $pos, $args.$pos))
    };

    // This is how we bind *N* arguments.  Note that because you can't do
    // arithmetic in macros, we have to spell out every supported integer.
    // This could *maybe* be factored down with some more work, but that
    // can be homework.  ;)

    (@bind_args $stmt:expr, $args:expr, 0, $next:ty, $($tys:ty,)*) => {
        prepare!(@bind_args prepare!(@bind_arg $stmt, $args, 0, $next), $args, 1, $($tys,)*)
    };

    (@bind_args $stmt:expr, $args:expr, 1, $next:ty, $($tys:ty,)*) => {
        prepare!(@bind_args prepare!(@bind_arg $stmt, $args, 1, $next), $args, 2, $($tys,)*)
    };

    (@bind_args $stmt:expr, $args:expr, 2, $next:ty, $($tys:ty,)*) => {
        prepare!(@bind_args prepare!(@bind_arg $stmt, $args, 2, $next), $args, 3, $($tys,)*)
    };

    (@bind_args $stmt:expr, $_args:expr, $_pos:tt,) => {
        $stmt
    };

    // Finally, the entry point of the macro.

    ($stmt:expr, $($tys:ty),* $(,)*) => {
        {
            // I cheated: rather than face the horror of trying to *also* do
            // unique identifiers, I just shoved the arguments into a tuple, so
            // that I could just re-use the position.
            fn prepared_statement(args: ($($tys,)*)) -> Statement {
                let statement = Statement::new(
                    $stmt,
                    prepare!(@count_tts $(($tys))*));
                prepare!(@bind_args statement, args, 0, $($tys,)*)
            }
            prepared_statement
        }
    };
}

fn main() {
    let session = Session;
    let prepared = prepare!(
        r#"insert into blah (id, name, reading ) values (?, ?, ?)"#,
        i32, String, f32);
    // Don't use .to_string() for &str -> String; it's horribly inefficient.
    let stmt = prepared((1, "test".to_owned(), 3.1));
    session.execute(stmt);
}

下面是 main 函数扩展到的内容,为您提供一个引用框架:
fn main() {
    let session = Session;
    let prepared = {
        fn prepared_statement(args: (i32, String, f32)) -> Statement {
            let statement = Statement::new(
                r#"insert into blah (id, name, reading ) values (?, ?, ?)"#,
                <[()]>::len(&[(), (), ()]));
            <f32 as BindArgument>::bind(
                <String as BindArgument>::bind(
                    <i32 as BindArgument>::bind(
                        statement, 0, args.0),
                    1, args.1),
                2, args.2)
        }
        prepared_statement
    };
    // Don't use .to_string() for &str -> String; it's horribly inefficient.
    let stmt = prepared((1, "test".to_owned(), 3.1));
    session.execute(stmt);
}

关于macros - 生成具有由宏确定的参数的函数的宏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33173235/

10-13 04:46