diff --git a/bindings/rust/Cargo.toml b/bindings/rust/Cargo.toml index 557a12437..257123468 100644 --- a/bindings/rust/Cargo.toml +++ b/bindings/rust/Cargo.toml @@ -10,7 +10,7 @@ repository.workspace = true description = "Turso Rust API" [features] -default = [] +default = ["futures"] experimental_indexes = [] antithesis = ["turso_core/antithesis"] futures = ["dep:futures-util"] diff --git a/bindings/rust/examples/example.rs b/bindings/rust/examples/example.rs index f39204ba6..0c1fdbd79 100644 --- a/bindings/rust/examples/example.rs +++ b/bindings/rust/examples/example.rs @@ -1,5 +1,6 @@ -#[cfg(feature = "futures")] -use futures_util::stream::StreamExt; +#![cfg(feature = "futures")] + +use futures_util::stream::TryStreamExt; use turso::Builder; #[tokio::main] @@ -8,8 +9,15 @@ async fn main() { let conn = db.connect().unwrap(); - conn.query("select 1; select 1;", ()).await.unwrap(); + // `query` and other methods, only parse the first query given. + let mut rows = conn.query("select 1; select 1;", ()).await.unwrap(); + // Iterate over the rows with the Stream iterator syntax + while let Some(row) = rows.try_next().await.unwrap() { + let val = row.get_value(0).unwrap(); + println!("{:?}", val); + } + // Contrary to `prepare` and `query`, `execute` is not lazy and will execute the query to completion conn.execute("CREATE TABLE IF NOT EXISTS users (email TEXT)", ()) .await .unwrap(); @@ -34,9 +42,8 @@ async fn main() { let mut rows = stmt.query(["foo@example.com"]).await.unwrap(); - let row = rows.next().await.unwrap().unwrap(); - - let value = row.get_value(0).unwrap(); - - println!("Row: {:?}", value); + while let Some(row) = rows.try_next().await.unwrap() { + let value = row.get_value(0).unwrap(); + println!("Row: {:?}", value); + } } diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 5d5c56036..8ba309502 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -37,9 +37,10 @@ pub mod row; pub mod statement; pub mod value; -pub use value::Value; - +#[cfg(feature = "futures")] +pub use futures_util; pub use params::params_from_iter; +pub use value::Value; use crate::params::*; use crate::row::{Row, Rows}; @@ -65,7 +66,7 @@ impl From for Error { pub(crate) type BoxError = Box; -pub(crate) type Result = std::result::Result; +pub(crate) type Result = std::result::Result; /// A builder for `Database`. pub struct Builder { diff --git a/bindings/rust/src/row.rs b/bindings/rust/src/row.rs index 61d83f985..1f60d6d14 100644 --- a/bindings/rust/src/row.rs +++ b/bindings/rust/src/row.rs @@ -19,10 +19,11 @@ impl Clone for Rows { unsafe impl Send for Rows {} unsafe impl Sync for Rows {} +#[cfg(not(feature = "futures"))] impl Rows { - #[cfg(not(feature = "futures"))] /// Fetch the next row of this result set. - pub async fn next(&mut self) -> Result> { + pub async fn next(&mut self) -> crate::Result> { + use crate::Error; loop { let mut stmt = self .inner @@ -140,3 +141,10 @@ impl<'a> FromIterator<&'a turso_core::Value> for Row { Row { values } } } + +#[cfg(test)] +mod tests { + + #[tokio::test] + async fn test_row() {} +}