生成具有由宏确定的参数的函数的宏

时间:2015-10-16 14:41:31

标签: macros rust

是否可以编写一个生成函数的宏,其中该函数的参数个数由宏决定?例如,我想在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()
}

1 个答案:

答案 0 :(得分:8)

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

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

此外,Rust没有intstring float。我假设您的意思是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);
}