Add example of OwningRows using Ouroboros

This commit is contained in:
gwenn 2024-03-03 18:15:22 +01:00
parent def8e9460d
commit 7dbc3175f9
2 changed files with 30 additions and 0 deletions

View File

@ -132,6 +132,7 @@ lazy_static = "1.4"
regex = "1.5.5"
uuid = { version = "1.0", features = ["v4"] }
unicase = "2.6.0"
ouroboros = "0.18"
# Use `bencher` over criterion because it builds much faster and we don't have
# many benchmarks
bencher = "0.1"

29
examples/owning_rows.rs Normal file
View File

@ -0,0 +1,29 @@
extern crate rusqlite;
use ouroboros::self_referencing;
use rusqlite::{CachedStatement, Connection, Result, Rows};
#[self_referencing]
struct OwningRows<'conn> {
stmt: CachedStatement<'conn>,
#[borrows(mut stmt)]
#[covariant]
rows: Rows<'this>,
}
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;
let stmt = conn.prepare_cached("SELECT 1")?;
let mut or = OwningRowsTryBuilder {
stmt,
rows_builder: |s| s.query([]),
}
.try_build()?;
or.with_rows_mut(|rows| -> Result<()> {
while let Some(row) = rows.next()? {
assert_eq!(Ok(1), row.get(0));
}
Ok(())
})?;
Ok(())
}