Drop later

This commit is contained in:
Alex 2022-06-03 04:44:40 +02:00
parent 6ebb54cddb
commit 7b79906350
Signed by: lx
GPG Key ID: 0E496D15096376BE
2 changed files with 16 additions and 14 deletions

View File

@ -288,7 +288,7 @@ impl<'a> ITx<'a> for SqliteTx<'a> {
// ----
struct DbValueIterator<'a> {
db: Option<MutexGuard<'a, Connection>>,
db: MutexGuard<'a, Connection>,
stmt: Option<Statement<'a>>,
iter: Option<Rows<'a>>,
_pin: PhantomPinned,
@ -301,7 +301,7 @@ impl<'a> DbValueIterator<'a> {
args: P,
) -> Result<ValueIter<'a>> {
let res = DbValueIterator {
db: Some(db),
db: db,
stmt: None,
iter: None,
_pin: PhantomPinned,
@ -310,7 +310,7 @@ impl<'a> DbValueIterator<'a> {
unsafe {
let db = NonNull::from(&boxed.db);
let stmt = db.as_ref().as_ref().unwrap().prepare(&sql)?;
let stmt = db.as_ref().prepare(&sql)?;
let mut_ref: Pin<&mut DbValueIterator<'a>> = Pin::as_mut(&mut boxed);
Pin::get_unchecked_mut(mut_ref).stmt = Some(stmt);
@ -326,6 +326,13 @@ impl<'a> DbValueIterator<'a> {
}
}
impl<'a> Drop for DbValueIterator<'a> {
fn drop(&mut self) {
drop(self.iter.take());
drop(self.stmt.take());
}
}
struct DbValueIteratorPin<'a>(Pin<Box<DbValueIterator<'a>>>);
impl<'a> Iterator for DbValueIteratorPin<'a> {
@ -338,17 +345,7 @@ impl<'a> Iterator for DbValueIteratorPin<'a> {
};
let row = match next {
Err(e) => return Some(Err(e.into())),
Ok(None) => {
// finished, drop everything
unsafe {
let mut_ref: Pin<&mut DbValueIterator<'a>> = Pin::as_mut(&mut self.0);
let t = Pin::get_unchecked_mut(mut_ref);
drop(t.iter.take());
drop(t.stmt.take());
drop(t.db.take());
}
return None;
}
Ok(None) => return None,
Ok(Some(r)) => r,
};
let k = match row.get::<_, Vec<u8>>(0) {

View File

@ -43,6 +43,7 @@ fn test_suite(db: Db) {
let mut iter = tree.iter().unwrap();
assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into()));
assert!(iter.next().is_none());
drop(iter);
tree.insert(kb, vc).unwrap();
assert_eq!(tree.get(kb).unwrap(), Some(vc.into()));
@ -51,19 +52,23 @@ fn test_suite(db: Db) {
assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into()));
assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into()));
assert!(iter.next().is_none());
drop(iter);
let mut iter = tree.range(kint..).unwrap();
assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into()));
assert!(iter.next().is_none());
drop(iter);
let mut iter = tree.range_rev(..kint).unwrap();
assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into()));
assert!(iter.next().is_none());
drop(iter);
let mut iter = tree.iter_rev().unwrap();
assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into()));
assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into()));
assert!(iter.next().is_none());
drop(iter);
}
#[test]