Adding lifetime to DBIterator and DBRawIterator...

master
Jordan Terrell 6 years ago
parent 248b40b465
commit 857efff670
  1. 31
      src/db.rs

@ -142,8 +142,9 @@ pub struct Snapshot<'a> {
/// iter.prev(); /// iter.prev();
/// } /// }
/// ``` /// ```
pub struct DBRawIterator { pub struct DBRawIterator<'a> {
inner: *mut ffi::rocksdb_iterator_t, inner: *mut ffi::rocksdb_iterator_t,
db: PhantomData<&'a DB>
} }
/// An iterator over a database or column family, with specifiable /// An iterator over a database or column family, with specifiable
@ -173,13 +174,13 @@ pub struct DBRawIterator {
/// println!("Saw {:?} {:?}", key, value); /// println!("Saw {:?} {:?}", key, value);
/// } /// }
/// ``` /// ```
pub struct DBIterator { pub struct DBIterator<'a> {
raw: DBRawIterator, raw: DBRawIterator<'a>,
direction: Direction, direction: Direction,
just_seeked: bool, just_seeked: bool,
} }
unsafe impl Send for DBIterator {} unsafe impl<'a> Send for DBIterator<'a> {}
pub enum Direction { pub enum Direction {
Forward, Forward,
@ -194,11 +195,12 @@ pub enum IteratorMode<'a> {
From(&'a [u8], Direction), From(&'a [u8], Direction),
} }
impl DBRawIterator { impl<'a> DBRawIterator<'a> {
fn new(db: &DB, readopts: &ReadOptions) -> DBRawIterator { fn new(db: &DB, readopts: &ReadOptions) -> DBRawIterator<'a> {
unsafe { unsafe {
DBRawIterator { DBRawIterator {
inner: ffi::rocksdb_create_iterator(db.inner, readopts.inner), inner: ffi::rocksdb_create_iterator(db.inner, readopts.inner),
db: PhantomData
} }
} }
} }
@ -207,10 +209,11 @@ impl DBRawIterator {
db: &DB, db: &DB,
cf_handle: ColumnFamily, cf_handle: ColumnFamily,
readopts: &ReadOptions, readopts: &ReadOptions,
) -> Result<DBRawIterator, Error> { ) -> Result<DBRawIterator<'a>, Error> {
unsafe { unsafe {
Ok(DBRawIterator { Ok(DBRawIterator {
inner: ffi::rocksdb_create_iterator_cf(db.inner, readopts.inner, cf_handle.inner), inner: ffi::rocksdb_create_iterator_cf(db.inner, readopts.inner, cf_handle.inner),
db: PhantomData
}) })
} }
} }
@ -425,7 +428,7 @@ impl DBRawIterator {
} }
} }
impl Drop for DBRawIterator { impl<'a> Drop for DBRawIterator<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
ffi::rocksdb_iter_destroy(self.inner); ffi::rocksdb_iter_destroy(self.inner);
@ -433,8 +436,8 @@ impl Drop for DBRawIterator {
} }
} }
impl DBIterator { impl<'a> DBIterator<'a> {
fn new(db: &DB, readopts: &ReadOptions, mode: IteratorMode) -> DBIterator { fn new(db: &DB, readopts: &ReadOptions, mode: IteratorMode) -> DBIterator<'a> {
let mut rv = DBIterator { let mut rv = DBIterator {
raw: DBRawIterator::new(db, readopts), raw: DBRawIterator::new(db, readopts),
direction: Direction::Forward, // blown away by set_mode() direction: Direction::Forward, // blown away by set_mode()
@ -449,7 +452,7 @@ impl DBIterator {
cf_handle: ColumnFamily, cf_handle: ColumnFamily,
readopts: &ReadOptions, readopts: &ReadOptions,
mode: IteratorMode, mode: IteratorMode,
) -> Result<DBIterator, Error> { ) -> Result<DBIterator<'a>, Error> {
let mut rv = DBIterator { let mut rv = DBIterator {
raw: DBRawIterator::new_cf(db, cf_handle, readopts)?, raw: DBRawIterator::new_cf(db, cf_handle, readopts)?,
direction: Direction::Forward, // blown away by set_mode() direction: Direction::Forward, // blown away by set_mode()
@ -487,7 +490,7 @@ impl DBIterator {
} }
} }
impl Iterator for DBIterator { impl<'a> Iterator for DBIterator<'a> {
type Item = KVBytes; type Item = KVBytes;
fn next(&mut self) -> Option<KVBytes> { fn next(&mut self) -> Option<KVBytes> {
@ -514,8 +517,8 @@ impl Iterator for DBIterator {
} }
} }
impl Into<DBRawIterator> for DBIterator { impl<'a> Into<DBRawIterator<'a>> for DBIterator<'a> {
fn into(self) -> DBRawIterator { fn into(self) -> DBRawIterator<'a> {
self.raw self.raw
} }
} }

Loading…
Cancel
Save