0

Seeking your support which I have illogical mismatched types in our project.

Project built by docker compose up. Actually images working fine from 3-4 months without issues; but now it shows error as illustrated below.

Sample Error (Note it is happened many times):

error[E0308]: mismatched types
   --> db/src/pg/user_profiles.rs:158:27
    |
158 |             &[&is_worker, &*user_id],
    |                           ^^^^^^^^^ expected `bool`, found `i64`
    |
    = note: expected reference `&bool`
               found reference `&i64`

Code snippet for this error as below:

pub struct UserId(pub i64);
pub async fn update_is_worker(user_id: UserId, is_worker: bool) -> Result<()> {
    let client = PG_POOL.get().await?;
    let stmt = client
        .prepare_typed(
            "\
        UPDATE user_profiles \
        SET \
            is_worker = $1 \
        WHERE user_id = $2;\
        ",
            &[Type::BOOL, Type::INT8],
        )
        .await?;

    client.execute(&stmt, &[&is_worker, &*user_id]).await?;

    Ok(())
}
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • But the line with the error, is not the same as the line you include in the example. The one with the error has a trailing comma, while that in your snippet does not. – rodrigo Sep 20 '22 at 15:30
  • 2
    Please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – kmdreko Sep 20 '22 at 15:35
  • It's not clear what you find illogical in that error. Arrays are homogeneous in Rust, so you can't have mixed types. You should look at the documentation or some examples of the `execute` method, to find out what is the proper way to pass arguments. – jthulhu Sep 20 '22 at 16:10
  • Reading this [answer](https://stackoverflow.com/a/71684954/865874), maybe just adding `as &(dyn ToSql + Sync)` to the first argument of the array should be enough, but currently I'm unable to verify it. – rodrigo Sep 20 '22 at 16:37
  • 1
    If its passed directly to `execute` then it should automatically coerce the elements to `&dyn ToSql`. (are we even sure this is tokio-postgres? a lot of these libraries look the same) but if the list is defined separately then yeah you'd probably need an `as` like @rodrigo suggests. – kmdreko Sep 20 '22 at 17:03
  • @kmdreko: Indeed! I'm never sure of when the compile can automate these things. I think [this playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=419f1eb525a77378fbfa116e7b7e1f10) would be a minimal reproducible example of the OP issue, assuming that the error is not in the quoted snippet. – rodrigo Sep 20 '22 at 20:05

1 Answers1

0

I got a solution for this in the Rust server @ Discord from skeletizzle which he asked me to edit line as below: client.execute(&stmt, &[&is_worker as _, &*user_id as _]).await?;

I have a lot of these errors, so I did change similar lines with same error with same concept.