Skip to content

Conversation

@dyoshikawa
Copy link
Contributor

@dyoshikawa dyoshikawa commented May 28, 2020

This .await? in the example code looks unnecessary.

Example

$ rustc -V
rustc 1.43.0 (4fb7144ed 2020-04-20)
# Cargo.toml
[dependencies]
tokio = { version = "0.2", features = ["full"] }
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros", "mysql" ] }
// main.rs
use sqlx::mysql::MySqlPool;
use sqlx::Cursor;

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    // Create a connection pool
    let pool = MySqlPool::new("mysql://root:secret@localhost:3306/foo").await.unwrap();

    let email = "[email protected]";
    // Make a simple query to return the given parameter
    let mut cursor = sqlx::query("SELECT * FROM users WHERE email = ?")
        .bind(email)
        .fetch(&pool).await.unwrap();

    while let Some(row) = cursor.next().await? {
        // map the row into a user-defined domain type
    }

    Ok(())
}

Compilation Error

error[E0277]: the trait bound `sqlx_core::mysql::cursor::MySqlCursor<'_, '_>: std::future::Future` is not satisfied
  --> src/main.rs:11:22
   |
11 |       let mut cursor = sqlx::query("SELECT * FROM users WHERE email = ?")
   |  ______________________^
12 | |         .bind(email)
13 | |         .fetch(&pool).await.unwrap();
   | |___________________________^ the trait `std::future::Future` is not implemented for `sqlx_core::mysql::cursor::MySqlCursor<'_, '_>`

@mehcode mehcode merged commit 086dfec into launchbadge:master May 30, 2020
@mehcode
Copy link
Member

mehcode commented May 30, 2020

Good catch. fetch returns a stream which can't be awaited directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants