From 47f6f8130155a8fbeb333f7450c1da03b59c7b6d Mon Sep 17 00:00:00 2001 From: Christopher Berner Date: Sat, 20 Jan 2024 17:32:31 -0800 Subject: [PATCH] impl Clone for Range --- src/table.rs | 1 + src/tree_store/btree_iters.rs | 3 ++- tests/basic_tests.rs | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/table.rs b/src/table.rs index 061dc69b..0046f1ec 100644 --- a/src/table.rs +++ b/src/table.rs @@ -605,6 +605,7 @@ impl< } } +#[derive(Clone)] pub struct Range<'a, K: RedbKey + 'static, V: RedbValue + 'static> { inner: BtreeRangeIter, _transaction_guard: Arc, diff --git a/src/tree_store/btree_iters.rs b/src/tree_store/btree_iters.rs index f08e4399..00bb29f4 100644 --- a/src/tree_store/btree_iters.rs +++ b/src/tree_store/btree_iters.rs @@ -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, @@ -380,6 +380,7 @@ impl FnMut(K::SelfType<'f>, V::SelfType<'f> } } +#[derive(Clone)] pub(crate) struct BtreeRangeIter { left: Option, // Exclusive. The previous element returned right: Option, // Exclusive. The previous element returned diff --git a/tests/basic_tests.rs b/tests/basic_tests.rs index 490b0bdb..b440c15e 100644 --- a/tests/basic_tests.rs +++ b/tests/basic_tests.rs @@ -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();