0

Would like to make this function generic but having trouble specifying the bounds for parameter T

Errors : 1)'cannot find type DB in this scope not found in this scope'

Not sure if I was supposed to include ::bind either. I got that from rust-analyzer


pub async fn set_db_value<
    'q,
    T: std::marker::Sync
        + std::marker::Send
        + sqlx::query::Query<'q, DB, <DB as HasArguments<'q>>::Arguments>::bind,
>(
    key: &str,
    b: T,
) {
    let statement = format!("update preferences set {} = $1  where key = 'p'", &key);
    sqlx::query(&statement)
        .bind(&b)
        .execute(&pg)
        .await
        .unwrap();
}

Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97

1 Answers1

0

Assuming pg is of type sqlx::Pool<sqlx::Postgres>, here are the correct bounds:

pub async fn set_db_value<T>(key: &str, b: T)
where
    T: Sync
        + Send
        + for<'q> sqlx::Encode<'q, sqlx::Postgres>
        + sqlx::Type<sqlx::Postgres>,
{
    let statement = format!("update preferences set {} = $1  where key = 'p'", &key);
    sqlx::query(&statement).bind(&b).execute(&pg).await.unwrap();
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77