diff --git a/Cargo.toml b/Cargo.toml index 99b6d7c..9318b6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -146,6 +146,7 @@ regex = "1.5.5" uuid = { version = "1.0", features = ["v4"] } unicase = "2.6.0" ouroboros = "0.18" +self_cell = { git = "https://github.com/Voultapher/self_cell.git" } # Use `bencher` over criterion because it builds much faster, # and we don't have many benchmarks bencher = "0.1" diff --git a/examples/owning_rows_self_cell.rs b/examples/owning_rows_self_cell.rs new file mode 100644 index 0000000..1e42212 --- /dev/null +++ b/examples/owning_rows_self_cell.rs @@ -0,0 +1,27 @@ +extern crate rusqlite; + +use rusqlite::{CachedStatement, Connection, Result, Rows}; +use self_cell::{self_cell, MutBorrow}; + +type RowsRef<'a> = Rows<'a>; + +self_cell!( + struct OwningRows<'conn> { + owner: MutBorrow>, + #[covariant] + dependent: RowsRef, + } +); + +fn main() -> Result<()> { + let conn = Connection::open_in_memory()?; + let stmt = conn.prepare_cached("SELECT 1")?; + let mut or = OwningRows::try_new(MutBorrow::new(stmt), |s| s.borrow_mut().query([]))?; + or.with_dependent_mut(|_stmt, rows| -> Result<()> { + while let Some(row) = rows.next()? { + assert_eq!(Ok(1), row.get(0)); + } + Ok(()) + })?; + Ok(()) +}