rust-cpython: generalised conversion function
authorGeorges Racinet <georges.racinet@octobus.net>
Wed, 09 Jan 2019 17:31:36 +0100
changeset 41187 3bf6979a1785
parent 41186 1b4b94bac8a0
child 41188 006c9ce486fa
rust-cpython: generalised conversion function Because `hg::ancestors::MissingAncestors` has methods taking some `HashSet<Revision>` besides `impl IntoIterator<Item = Revision>` as parameters we'll need the more generic `rev_pyiter_collect()` function to also build these Differential Revision: https://phab.mercurial-scm.org/D5549
rust/hg-cpython/src/ancestors.rs
--- a/rust/hg-cpython/src/ancestors.rs	Tue Jan 08 14:00:33 2019 +0100
+++ b/rust/hg-cpython/src/ancestors.rs	Wed Jan 09 17:31:36 2019 +0100
@@ -30,13 +30,18 @@
 use hg::Revision;
 use hg::{AncestorsIterator as CoreIterator, LazyAncestors as CoreLazy};
 use std::cell::RefCell;
+use std::iter::FromIterator;
 
-/// Utility function to convert a Python iterable into a Vec<Revision>
+/// Utility function to convert a Python iterable into various collections
 ///
-/// We need this to feed to `AncestorIterators` constructors because
-/// a `PyErr` can arise at each step of iteration, whereas our inner objects
+/// We need this in particular to feed to various methods of inner objects
+/// with `impl IntoIterator<Item=Revision>` arguments, because
+/// a `PyErr` can arise at each step of iteration, whereas these methods
 /// expect iterables over `Revision`, not over some `Result<Revision, PyErr>`
-fn reviter_to_revvec(py: Python, revs: PyObject) -> PyResult<Vec<Revision>> {
+fn rev_pyiter_collect<C>(py: Python, revs: &PyObject) -> PyResult<C>
+where
+    C: FromIterator<Revision>,
+{
     revs.iter(py)?
         .map(|r| r.and_then(|o| o.extract::<Revision>(py)))
         .collect()
@@ -64,7 +69,7 @@
 
     def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision,
                 inclusive: bool) -> PyResult<AncestorsIterator> {
-        let initvec = reviter_to_revvec(py, initrevs)?;
+        let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?;
         let ait = CoreIterator::new(
             Index::new(py, index)?,
             initvec,
@@ -103,7 +108,7 @@
 
     def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision,
                 inclusive: bool) -> PyResult<Self> {
-        let initvec = reviter_to_revvec(py, initrevs)?;
+        let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?;
 
         let lazy =
             CoreLazy::new(Index::new(py, index)?, initvec, stoprev, inclusive)