rust/hg-cpython/src/conversion.rs
changeset 41704 060c030c9993
parent 41240 ff333620a4cc
child 43269 33fe96a5c522
equal deleted inserted replaced
41703:ee7b7bd432a1 41704:060c030c9993
     6 // GNU General Public License version 2 or any later version.
     6 // GNU General Public License version 2 or any later version.
     7 
     7 
     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
    16 ///
    20 ///
    17 /// We need this in particular to feed to various methods of inner objects
    21 /// We need this in particular to feed to various methods of inner objects
    24 {
    28 {
    25     revs.iter(py)?
    29     revs.iter(py)?
    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 }