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> { struct DbValueIterator<'a> {
db: Option<MutexGuard<'a, Connection>>, db: MutexGuard<'a, Connection>,
stmt: Option<Statement<'a>>, stmt: Option<Statement<'a>>,
iter: Option<Rows<'a>>, iter: Option<Rows<'a>>,
_pin: PhantomPinned, _pin: PhantomPinned,
@ -301,7 +301,7 @@ impl<'a> DbValueIterator<'a> {
args: P, args: P,
) -> Result<ValueIter<'a>> { ) -> Result<ValueIter<'a>> {
let res = DbValueIterator { let res = DbValueIterator {
db: Some(db), db: db,
stmt: None, stmt: None,
iter: None, iter: None,
_pin: PhantomPinned, _pin: PhantomPinned,
@ -310,7 +310,7 @@ impl<'a> DbValueIterator<'a> {
unsafe { unsafe {
let db = NonNull::from(&boxed.db); 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); let mut_ref: Pin<&mut DbValueIterator<'a>> = Pin::as_mut(&mut boxed);
Pin::get_unchecked_mut(mut_ref).stmt = Some(stmt); 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>>>); struct DbValueIteratorPin<'a>(Pin<Box<DbValueIterator<'a>>>);
impl<'a> Iterator for DbValueIteratorPin<'a> { impl<'a> Iterator for DbValueIteratorPin<'a> {
@ -338,17 +345,7 @@ impl<'a> Iterator for DbValueIteratorPin<'a> {
}; };
let row = match next { let row = match next {
Err(e) => return Some(Err(e.into())), Err(e) => return Some(Err(e.into())),
Ok(None) => { Ok(None) => return 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(Some(r)) => r, Ok(Some(r)) => r,
}; };
let k = match row.get::<_, Vec<u8>>(0) { 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(); let mut iter = tree.iter().unwrap();
assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into())); assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into()));
assert!(iter.next().is_none()); assert!(iter.next().is_none());
drop(iter);
tree.insert(kb, vc).unwrap(); tree.insert(kb, vc).unwrap();
assert_eq!(tree.get(kb).unwrap(), Some(vc.into())); 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(), (ka.into(), vb.into()));
assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into())); assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into()));
assert!(iter.next().is_none()); assert!(iter.next().is_none());
drop(iter);
let mut iter = tree.range(kint..).unwrap(); let mut iter = tree.range(kint..).unwrap();
assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into())); assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into()));
assert!(iter.next().is_none()); assert!(iter.next().is_none());
drop(iter);
let mut iter = tree.range_rev(..kint).unwrap(); let mut iter = tree.range_rev(..kint).unwrap();
assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into())); assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into()));
assert!(iter.next().is_none()); assert!(iter.next().is_none());
drop(iter);
let mut iter = tree.iter_rev().unwrap(); let mut iter = tree.iter_rev().unwrap();
assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into())); assert_eq!(iter.next().unwrap().unwrap(), (kb.into(), vc.into()));
assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into())); assert_eq!(iter.next().unwrap().unwrap(), (ka.into(), vb.into()));
assert!(iter.next().is_none()); assert!(iter.next().is_none());
drop(iter);
} }
#[test] #[test]