rust/hg-cpython/src/ancestors.rs
changeset 41693 060c030c9993
parent 41246 619ee4039bd4
child 42609 326fdce22fb2
equal deleted inserted replaced
41692:ee7b7bd432a1 41693:060c030c9993
    32 //!   instance.
    32 //!   instance.
    33 //!
    33 //!
    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;
    45 use hg::{
    45 use hg::{
    46     AncestorsIterator as CoreIterator, LazyAncestors as CoreLazy,
    46     AncestorsIterator as CoreIterator, LazyAncestors as CoreLazy,
    86 
    86 
    87 impl AncestorsIterator {
    87 impl AncestorsIterator {
    88     pub fn from_inner(py: Python, ait: CoreIterator<Index>) -> PyResult<Self> {
    88     pub fn from_inner(py: Python, ait: CoreIterator<Index>) -> PyResult<Self> {
    89         Self::create_instance(py, RefCell::new(Box::new(ait)))
    89         Self::create_instance(py, RefCell::new(Box::new(ait)))
    90     }
    90     }
    91 }
       
    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 }
    91 }
   110 
    92 
   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