changeset 51264:0993a3520dc6

rust-discovery: encapsulated conversions to vec for instance methods This new `pyiter_to_vec` is pretty trivial, and only mildly reduces code duplication. The main advantage is that it encapsulates access to the `index` attribute, which will be changed when we replace the C index by the Rust index, given as `PySharedRef`.
author Georges Racinet <georges.racinet@octobus.net>
date Sun, 29 Oct 2023 11:21:18 +0100
parents 2e2832e00f6c
children 03fdd4d7b5bd
files rust/hg-cpython/src/discovery.rs
diffstat 1 files changed, 13 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-cpython/src/discovery.rs	Sun Oct 29 11:10:09 2023 +0100
+++ b/rust/hg-cpython/src/discovery.rs	Sun Oct 29 11:21:18 2023 +0100
@@ -116,6 +116,16 @@
         )
     }
 
+    /// Convert a Python iterator of revisions into a vector
+    fn pyiter_to_vec(
+        &self,
+        py: Python,
+        iter: &PyObject,
+    ) -> PyResult<Vec<Revision>> {
+        let index = self.index(py).borrow();
+        rev_pyiter_collect(py, iter, &*index)
+    }
+
     fn inner_addinfo(
         &self,
         py: Python,
@@ -152,8 +162,7 @@
         py: Python,
         commons: PyObject,
     ) -> PyResult<PyObject> {
-        let index = self.index(py).borrow();
-        let commons_vec: Vec<_> = rev_pyiter_collect(py, &commons, &*index)?;
+        let commons_vec = self.pyiter_to_vec(py, &commons)?;
         let mut inner = self.inner(py).borrow_mut();
         inner
             .add_common_revisions(commons_vec)
@@ -166,8 +175,7 @@
         py: Python,
         missings: PyObject,
     ) -> PyResult<PyObject> {
-        let index = self.index(py).borrow();
-        let missings_vec: Vec<_> = rev_pyiter_collect(py, &missings, &*index)?;
+        let missings_vec = self.pyiter_to_vec(py, &missings)?;
         let mut inner = self.inner(py).borrow_mut();
         inner
             .add_missing_revisions(missings_vec)
@@ -198,9 +206,8 @@
         headrevs: PyObject,
         size: usize,
     ) -> PyResult<PyObject> {
-        let index = self.index(py).borrow();
+        let revsvec = self.pyiter_to_vec(py, &headrevs)?;
         let mut inner = self.inner(py).borrow_mut();
-        let revsvec: Vec<_> = rev_pyiter_collect(py, &headrevs, &*index)?;
         let sample = inner
             .take_quick_sample(revsvec, size)
             .map_err(|e| GraphError::pynew(py, e))?;