I’ve been looking into it a bit - not pinning per se but self referential. There’s a library called ouroboros that looks helpful. There’s even an example on github where someone uses rusqlite and ouroboros together.
So it seems like this should work:
#[self_referencing]
pub struct ZkNoteStream {
conn: Connection,
#[borrows(conn)]
pstmt: rusqlite::Statement<'this>,
#[borrows(mut pstmt)]
#[covariant]
rec_iter: rusqlite::Rows<'this>,
}
impl ZkNoteStream {
pub fn init(conn: Connection, user: i64, search: &ZkNoteSearch) -> Result> {
let (sql, args) = build_sql(&conn, user, search.clone())?;
Ok(
ZkNoteStreamTryBuilder {
conn: conn,
pstmt_builder: |conn: &Connection| conn.prepare(sql.as_str()),
rec_iter_builder: |pstmt: &mut rusqlite::Statement<'_>| {
pstmt.query(rusqlite::params_from_iter(args.iter()))
},
}
.try_build()?,
)
}
}
Unfortunately I get this:
error[E0597]: `pstmt` does not live long enough
--> server-lib/src/search.rs:880:1
|
880 | #[self_referencing]
| ^^^^^^^^^^^^^^^^^^-
| | |
| | `pstmt` dropped here while still borrowed
| | borrow might be used here, when `pstmt` is dropped and runs the `Drop` code for type `Statement`
| borrowed value does not live long enough
|
= note: this error originates in the attribute macro `self_referencing` (in Nightly builds, run with -Z macro-backtrace for more info)
So close! But no cigar so far. No idea why its complaining.
Got this solved over on the rust-lang discourse. The solution was to use the async_stream crate. Ends up being a small amount of code too.