# HG changeset patch # User Georges Racinet # Date 1698574878 -3600 # Node ID 0993a3520dc648e74f6221563d132fa5979feea9 # Parent 2e2832e00f6ceaf96f458e297de315771bd9771e 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`. diff -r 2e2832e00f6c -r 0993a3520dc6 rust/hg-cpython/src/discovery.rs --- 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> { + 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 { - 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 { - 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 { - 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))?;