nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: cberner/redb
Pull request #503 (2023-01-22 19:12:32)

Decouple &self and argument lifetime for multimap range()


Patch:
diff --git a/src/multimap_table.rs b/src/multimap_table.rs
index d968b40..c611734 100644
--- a/src/multimap_table.rs
+++ b/src/multimap_table.rs
@@ -806,13 +806,13 @@ impl<'db, 'txn, K: RedbKey + ?Sized, V: RedbKey + ?Sized> ReadableMultimapTable<
     }
 
     /// Returns a double-ended iterator over a range of elements in the table
-    fn range<'a, KR>(
+    fn range<'a: 'b, 'b, KR>(
         &'a self,
-        range: impl RangeBounds<KR> + 'a,
+        range: impl RangeBounds<KR> + 'b,
     ) -> Result<MultimapRangeIter<'a, K, V>>
     where
         K: 'a,
-        KR: Borrow<K::SelfType<'a>> + ?Sized + 'a,
+        KR: Borrow<K::SelfType<'b>> + ?Sized + 'b,
     {
         let inner = self.tree.range(range)?;
         Ok(MultimapRangeIter::new(inner, self.mem))
@@ -847,13 +847,13 @@ pub trait ReadableMultimapTable<K: RedbKey + ?Sized, V: RedbKey + ?Sized> {
     where
         K: 'a;
 
-    fn range<'a, KR>(
+    fn range<'a: 'b, 'b, KR>(
         &'a self,
-        range: impl RangeBounds<KR> + 'a,
+        range: impl RangeBounds<KR> + 'b,
     ) -> Result<MultimapRangeIter<'a, K, V>>
     where
         K: 'a,
-        KR: Borrow<K::SelfType<'a>> + ?Sized + 'a;
+        KR: Borrow<K::SelfType<'b>> + ?Sized + 'b;
 
     fn len(&self) -> Result<usize>;
 
@@ -905,13 +905,13 @@ impl<'txn, K: RedbKey + ?Sized, V: RedbKey + ?Sized> ReadableMultimapTable<K, V>
         Ok(iter)
     }
 
-    fn range<'a, KR>(
+    fn range<'a: 'b, 'b, KR>(
         &'a self,
-        range: impl RangeBounds<KR> + 'a,
+        range: impl RangeBounds<KR> + 'b,
     ) -> Result<MultimapRangeIter<'a, K, V>>
     where
         K: 'a,
-        KR: Borrow<K::SelfType<'a>> + ?Sized + 'a,
+        KR: Borrow<K::SelfType<'b>> + ?Sized + 'b,
     {
         let inner = self.tree.range(range)?;
         Ok(MultimapRangeIter::new(inner, self.mem))


Test patch:
diff --git a/tests/multimap_tests.rs b/tests/multimap_tests.rs
index 7e0c20b..99159f6 100644
--- a/tests/multimap_tests.rs
+++ b/tests/multimap_tests.rs
@@ -129,6 +129,31 @@ fn range_query() {
     assert_eq!(total, 45);
 }
 
+#[test]
+fn range_lifetime() {
+    let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
+    let db = Database::create(tmpfile.path()).unwrap();
+
+    let definition: MultimapTableDefinition<&str, &str> = MultimapTableDefinition::new("x");
+
+    let write_txn = db.begin_write().unwrap();
+    {
+        let mut table = write_txn.open_multimap_table(definition).unwrap();
+        table.insert("hello", "world").unwrap();
+    }
+    write_txn.commit().unwrap();
+
+    let read_txn = db.begin_read().unwrap();
+    let table = read_txn.open_multimap_table(definition).unwrap();
+
+    let mut iter = {
+        let start = "hello".to_string();
+        table.range::<&str>(start.as_str()..).unwrap()
+    };
+    assert_eq!(iter.next().unwrap().1.next().unwrap().value(), "world");
+    assert!(iter.next().is_none());
+}
+
 #[test]
 fn delete() {
     let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();