make futures-util default feature + use futures in example.rs

This commit is contained in:
pedrocarlo
2025-06-30 12:48:59 -03:00
parent a1fe46779d
commit e08748e07e
4 changed files with 30 additions and 14 deletions

View File

@@ -10,7 +10,7 @@ repository.workspace = true
description = "Turso Rust API" description = "Turso Rust API"
[features] [features]
default = [] default = ["futures"]
experimental_indexes = [] experimental_indexes = []
antithesis = ["turso_core/antithesis"] antithesis = ["turso_core/antithesis"]
futures = ["dep:futures-util"] futures = ["dep:futures-util"]

View File

@@ -1,5 +1,6 @@
#[cfg(feature = "futures")] #![cfg(feature = "futures")]
use futures_util::stream::StreamExt;
use futures_util::stream::TryStreamExt;
use turso::Builder; use turso::Builder;
#[tokio::main] #[tokio::main]
@@ -8,8 +9,15 @@ async fn main() {
let conn = db.connect().unwrap(); 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)", ()) conn.execute("CREATE TABLE IF NOT EXISTS users (email TEXT)", ())
.await .await
.unwrap(); .unwrap();
@@ -34,9 +42,8 @@ async fn main() {
let mut rows = stmt.query(["foo@example.com"]).await.unwrap(); let mut rows = stmt.query(["foo@example.com"]).await.unwrap();
let row = rows.next().await.unwrap().unwrap(); while let Some(row) = rows.try_next().await.unwrap() {
let value = row.get_value(0).unwrap();
let value = row.get_value(0).unwrap(); println!("Row: {:?}", value);
}
println!("Row: {:?}", value);
} }

View File

@@ -37,9 +37,10 @@ pub mod row;
pub mod statement; pub mod statement;
pub mod value; pub mod value;
pub use value::Value; #[cfg(feature = "futures")]
pub use futures_util;
pub use params::params_from_iter; pub use params::params_from_iter;
pub use value::Value;
use crate::params::*; use crate::params::*;
use crate::row::{Row, Rows}; use crate::row::{Row, Rows};
@@ -65,7 +66,7 @@ impl From<turso_core::LimboError> for Error {
pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>; pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>;
pub(crate) type Result<T> = std::result::Result<T, Error>; pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
/// A builder for `Database`. /// A builder for `Database`.
pub struct Builder { pub struct Builder {

View File

@@ -19,10 +19,11 @@ impl Clone for Rows {
unsafe impl Send for Rows {} unsafe impl Send for Rows {}
unsafe impl Sync for Rows {} unsafe impl Sync for Rows {}
#[cfg(not(feature = "futures"))]
impl Rows { impl Rows {
#[cfg(not(feature = "futures"))]
/// Fetch the next row of this result set. /// Fetch the next row of this result set.
pub async fn next(&mut self) -> Result<Option<Row>> { pub async fn next(&mut self) -> crate::Result<Option<Row>> {
use crate::Error;
loop { loop {
let mut stmt = self let mut stmt = self
.inner .inner
@@ -140,3 +141,10 @@ impl<'a> FromIterator<&'a turso_core::Value> for Row {
Row { values } Row { values }
} }
} }
#[cfg(test)]
mod tests {
#[tokio::test]
async fn test_row() {}
}