D5439: rust-cpython: binding for AncestorsIterator

Yuya Nishihara yuya at tcha.org
Sat Dec 15 23:01:58 EST 2018


>  // GNU General Public License version 2 or any later version.
> -
>  //! Bindings for the hg::ancestors module provided by the

Nit: perhaps the empty line was removed by mistake.

> +fn reviter_to_revvec(py: Python, revs: PyObject) -> PyResult<Vec<Revision>> {
> +    let revs_iter = revs.iter(py)?;
> +    // we need to convert to a vector, because Python iterables can
> +    // raise errors at each step.
> +    let cap = match revs.len(py) {
> +        Ok(l) => l,
> +        Err(_) => 0, // unknown
> +    };
> +    let mut initvec: Vec<Revision> = Vec::with_capacity(cap);
> +    for result_revpy in revs_iter {
> +        let as_pyint: PyInt = match result_revpy {
> +            Err(e) => {
> +                return Err(e);
> +            }
> +            Ok(revpy) => revpy.extract(py)?,
> +        };
> +        initvec.push(as_pyint.value(py) as Revision);
> +    }
> +    Ok(initvec)

```
revs_iter.map(|r| r.and_then(|o| o.extract::<Revision>(py))).collect()
```

`PyInt::value()` isn't supported on Python 3, and it should be better to
extract int directly with bounds checking.

https://dgrunwald.github.io/rust-cpython/doc/cpython/struct.PyInt.html#method.value

> +impl AncestorsIterator {
> +    pub fn from_inner(py: Python, ait: CoreIterator<Index>) -> PyResult<Self> {

Nit: I slightly prefer spelling it as `hg::AncestorsIterator`.


More information about the Mercurial-devel mailing list