Simplify lmdb adapter (one less indirection)
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Alex 2022-06-06 15:04:04 +02:00
parent 17918e2ef5
commit a65049db8f
Signed by: lx
GPG key ID: 0E496D15096376BE

View file

@ -1,6 +1,4 @@
use core::marker::PhantomPinned;
use core::ops::Bound; use core::ops::Bound;
use core::pin::Pin;
use core::ptr::NonNull; use core::ptr::NonNull;
use std::collections::HashMap; use std::collections::HashMap;
@ -272,7 +270,6 @@ where
{ {
tx: RoTxn<'a>, tx: RoTxn<'a>,
iter: Option<I>, iter: Option<I>,
_pin: PhantomPinned,
} }
impl<'a, I> TxAndIterator<'a, I> impl<'a, I> TxAndIterator<'a, I>
@ -283,22 +280,12 @@ where
where where
F: FnOnce(&'a RoTxn<'a>) -> Result<I>, F: FnOnce(&'a RoTxn<'a>) -> Result<I>,
{ {
let res = TxAndIterator { let mut res = TxAndIterator { tx, iter: None };
tx,
iter: None,
_pin: PhantomPinned,
};
let mut boxed = Box::pin(res);
unsafe { let tx = unsafe { NonNull::from(&res.tx).as_ref() };
let tx = NonNull::from(&boxed.tx); res.iter = Some(iterfun(tx)?);
let iter = iterfun(tx.as_ref())?;
let mut_ref: Pin<&mut TxAndIterator<'a, I>> = Pin::as_mut(&mut boxed); Ok(Box::new(res))
Pin::get_unchecked_mut(mut_ref).iter = Some(iter);
}
Ok(Box::new(TxAndIteratorPin(boxed)))
} }
} }
@ -311,22 +298,14 @@ where
} }
} }
struct TxAndIteratorPin<'a, I: Iterator<Item = IteratorItem<'a>> + 'a>( impl<'a, I> Iterator for TxAndIterator<'a, I>
Pin<Box<TxAndIterator<'a, I>>>,
);
impl<'a, I> Iterator for TxAndIteratorPin<'a, I>
where where
I: Iterator<Item = IteratorItem<'a>> + 'a, I: Iterator<Item = IteratorItem<'a>> + 'a,
{ {
type Item = Result<(Value, Value)>; type Item = Result<(Value, Value)>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let iter_ref = unsafe { match self.iter.as_mut().unwrap().next() {
let mut_ref: Pin<&mut TxAndIterator<'a, I>> = Pin::as_mut(&mut self.0);
Pin::get_unchecked_mut(mut_ref).iter.as_mut()
};
match iter_ref.unwrap().next() {
None => None, None => None,
Some(Err(e)) => Some(Err(e.into())), Some(Err(e)) => Some(Err(e.into())),
Some(Ok((k, v))) => Some(Ok((k.to_vec(), v.to_vec()))), Some(Ok((k, v))) => Some(Ok((k.to_vec(), v.to_vec()))),