Open NyxCode opened 3 years ago
I see that this error is raised because a local value here is borrowed. The local value itself is borrowed from the argument bindings. This means that even if a reference with static lifetime is used for argument binding, we will still see an error.
let arg_bindings = quote! {
#(let #arg_name = &(#arg_expr);)*
};
IMHO, passing by reference should be the library users responsibility. But we can't break this syntax now. Maybe this should be part of #875.
I have a more backward compatible solution in mind. Would it make sense to allow prepending &
before the argument to signal that its already a reference? So if arg_expr
is already a reference then we don't prepend &
. This would allow the following code to work
fn query<'a, 'c: 'a>(
con: impl Executor<'c, Database = Sqlite> + 'a,
limit: &'x i64,
offset: &'x i64,
) -> BoxStream<'x, sqlx::Result<i64>>
where
'a: 'x
{
sqlx::query_scalar!("SELECT id FROM users LIMIT ? OFFSET ?",
&limit, // <---- `&` here signals this is already a reference
&offset, // <---- `&` here signals this is already a reference
).fetch(con)
}
@abonander I think this is simple enough and I can give a PR. Do you think its worth spending time on this?
I just ran into this as well, is there any way to work around this?
I've struck a strange issue with sqlite. This code
fails to compile with sqlite with this error:
The same happens with
query!
andquery_as!
. The equivalent code compiles fine on MySql and Postgres. It's only on sqlite that the stream borrows the parameters.