兩個堅硬的東西:計數和獨特identifers。你有兩個。然後再次,我是寫答案的人,所以我想現在是我的問題。至少你沒有問過解析字符串(如果沒有編譯器插件,這是完全不可能的)。
另一個不可能的事情是將類型映射到不同的方法。你不能。相反,我會假設存在一個幫助特徵來完成這種映射。
而且,鏽沒有int
,string
,或float
。我假設你的意思是i32
,String
和f32
。
最後,您編寫調用和擴展的方式並不真正凝聚。我不明白爲什麼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);
}
請刪除您的第二個問題,關於「這是可能的」,並且[問一個單獨的問題] (http://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts)。 – Shepmaster