Skip to content

Commit

Permalink
Iterator should hold the lifetime of from_key & to_key
Browse files Browse the repository at this point in the history
  • Loading branch information
rim99 committed Oct 13, 2024
1 parent 0020ad3 commit 68414f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
56 changes: 28 additions & 28 deletions src/database/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub struct Iterator<'a, K: Serializable + 'a> {
#[allow(dead_code)]
database: PhantomData<&'a Database<K>>,
iter: RawIterator,
from: Option<&'a K>,
to: Option<&'a K>,
from: Option<K>,
to: Option<K>,
}

/// An iterator over the leveldb keyspace that browses the keys backwards.
Expand All @@ -54,8 +54,8 @@ pub struct RevIterator<'a, K: Serializable + 'a> {
#[allow(dead_code)]
database: PhantomData<&'a Database<K>>,
iter: RawIterator,
from: Option<&'a K>,
to: Option<&'a K>,
from: Option<K>,
to: Option<K>,
}

/// An iterator over the leveldb keyspace.
Expand Down Expand Up @@ -132,11 +132,11 @@ pub trait LevelDBIterator<'a, K: Serializable + Ord> {

fn reverse(self) -> Self::RevIter;

fn from(self, key: &'a K) -> Self;
fn to(self, key: &'a K) -> Self;
fn from(self, key: K) -> Self;
fn to(self, key: K) -> Self;

fn from_key(&self) -> Option<&K>;
fn to_key(&self) -> Option<&K>;
fn from_key(&self) -> &Option<K>;
fn to_key(&self) -> &Option<K>;

fn valid(&self) -> bool {
unsafe { leveldb_iter_valid(self.raw_iterator()) != 0 }
Expand Down Expand Up @@ -286,22 +286,22 @@ impl<'a, K: Serializable + Ord> LevelDBIterator<'a, K> for Iterator<'a, K> {
}
}

fn from(mut self, key: &'a K) -> Self {
fn from(mut self, key: K) -> Self {
self.from = Some(key);
self
}

fn to(mut self, key: &'a K) -> Self {
fn to(mut self, key: K) -> Self {
self.to = Some(key);
self
}

fn from_key(&self) -> Option<&K> {
self.from
fn from_key(&self) -> &Option<K> {
&self.from
}

fn to_key(&self) -> Option<&K> {
self.to
fn to_key(&self) -> &Option<K> {
&self.to
}
}

Expand Down Expand Up @@ -355,22 +355,22 @@ impl<'a, K: Serializable + Ord> LevelDBIterator<'a, K> for RevIterator<'a, K> {
}
}

fn from(mut self, key: &'a K) -> Self {
fn from(mut self, key: K) -> Self {
self.from = Some(key);
self
}

fn to(mut self, key: &'a K) -> Self {
fn to(mut self, key: K) -> Self {
self.to = Some(key);
self
}

fn from_key(&self) -> Option<&K> {
self.from
fn from_key(&self) -> &Option<K> {
&self.from
}

fn to_key(&self) -> Option<&K> {
self.to
fn to_key(&self) -> &Option<K> {
&self.to
}
}

Expand Down Expand Up @@ -444,22 +444,22 @@ macro_rules! impl_leveldb_iterator {
}
}

fn from(mut self, key: &'a K) -> Self {
fn from(mut self, key: K) -> Self {
self.inner.from = Some(key);
self
}

fn to(mut self, key: &'a K) -> Self {
fn to(mut self, key: K) -> Self {
self.inner.to = Some(key);
self
}

fn from_key(&self) -> Option<&K> {
self.inner.from
fn from_key(&self) -> &Option<K> {
&self.inner.from
}

fn to_key(&self) -> Option<&K> {
self.inner.to
fn to_key(&self) -> &Option<K> {
&self.inner.to
}
}
};
Expand All @@ -474,8 +474,8 @@ macro_rules! impl_iterator {
($T:ty, $Item:ty, $ItemMethod:ident) => {
impl<'a, K: Serializable + Ord> iter::Iterator for $T {
type Item = $Item;
// The "next" method would iterate once, and stop at the end (to_key).
// Use "advance" method if starting from begin (from_key) again is preferred
/// The "next" method would iterate once, and stop at the end (to_key).
/// Use "advance" method if starting from begin (from_key) again is preferred
fn next(&mut self) -> Option<Self::Item> {
if self.advance() && !self.stopped() {
Some(self.$ItemMethod())
Expand Down
20 changes: 10 additions & 10 deletions tests/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ fn test_iterator_from_to() {
db_put_simple(database, 4, &[4]);
db_put_simple(database, 5, &[5]);

let from = 2;
let to = 4;
let begin = 2;
let end = 4;
let read_opts = ReadOptions::new();
let mut iter = database.iter(read_opts).from(&from).to(&to);
let mut iter = database.iter(read_opts).from(begin).to(end);

assert_eq!(iter.next().unwrap(), (2, vec![2]));
assert_eq!(iter.last().unwrap(), (4, vec![4]));
Expand All @@ -86,7 +86,7 @@ fn test_iterator_bounded_next() {
let begin = 2;
let end = 4;
let read_opts = ReadOptions::new();
let mut iter = database.iter(read_opts).from(&begin).to(&end);
let mut iter = database.iter(read_opts).from(begin).to(end);

let mut keys = vec![];
while let Some((k, _)) = iter.next() {
Expand All @@ -108,7 +108,7 @@ fn test_iterator_bounded_next_2() {
let begin = 2;
let end = 6;
let read_opts = ReadOptions::new();
let mut iter = database.iter(read_opts).from(&begin).to(&end);
let mut iter = database.iter(read_opts).from(begin).to(end);

let mut keys = vec![];
while let Some((k, _)) = iter.next() {
Expand All @@ -130,7 +130,7 @@ fn test_iterator_bounded_next_3() {
let begin = 1;
let end = 7;
let read_opts = ReadOptions::new();
let mut iter = database.iter(read_opts).from(&begin).to(&end);
let mut iter = database.iter(read_opts).from(begin).to(end);

let mut keys = vec![];
while let Some((k, _)) = iter.next() {
Expand All @@ -152,7 +152,7 @@ fn test_iterator_bounded_next_4() {
let begin = 1;
let end = 5;
let read_opts = ReadOptions::new();
let mut iter = database.iter(read_opts).from(&begin).to(&end);
let mut iter = database.iter(read_opts).from(begin).to(end);

let mut keys = vec![];
while let Some((k, _)) = iter.next() {
Expand All @@ -174,7 +174,7 @@ fn test_iterator_bounded_next_5() {
let begin = 4;
let end = 7;
let read_opts = ReadOptions::new();
let mut iter = database.iter(read_opts).from(&begin).to(&end);
let mut iter = database.iter(read_opts).from(begin).to(end);

let mut keys = vec![];
while let Some((k, _)) = iter.next() {
Expand All @@ -192,7 +192,7 @@ fn test_iterator_bounded_next_6() {
let begin = 2;
let end = 3;
let read_opts = ReadOptions::new();
let mut iter = database.iter(read_opts).from(&begin).to(&end);
let mut iter = database.iter(read_opts).from(begin).to(end);

let mut keys = vec![];
while let Some((k, _)) = iter.next() {
Expand All @@ -210,7 +210,7 @@ fn test_iterator_bounded_next_7() {
let begin = 1;
let end = 2;
let read_opts = ReadOptions::new();
let mut iter = database.iter(read_opts).from(&begin).to(&end);
let mut iter = database.iter(read_opts).from(begin).to(end);

let mut keys = vec![];
while let Some((k, _)) = iter.next() {
Expand Down

0 comments on commit 68414f9

Please sign in to comment.