mirror of
https://github.com/isar/rusqlite.git
synced 2025-08-19 12:29:34 +08:00
Add example of OwningRows using self_cell
This commit is contained in:
27
examples/owning_rows_self_cell.rs
Normal file
27
examples/owning_rows_self_cell.rs
Normal file
@@ -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<CachedStatement<'conn>>,
|
||||
#[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(())
|
||||
}
|
Reference in New Issue
Block a user