use an option rather than ManuallyDrop

This commit is contained in:
Brian Picciano
2025-12-19 14:25:14 +01:00
parent b81cab5d28
commit fcbaa828e9

View File

@@ -1,7 +1,6 @@
use std::{
cell::Cell,
fmt,
mem::ManuallyDrop,
ops::{Bound, RangeBounds},
};
@@ -377,7 +376,7 @@ impl<T: DupSort, CURSOR: DbDupCursorRO<T>> Iterator for DupWalker<'_, T, CURSOR>
/// This allows cursors to be reused across multiple operations,
/// reducing the overhead of repeatedly creating new cursors.
pub struct ReusableCursor<'tx, 'cell, T: Table, C: DbCursorRO<T>> {
cursor: ManuallyDrop<C>,
cursor: Option<C>,
cell: &'cell Cell<Option<C>>,
_phantom: std::marker::PhantomData<&'tx T>,
}
@@ -395,16 +394,13 @@ where
impl<'tx, 'cell, T: Table, C: DbCursorRO<T>> ReusableCursor<'tx, 'cell, T, C> {
/// Creates a new `ReusableCursor` from a cursor and a cell to return it to.
pub const fn new(cursor: C, cell: &'cell Cell<Option<C>>) -> Self {
Self { cursor: ManuallyDrop::new(cursor), cell, _phantom: std::marker::PhantomData }
Self { cursor: Some(cursor), cell, _phantom: std::marker::PhantomData }
}
}
impl<'tx, 'cell, T: Table, C: DbCursorRO<T>> Drop for ReusableCursor<'tx, 'cell, T, C> {
fn drop(&mut self) {
// SAFETY: We take ownership of the cursor and return it to the cell.
// The cursor won't be dropped automatically due to ManuallyDrop.
let cursor = unsafe { ManuallyDrop::take(&mut self.cursor) };
self.cell.set(Some(cursor));
self.cell.set(self.cursor.take());
}
}
@@ -412,7 +408,7 @@ impl<'tx, 'cell, T: Table, C: DbCursorRO<T>> std::ops::Deref for ReusableCursor<
type Target = C;
fn deref(&self) -> &Self::Target {
&self.cursor
self.cursor.as_ref().expect("cursor always exists")
}
}
@@ -420,6 +416,6 @@ impl<'tx, 'cell, T: Table, C: DbCursorRO<T>> std::ops::DerefMut
for ReusableCursor<'tx, 'cell, T, C>
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.cursor
self.cursor.as_mut().expect("cursor always exists")
}
}