From f435fa7e75b195cf14e7a44c964fa5c0cb53ef21 Mon Sep 17 00:00:00 2001 From: rnarubin Date: Sun, 8 Sep 2019 01:49:31 -0700 Subject: [PATCH] Expose the `status` method on iterators (#302) --- src/db.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/db.rs b/src/db.rs index 6e5959e..1cd361f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -235,11 +235,28 @@ impl<'a> DBRawIterator<'a> { } } - /// Returns true if the iterator is valid. + /// Returns `true` if the iterator is valid. An iterator is invalidated when + /// it reaches the end of its defined range, or when it encounters an error. + /// + /// To check whether the iterator encountered an error after `valid` has + /// returned `false`, use the [`status`](DBRawIterator::status) method. `status` will never + /// return an error when `valid` is `true`. pub fn valid(&self) -> bool { unsafe { ffi::rocksdb_iter_valid(self.inner) != 0 } } + /// Returns an error `Result` if the iterator has encountered an error + /// during operation. When an error is encountered, the iterator is + /// invalidated and [`valid`](DBRawIterator::valid) will return `false` when called. + /// + /// Performing a seek will discard the current status. + pub fn status(&self) -> Result<(), Error> { + unsafe { + ffi_try!(ffi::rocksdb_iter_get_error(self.inner,)); + } + Ok(()) + } + /// Seeks to the first key in the database. /// /// # Examples @@ -515,9 +532,15 @@ impl<'a> DBIterator<'a> { self.just_seeked = true; } + /// See [`valid`](DBRawIterator::valid) pub fn valid(&self) -> bool { self.raw.valid() } + + /// See [`status`](DBRawIterator::status) + pub fn status(&self) -> Result<(), Error> { + self.raw.status() + } } impl<'a> Iterator for DBIterator<'a> {