Skip to content

Commit

Permalink
impl Clone for Range
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Jan 21, 2024
1 parent a6acb67 commit 2a36266
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ impl<
}
}

#[derive(Clone)]
pub struct Range<'a, K: RedbKey + 'static, V: RedbValue + 'static> {
inner: BtreeRangeIter<K, V>,
_transaction_guard: Arc<TransactionGuard>,
Expand Down
3 changes: 2 additions & 1 deletion src/tree_store/btree_iters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::marker::PhantomData;
use std::ops::{Range, RangeBounds};
use std::sync::{Arc, Mutex};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum RangeIterState {
Leaf {
page: PageImpl,
Expand Down Expand Up @@ -380,6 +380,7 @@ impl<K: RedbKey, V: RedbValue, F: for<'f> FnMut(K::SelfType<'f>, V::SelfType<'f>
}
}

#[derive(Clone)]
pub(crate) struct BtreeRangeIter<K: RedbKey + 'static, V: RedbValue + 'static> {
left: Option<RangeIterState>, // Exclusive. The previous element returned
right: Option<RangeIterState>, // Exclusive. The previous element returned
Expand Down
21 changes: 21 additions & 0 deletions tests/basic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,27 @@ fn range_arc() {
assert!(iter.next().is_none());
}

#[test]
fn range_clone() {
let tmpfile = create_tempfile();
let db = Database::create(tmpfile.path()).unwrap();

let definition: TableDefinition<&str, &str> = TableDefinition::new("x");

let write_txn = db.begin_write().unwrap();
{
let mut table = write_txn.open_table(definition).unwrap();
table.insert("hello", "world").unwrap();
let mut iter1 = table.iter().unwrap();
let mut iter2 = iter1.clone();
let (k1, v1) = iter1.next().unwrap().unwrap();
let (k2, v2) = iter2.next().unwrap().unwrap();
assert_eq!(k1.value(), k2.value());
assert_eq!(v1.value(), v2.value());
}
write_txn.commit().unwrap();
}

#[test]
fn drain_lifetime() {
let tmpfile = create_tempfile();
Expand Down

0 comments on commit 2a36266

Please sign in to comment.