Show More
@@ -0,0 +1,28 b'' | |||||
|
1 | // conversion.rs | |||
|
2 | // | |||
|
3 | // Copyright 2019 Georges Racinet <georges.racinet@octobus.net> | |||
|
4 | // | |||
|
5 | // This software may be used and distributed according to the terms of the | |||
|
6 | // GNU General Public License version 2 or any later version. | |||
|
7 | ||||
|
8 | //! Bindings for the hg::ancestors module provided by the | |||
|
9 | //! `hg-core` crate. From Python, this will be seen as `rustext.ancestor` | |||
|
10 | ||||
|
11 | use cpython::{ObjectProtocol, PyObject, PyResult, Python}; | |||
|
12 | use hg::Revision; | |||
|
13 | use std::iter::FromIterator; | |||
|
14 | ||||
|
15 | /// Utility function to convert a Python iterable into various collections | |||
|
16 | /// | |||
|
17 | /// We need this in particular to feed to various methods of inner objects | |||
|
18 | /// with `impl IntoIterator<Item=Revision>` arguments, because | |||
|
19 | /// a `PyErr` can arise at each step of iteration, whereas these methods | |||
|
20 | /// expect iterables over `Revision`, not over some `Result<Revision, PyErr>` | |||
|
21 | pub fn rev_pyiter_collect<C>(py: Python, revs: &PyObject) -> PyResult<C> | |||
|
22 | where | |||
|
23 | C: FromIterator<Revision>, | |||
|
24 | { | |||
|
25 | revs.iter(py)? | |||
|
26 | .map(|r| r.and_then(|o| o.extract::<Revision>(py))) | |||
|
27 | .collect() | |||
|
28 | } |
@@ -34,6 +34,7 b'' | |||||
34 | //! [`LazyAncestors`]: struct.LazyAncestors.html |
|
34 | //! [`LazyAncestors`]: struct.LazyAncestors.html | |
35 | //! [`MissingAncestors`]: struct.MissingAncestors.html |
|
35 | //! [`MissingAncestors`]: struct.MissingAncestors.html | |
36 | //! [`AncestorsIterator`]: struct.AncestorsIterator.html |
|
36 | //! [`AncestorsIterator`]: struct.AncestorsIterator.html | |
|
37 | use crate::conversion::rev_pyiter_collect; | |||
37 | use cindex::Index; |
|
38 | use cindex::Index; | |
38 | use cpython::{ |
|
39 | use cpython::{ | |
39 | ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, |
|
40 | ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, | |
@@ -46,24 +47,8 b' use hg::{' | |||||
46 | MissingAncestors as CoreMissing, |
|
47 | MissingAncestors as CoreMissing, | |
47 | }; |
|
48 | }; | |
48 | use std::cell::RefCell; |
|
49 | use std::cell::RefCell; | |
49 | use std::iter::FromIterator; |
|
|||
50 | use std::collections::HashSet; |
|
50 | use std::collections::HashSet; | |
51 |
|
51 | |||
52 | /// Utility function to convert a Python iterable into various collections |
|
|||
53 | /// |
|
|||
54 | /// We need this in particular to feed to various methods of inner objects |
|
|||
55 | /// with `impl IntoIterator<Item=Revision>` arguments, because |
|
|||
56 | /// a `PyErr` can arise at each step of iteration, whereas these methods |
|
|||
57 | /// expect iterables over `Revision`, not over some `Result<Revision, PyErr>` |
|
|||
58 | fn rev_pyiter_collect<C>(py: Python, revs: &PyObject) -> PyResult<C> |
|
|||
59 | where |
|
|||
60 | C: FromIterator<Revision>, |
|
|||
61 | { |
|
|||
62 | revs.iter(py)? |
|
|||
63 | .map(|r| r.and_then(|o| o.extract::<Revision>(py))) |
|
|||
64 | .collect() |
|
|||
65 | } |
|
|||
66 |
|
||||
67 | py_class!(pub class AncestorsIterator |py| { |
|
52 | py_class!(pub class AncestorsIterator |py| { | |
68 | data inner: RefCell<Box<CoreIterator<Index>>>; |
|
53 | data inner: RefCell<Box<CoreIterator<Index>>>; | |
69 |
|
54 |
@@ -26,6 +26,7 b' extern crate libc;' | |||||
26 |
|
26 | |||
27 | pub mod ancestors; |
|
27 | pub mod ancestors; | |
28 | mod cindex; |
|
28 | mod cindex; | |
|
29 | mod conversion; | |||
29 | pub mod exceptions; |
|
30 | pub mod exceptions; | |
30 |
|
31 | |||
31 | py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| { |
|
32 | py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| { |
General Comments 0
You need to be logged in to leave comments.
Login now