Skip to content

Can't use &dyn ToSql with Async/Await syntax with tokio-postgres #445

Closed
@kaibyao

Description

@kaibyao

See https://github.com/kaibyao/tokio-postgres-tosql-multithread-issue for a reference implementation of this problem.

// example endpoint using actix_web 1.0
async fn index() -> Result<HttpResponse, Error> {
    // ...
    let prepared_values: Vec<&dyn ToSql> = vec![&true];
    let rows = client.query(&statement, &prepared_values).collect().compat().await;
    let rows: Vec<i32> = rows.unwrap().into_iter().map(|row| row.get(0)).collect();

    Ok(HttpResponse::build(StatusCode::OK).json(rows))
}

// in another thread
fn returns_future() -> impl Future01<Item = HttpResponse, Error = Error> {
    index().boxed().compat()
    //      ^^^^^^^  `dyn tokio_postgres::types::ToSql` cannot be shared between threads safely
}

What's the best way to pass Vec<&dyn ToSql> (for prepared statements) in multi-threaded environments?

The following don't work:

  • Using &(dyn ToSql + Send) instead of &dyn ToSql
  • Creating a "wrapper" trait ToSqlSend that does have Send and ToSql and changing &dyn ToSql to &dyn ToSqlSend.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions