##// END OF EJS Templates
rust-cpython: moved py_set() utility to conversion module...
Georges Racinet -
r41842:060c030c default
parent child Browse files
Show More
@@ -34,11 +34,11 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 crate::conversion::{py_set, rev_pyiter_collect};
38 use cindex::Index;
38 use cindex::Index;
39 use cpython::{
39 use cpython::{
40 ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult,
40 ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult,
41 PyTuple, Python, PythonObject, ToPyObject,
41 Python, PythonObject, ToPyObject,
42 };
42 };
43 use exceptions::GraphError;
43 use exceptions::GraphError;
44 use hg::Revision;
44 use hg::Revision;
@@ -90,24 +90,6 b' impl AncestorsIterator {'
90 }
90 }
91 }
91 }
92
92
93 /// Copy and convert an `HashSet<Revision>` in a Python set
94 ///
95 /// This will probably turn useless once `PySet` support lands in
96 /// `rust-cpython`.
97 ///
98 /// This builds a Python tuple, then calls Python's "set()" on it
99 fn py_set(py: Python, set: &HashSet<Revision>) -> PyResult<PyObject> {
100 let as_vec: Vec<PyObject> = set
101 .iter()
102 .map(|rev| rev.to_py_object(py).into_object())
103 .collect();
104 let as_pytuple = PyTuple::new(py, as_vec.as_slice());
105
106 let locals = PyDict::new(py);
107 locals.set_item(py, "obj", as_pytuple.to_py_object(py))?;
108 py.eval("set(obj)", None, Some(&locals))
109 }
110
111 py_class!(pub class LazyAncestors |py| {
93 py_class!(pub class LazyAncestors |py| {
112 data inner: RefCell<Box<CoreLazy<Index>>>;
94 data inner: RefCell<Box<CoreLazy<Index>>>;
113
95
@@ -8,8 +8,12 b''
8 //! Bindings for the hg::ancestors module provided by the
8 //! Bindings for the hg::ancestors module provided by the
9 //! `hg-core` crate. From Python, this will be seen as `rustext.ancestor`
9 //! `hg-core` crate. From Python, this will be seen as `rustext.ancestor`
10
10
11 use cpython::{ObjectProtocol, PyObject, PyResult, Python};
11 use cpython::{
12 ObjectProtocol, PyDict, PyObject, PyResult, PyTuple, Python, PythonObject,
13 ToPyObject,
14 };
12 use hg::Revision;
15 use hg::Revision;
16 use std::collections::HashSet;
13 use std::iter::FromIterator;
17 use std::iter::FromIterator;
14
18
15 /// Utility function to convert a Python iterable into various collections
19 /// Utility function to convert a Python iterable into various collections
@@ -26,3 +30,21 b' where'
26 .map(|r| r.and_then(|o| o.extract::<Revision>(py)))
30 .map(|r| r.and_then(|o| o.extract::<Revision>(py)))
27 .collect()
31 .collect()
28 }
32 }
33
34 /// Copy and convert an `HashSet<Revision>` in a Python set
35 ///
36 /// This will probably turn useless once `PySet` support lands in
37 /// `rust-cpython`.
38 ///
39 /// This builds a Python tuple, then calls Python's "set()" on it
40 pub fn py_set(py: Python, set: &HashSet<Revision>) -> PyResult<PyObject> {
41 let as_vec: Vec<PyObject> = set
42 .iter()
43 .map(|rev| rev.to_py_object(py).into_object())
44 .collect();
45 let as_pytuple = PyTuple::new(py, as_vec.as_slice());
46
47 let locals = PyDict::new(py);
48 locals.set_item(py, "obj", as_pytuple.to_py_object(py))?;
49 py.eval("set(obj)", None, Some(&locals))
50 }
General Comments 0
You need to be logged in to leave comments. Login now