comparison rust/hg-cpython/src/ancestors.rs @ 48519:35ebe6f824be

hg-cpython: use ancestor iterator impls from vcsgraph Differential Revision: https://phab.mercurial-scm.org/D11947
author pacien <pacien.trangirard@pacien.net>
date Tue, 16 Nov 2021 18:54:05 +0100
parents b24721e7c5ee
children 4c5f6e95df84
comparison
equal deleted inserted replaced
48518:8e8737a1fa7d 48519:35ebe6f824be
40 }; 40 };
41 use cpython::{ 41 use cpython::{
42 ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult, 42 ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult,
43 Python, PythonObject, ToPyObject, 43 Python, PythonObject, ToPyObject,
44 }; 44 };
45 use hg::MissingAncestors as CoreMissing;
45 use hg::Revision; 46 use hg::Revision;
46 use hg::{
47 AncestorsIterator as CoreIterator, LazyAncestors as CoreLazy,
48 MissingAncestors as CoreMissing,
49 };
50 use std::cell::RefCell; 47 use std::cell::RefCell;
51 use std::collections::HashSet; 48 use std::collections::HashSet;
49 use vcsgraph::lazy_ancestors::{
50 AncestorsIterator as VCGAncestorsIterator,
51 LazyAncestors as VCGLazyAncestors,
52 };
52 53
53 py_class!(pub class AncestorsIterator |py| { 54 py_class!(pub class AncestorsIterator |py| {
54 data inner: RefCell<Box<CoreIterator<Index>>>; 55 data inner: RefCell<Box<VCGAncestorsIterator<Index>>>;
55 56
56 def __next__(&self) -> PyResult<Option<Revision>> { 57 def __next__(&self) -> PyResult<Option<Revision>> {
57 match self.inner(py).borrow_mut().next() { 58 match self.inner(py).borrow_mut().next() {
58 Some(Err(e)) => Err(GraphError::pynew(py, e)), 59 Some(Err(e)) => Err(GraphError::pynew_from_vcsgraph(py, e)),
59 None => Ok(None), 60 None => Ok(None),
60 Some(Ok(r)) => Ok(Some(r)), 61 Some(Ok(r)) => Ok(Some(r)),
61 } 62 }
62 } 63 }
63 64
64 def __contains__(&self, rev: Revision) -> PyResult<bool> { 65 def __contains__(&self, rev: Revision) -> PyResult<bool> {
65 self.inner(py).borrow_mut().contains(rev) 66 self.inner(py).borrow_mut().contains(rev)
66 .map_err(|e| GraphError::pynew(py, e)) 67 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e))
67 } 68 }
68 69
69 def __iter__(&self) -> PyResult<Self> { 70 def __iter__(&self) -> PyResult<Self> {
70 Ok(self.clone_ref(py)) 71 Ok(self.clone_ref(py))
71 } 72 }
72 73
73 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, 74 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision,
74 inclusive: bool) -> PyResult<AncestorsIterator> { 75 inclusive: bool) -> PyResult<AncestorsIterator> {
75 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; 76 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?;
76 let ait = CoreIterator::new( 77 let ait = VCGAncestorsIterator::new(
77 pyindex_to_graph(py, index)?, 78 pyindex_to_graph(py, index)?,
78 initvec, 79 initvec,
79 stoprev, 80 stoprev,
80 inclusive, 81 inclusive,
81 ) 82 )
82 .map_err(|e| GraphError::pynew(py, e))?; 83 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e))?;
83 AncestorsIterator::from_inner(py, ait) 84 AncestorsIterator::from_inner(py, ait)
84 } 85 }
85 86
86 }); 87 });
87 88
88 impl AncestorsIterator { 89 impl AncestorsIterator {
89 pub fn from_inner(py: Python, ait: CoreIterator<Index>) -> PyResult<Self> { 90 pub fn from_inner(
91 py: Python,
92 ait: VCGAncestorsIterator<Index>,
93 ) -> PyResult<Self> {
90 Self::create_instance(py, RefCell::new(Box::new(ait))) 94 Self::create_instance(py, RefCell::new(Box::new(ait)))
91 } 95 }
92 } 96 }
93 97
94 py_class!(pub class LazyAncestors |py| { 98 py_class!(pub class LazyAncestors |py| {
95 data inner: RefCell<Box<CoreLazy<Index>>>; 99 data inner: RefCell<Box<VCGLazyAncestors<Index>>>;
96 100
97 def __contains__(&self, rev: Revision) -> PyResult<bool> { 101 def __contains__(&self, rev: Revision) -> PyResult<bool> {
98 self.inner(py) 102 self.inner(py)
99 .borrow_mut() 103 .borrow_mut()
100 .contains(rev) 104 .contains(rev)
101 .map_err(|e| GraphError::pynew(py, e)) 105 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e))
102 } 106 }
103 107
104 def __iter__(&self) -> PyResult<AncestorsIterator> { 108 def __iter__(&self) -> PyResult<AncestorsIterator> {
105 AncestorsIterator::from_inner(py, self.inner(py).borrow().iter()) 109 AncestorsIterator::from_inner(py, self.inner(py).borrow().iter())
106 } 110 }
112 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, 116 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision,
113 inclusive: bool) -> PyResult<Self> { 117 inclusive: bool) -> PyResult<Self> {
114 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; 118 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?;
115 119
116 let lazy = 120 let lazy =
117 CoreLazy::new(pyindex_to_graph(py, index)?, 121 VCGLazyAncestors::new(pyindex_to_graph(py, index)?,
118 initvec, stoprev, inclusive) 122 initvec, stoprev, inclusive)
119 .map_err(|e| GraphError::pynew(py, e))?; 123 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e))?;
120 124
121 Self::create_instance(py, RefCell::new(Box::new(lazy))) 125 Self::create_instance(py, RefCell::new(Box::new(lazy)))
122 } 126 }
123 127
124 }); 128 });