Mercurial > hg
comparison rust/hg-cpython/src/ancestors.rs @ 43945:f98f0e3ddaa1
rust-index: add a function to convert PyObject index for hg-core
Function in hg-core need something implementing the `Graph` trait. Right now,
the `hg-cpython` entry points directly turn the PyObject passed as argument
into a `cindex::Index`. However, if we start having the option to use an Index
in Rust, we need to dispatch between the different possible PyObject we could
receive.
So move the "duplicate" call into a unified function. When time come. It will be
easy to update the logic of all interface when the time come.
Differential Revision: https://phab.mercurial-scm.org/D7653
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Fri, 13 Dec 2019 19:59:59 +0100 |
parents | 33fe96a5c522 |
children | b24721e7c5ee |
comparison
equal
deleted
inserted
replaced
43944:8a8305f557d0 | 43945:f98f0e3ddaa1 |
---|---|
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::revlog::pyindex_to_graph; | |
37 use crate::{ | 38 use crate::{ |
38 cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError, | 39 cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError, |
39 }; | 40 }; |
40 use cpython::{ | 41 use cpython::{ |
41 ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult, | 42 ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult, |
71 | 72 |
72 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, | 73 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, |
73 inclusive: bool) -> PyResult<AncestorsIterator> { | 74 inclusive: bool) -> PyResult<AncestorsIterator> { |
74 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; | 75 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; |
75 let ait = CoreIterator::new( | 76 let ait = CoreIterator::new( |
76 Index::new(py, index)?, | 77 pyindex_to_graph(py, index)?, |
77 initvec, | 78 initvec, |
78 stoprev, | 79 stoprev, |
79 inclusive, | 80 inclusive, |
80 ) | 81 ) |
81 .map_err(|e| GraphError::pynew(py, e))?; | 82 .map_err(|e| GraphError::pynew(py, e))?; |
111 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, | 112 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, |
112 inclusive: bool) -> PyResult<Self> { | 113 inclusive: bool) -> PyResult<Self> { |
113 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; | 114 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; |
114 | 115 |
115 let lazy = | 116 let lazy = |
116 CoreLazy::new(Index::new(py, index)?, initvec, stoprev, inclusive) | 117 CoreLazy::new(pyindex_to_graph(py, index)?, |
118 initvec, stoprev, inclusive) | |
117 .map_err(|e| GraphError::pynew(py, e))?; | 119 .map_err(|e| GraphError::pynew(py, e))?; |
118 | 120 |
119 Self::create_instance(py, RefCell::new(Box::new(lazy))) | 121 Self::create_instance(py, RefCell::new(Box::new(lazy))) |
120 } | 122 } |
121 | 123 |
124 py_class!(pub class MissingAncestors |py| { | 126 py_class!(pub class MissingAncestors |py| { |
125 data inner: RefCell<Box<CoreMissing<Index>>>; | 127 data inner: RefCell<Box<CoreMissing<Index>>>; |
126 | 128 |
127 def __new__(_cls, index: PyObject, bases: PyObject) -> PyResult<MissingAncestors> { | 129 def __new__(_cls, index: PyObject, bases: PyObject) -> PyResult<MissingAncestors> { |
128 let bases_vec: Vec<Revision> = rev_pyiter_collect(py, &bases)?; | 130 let bases_vec: Vec<Revision> = rev_pyiter_collect(py, &bases)?; |
129 let inner = CoreMissing::new(Index::new(py, index)?, bases_vec); | 131 let inner = CoreMissing::new(pyindex_to_graph(py, index)?, bases_vec); |
130 MissingAncestors::create_instance(py, RefCell::new(Box::new(inner))) | 132 MissingAncestors::create_instance(py, RefCell::new(Box::new(inner))) |
131 } | 133 } |
132 | 134 |
133 def hasbases(&self) -> PyResult<bool> { | 135 def hasbases(&self) -> PyResult<bool> { |
134 Ok(self.inner(py).borrow().has_bases()) | 136 Ok(self.inner(py).borrow().has_bases()) |