SQL parameter values


#1

Hello,
I am trying to solve a rusqlite issue.
Currently, rusqlite uses params: &[&ToSql] (for example, execute).
Like rust-postgres which has the same issue and a solution.

The solution works fine except when there is no parameter:
You can’t use:

db.execute("INSERT INTO foo DEFAULT VALUES", &[]);

anymore, you have to specify the type:

db.prepare("INSERT INTO foo DEFAULT VALUES", &[] as &[&ToSql]);

Do you have any suggestion ?
Here is an example that illustrates the problem with no dependency on rusqlite:

fn collect_as_strings<T>(collection: T) -> Vec<String>
    where T: IntoIterator,
          T::Item : std::fmt::Debug,
{
    collection
        .into_iter()
        .map(|item| format!("{:?}", item))
        .collect()
}

fn main() {
    //let data = vec![1, 2];
    let data = vec![];
    let strings = collect_as_strings(data);
    println!("{:?}", strings)
}

(Playground)

Errors:

   Compiling playground v0.0.1 (file:///playground)
error[E0282]: type annotations needed
  --> src/main.rs:13:16
   |
13 |     let data = vec![];
   |         ----   ^^^^^^ cannot infer type for `T`
   |         |
   |         consider giving `data` a type
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Thanks.


#2

There’s really no way around this. If you don’t give the compiler enough information to infer the type, it can’t infer the type, and it cannot have values where it doesn’t know the type. The solution is to specify the type. Every solution to this eventually boils down to: you have to specify the type.

In your specific, original case, you could define something like const NO_ARGS: &'static [&'static ToSql] = &[]; and use NO_ARGS instead.


#3

Thanks.
I am going to introduce a constant.