author | Simon Sapin <simon.sapin@octobus.net> |
Thu, 04 Feb 2021 13:16:21 +0100 | |
changeset 46483 | 2845892dd489 |
parent 44431 | b24721e7c5ee |
child 48519 | 35ebe6f824be |
permissions | -rw-r--r-- |
40965
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
1 |
// ancestors.rs |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
2 |
// |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
3 |
// Copyright 2018 Georges Racinet <gracinet@anybox.fr> |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
4 |
// |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
5 |
// This software may be used and distributed according to the terms of the |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
6 |
// GNU General Public License version 2 or any later version. |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
7 |
|
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
8 |
//! Bindings for the `hg::ancestors` module provided by the |
40965
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
9 |
//! `hg-core` crate. From Python, this will be seen as `rustext.ancestor` |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
10 |
//! and can be used as replacement for the the pure `ancestor` Python module. |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
11 |
//! |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
12 |
//! # Classes visible from Python: |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
13 |
//! - [`LazyAncestors`] is the Rust implementation of |
41243
5257e6299d4c
rust-cpython: set conversion for MissingAncestors.bases()
Georges Racinet <georges.racinet@octobus.net>
parents:
41240
diff
changeset
|
14 |
//! `mercurial.ancestor.lazyancestors`. The only difference is that it is |
5257e6299d4c
rust-cpython: set conversion for MissingAncestors.bases()
Georges Racinet <georges.racinet@octobus.net>
parents:
41240
diff
changeset
|
15 |
//! instantiated with a C `parsers.index` instance instead of a parents |
5257e6299d4c
rust-cpython: set conversion for MissingAncestors.bases()
Georges Racinet <georges.racinet@octobus.net>
parents:
41240
diff
changeset
|
16 |
//! function. |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
17 |
//! |
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
18 |
//! - [`MissingAncestors`] is the Rust implementation of |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
19 |
//! `mercurial.ancestor.incrementalmissingancestors`. |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
20 |
//! |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
21 |
//! API differences: |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
22 |
//! + it is instantiated with a C `parsers.index` |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
23 |
//! instance instead of a parents function. |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
24 |
//! + `MissingAncestors.bases` is a method returning a tuple instead of |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
25 |
//! a set-valued attribute. We could return a Python set easily if our |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
26 |
//! [PySet PR](https://github.com/dgrunwald/rust-cpython/pull/165) |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
27 |
//! is accepted. |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
28 |
//! |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
29 |
//! - [`AncestorsIterator`] is the Rust counterpart of the |
41243
5257e6299d4c
rust-cpython: set conversion for MissingAncestors.bases()
Georges Racinet <georges.racinet@octobus.net>
parents:
41240
diff
changeset
|
30 |
//! `ancestor._lazyancestorsiter` Python generator. From Python, instances of |
5257e6299d4c
rust-cpython: set conversion for MissingAncestors.bases()
Georges Racinet <georges.racinet@octobus.net>
parents:
41240
diff
changeset
|
31 |
//! this should be mainly obtained by calling `iter()` on a [`LazyAncestors`] |
5257e6299d4c
rust-cpython: set conversion for MissingAncestors.bases()
Georges Racinet <georges.racinet@octobus.net>
parents:
41240
diff
changeset
|
32 |
//! instance. |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
33 |
//! |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
34 |
//! [`LazyAncestors`]: struct.LazyAncestors.html |
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
35 |
//! [`MissingAncestors`]: struct.MissingAncestors.html |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
36 |
//! [`AncestorsIterator`]: struct.AncestorsIterator.html |
43945
f98f0e3ddaa1
rust-index: add a function to convert PyObject index for hg-core
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43269
diff
changeset
|
37 |
use crate::revlog::pyindex_to_graph; |
42609
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents:
41693
diff
changeset
|
38 |
use crate::{ |
43269
33fe96a5c522
rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents:
42609
diff
changeset
|
39 |
cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError, |
42609
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents:
41693
diff
changeset
|
40 |
}; |
41053
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
41 |
use cpython::{ |
41243
5257e6299d4c
rust-cpython: set conversion for MissingAncestors.bases()
Georges Racinet <georges.racinet@octobus.net>
parents:
41240
diff
changeset
|
42 |
ObjectProtocol, PyClone, PyDict, PyList, PyModule, PyObject, PyResult, |
41693
060c030c9993
rust-cpython: moved py_set() utility to conversion module
Georges Racinet <georges.racinet@octobus.net>
parents:
41246
diff
changeset
|
43 |
Python, PythonObject, ToPyObject, |
41053
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
44 |
}; |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
45 |
use hg::Revision; |
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
46 |
use hg::{ |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
47 |
AncestorsIterator as CoreIterator, LazyAncestors as CoreLazy, |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
48 |
MissingAncestors as CoreMissing, |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
49 |
}; |
41053
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
50 |
use std::cell::RefCell; |
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
51 |
use std::collections::HashSet; |
41053
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
52 |
|
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
53 |
py_class!(pub class AncestorsIterator |py| { |
41053
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
54 |
data inner: RefCell<Box<CoreIterator<Index>>>; |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
55 |
|
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
56 |
def __next__(&self) -> PyResult<Option<Revision>> { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
57 |
match self.inner(py).borrow_mut().next() { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
58 |
Some(Err(e)) => Err(GraphError::pynew(py, e)), |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
59 |
None => Ok(None), |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
60 |
Some(Ok(r)) => Ok(Some(r)), |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
61 |
} |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
62 |
} |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
63 |
|
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
64 |
def __contains__(&self, rev: Revision) -> PyResult<bool> { |
41186
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
65 |
self.inner(py).borrow_mut().contains(rev) |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
66 |
.map_err(|e| GraphError::pynew(py, e)) |
41053
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
67 |
} |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
68 |
|
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
69 |
def __iter__(&self) -> PyResult<Self> { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
70 |
Ok(self.clone_ref(py)) |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
71 |
} |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
72 |
|
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
73 |
def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
74 |
inclusive: bool) -> PyResult<AncestorsIterator> { |
41187
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
75 |
let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; |
41186
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
76 |
let ait = CoreIterator::new( |
43945
f98f0e3ddaa1
rust-index: add a function to convert PyObject index for hg-core
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43269
diff
changeset
|
77 |
pyindex_to_graph(py, index)?, |
41186
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
78 |
initvec, |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
79 |
stoprev, |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
80 |
inclusive, |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
81 |
) |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
82 |
.map_err(|e| GraphError::pynew(py, e))?; |
41053
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
83 |
AncestorsIterator::from_inner(py, ait) |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
84 |
} |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
85 |
|
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
86 |
}); |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
87 |
|
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
88 |
impl AncestorsIterator { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
89 |
pub fn from_inner(py: Python, ait: CoreIterator<Index>) -> PyResult<Self> { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
90 |
Self::create_instance(py, RefCell::new(Box::new(ait))) |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
91 |
} |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
92 |
} |
40965
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
93 |
|
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41114
diff
changeset
|
94 |
py_class!(pub class LazyAncestors |py| { |
41114
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
95 |
data inner: RefCell<Box<CoreLazy<Index>>>; |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
96 |
|
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
97 |
def __contains__(&self, rev: Revision) -> PyResult<bool> { |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
98 |
self.inner(py) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
99 |
.borrow_mut() |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
100 |
.contains(rev) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
101 |
.map_err(|e| GraphError::pynew(py, e)) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
102 |
} |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
103 |
|
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
104 |
def __iter__(&self) -> PyResult<AncestorsIterator> { |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
105 |
AncestorsIterator::from_inner(py, self.inner(py).borrow().iter()) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
106 |
} |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
107 |
|
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
108 |
def __bool__(&self) -> PyResult<bool> { |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
109 |
Ok(!self.inner(py).borrow().is_empty()) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
110 |
} |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
111 |
|
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
112 |
def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
113 |
inclusive: bool) -> PyResult<Self> { |
41187
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
114 |
let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; |
41114
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
115 |
|
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
116 |
let lazy = |
43945
f98f0e3ddaa1
rust-index: add a function to convert PyObject index for hg-core
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43269
diff
changeset
|
117 |
CoreLazy::new(pyindex_to_graph(py, index)?, |
f98f0e3ddaa1
rust-index: add a function to convert PyObject index for hg-core
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43269
diff
changeset
|
118 |
initvec, stoprev, inclusive) |
41114
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
119 |
.map_err(|e| GraphError::pynew(py, e))?; |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
120 |
|
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
121 |
Self::create_instance(py, RefCell::new(Box::new(lazy))) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
122 |
} |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
123 |
|
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
124 |
}); |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
125 |
|
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
126 |
py_class!(pub class MissingAncestors |py| { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
127 |
data inner: RefCell<Box<CoreMissing<Index>>>; |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
128 |
|
44431
b24721e7c5ee
rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents:
43945
diff
changeset
|
129 |
def __new__( |
b24721e7c5ee
rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents:
43945
diff
changeset
|
130 |
_cls, |
b24721e7c5ee
rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents:
43945
diff
changeset
|
131 |
index: PyObject, |
b24721e7c5ee
rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents:
43945
diff
changeset
|
132 |
bases: PyObject |
b24721e7c5ee
rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents:
43945
diff
changeset
|
133 |
) |
b24721e7c5ee
rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents:
43945
diff
changeset
|
134 |
-> PyResult<MissingAncestors> { |
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
135 |
let bases_vec: Vec<Revision> = rev_pyiter_collect(py, &bases)?; |
43945
f98f0e3ddaa1
rust-index: add a function to convert PyObject index for hg-core
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43269
diff
changeset
|
136 |
let inner = CoreMissing::new(pyindex_to_graph(py, index)?, bases_vec); |
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
137 |
MissingAncestors::create_instance(py, RefCell::new(Box::new(inner))) |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
138 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
139 |
|
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
140 |
def hasbases(&self) -> PyResult<bool> { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
141 |
Ok(self.inner(py).borrow().has_bases()) |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
142 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
143 |
|
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
144 |
def addbases(&self, bases: PyObject) -> PyResult<PyObject> { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
145 |
let mut inner = self.inner(py).borrow_mut(); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
146 |
let bases_vec: Vec<Revision> = rev_pyiter_collect(py, &bases)?; |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
147 |
inner.add_bases(bases_vec); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
148 |
// cpython doc has examples with PyResult<()> but this gives me |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
149 |
// the trait `cpython::ToPyObject` is not implemented for `()` |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
150 |
// so let's return an explicit None |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
151 |
Ok(py.None()) |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
152 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
153 |
|
43269
33fe96a5c522
rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents:
42609
diff
changeset
|
154 |
def bases(&self) -> PyResult<HashSet<Revision>> { |
33fe96a5c522
rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents:
42609
diff
changeset
|
155 |
Ok(self.inner(py).borrow().get_bases().clone()) |
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
156 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
157 |
|
43269
33fe96a5c522
rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents:
42609
diff
changeset
|
158 |
def basesheads(&self) -> PyResult<HashSet<Revision>> { |
41246
619ee4039bd4
rust: MissingAncestors.basesheads()
Georges Racinet <georges.racinet@octobus.net>
parents:
41243
diff
changeset
|
159 |
let inner = self.inner(py).borrow(); |
43269
33fe96a5c522
rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents:
42609
diff
changeset
|
160 |
inner.bases_heads().map_err(|e| GraphError::pynew(py, e)) |
41246
619ee4039bd4
rust: MissingAncestors.basesheads()
Georges Racinet <georges.racinet@octobus.net>
parents:
41243
diff
changeset
|
161 |
} |
619ee4039bd4
rust: MissingAncestors.basesheads()
Georges Racinet <georges.racinet@octobus.net>
parents:
41243
diff
changeset
|
162 |
|
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
163 |
def removeancestorsfrom(&self, revs: PyObject) -> PyResult<PyObject> { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
164 |
let mut inner = self.inner(py).borrow_mut(); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
165 |
// this is very lame: we convert to a Rust set, update it in place |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
166 |
// and then convert back to Python, only to have Python remove the |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
167 |
// excess (thankfully, Python is happy with a list or even an iterator) |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
168 |
// Leads to improve this: |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
169 |
// - have the CoreMissing instead do something emit revisions to |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
170 |
// discard |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
171 |
// - define a trait for sets of revisions in the core and implement |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
172 |
// it for a Python set rewrapped with the GIL marker |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
173 |
let mut revs_pyset: HashSet<Revision> = rev_pyiter_collect(py, &revs)?; |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
174 |
inner.remove_ancestors_from(&mut revs_pyset) |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
175 |
.map_err(|e| GraphError::pynew(py, e))?; |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
176 |
|
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
177 |
// convert as Python list |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
178 |
let mut remaining_pyint_vec: Vec<PyObject> = Vec::with_capacity( |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
179 |
revs_pyset.len()); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
180 |
for rev in revs_pyset { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
181 |
remaining_pyint_vec.push(rev.to_py_object(py).into_object()); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
182 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
183 |
let remaining_pylist = PyList::new(py, remaining_pyint_vec.as_slice()); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
184 |
revs.call_method(py, "intersection_update", (remaining_pylist, ), None) |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
185 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
186 |
|
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
187 |
def missingancestors(&self, revs: PyObject) -> PyResult<PyList> { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
188 |
let mut inner = self.inner(py).borrow_mut(); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
189 |
let revs_vec: Vec<Revision> = rev_pyiter_collect(py, &revs)?; |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
190 |
let missing_vec = match inner.missing_ancestors(revs_vec) { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
191 |
Ok(missing) => missing, |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
192 |
Err(e) => { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
193 |
return Err(GraphError::pynew(py, e)); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
194 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
195 |
}; |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
196 |
// convert as Python list |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
197 |
let mut missing_pyint_vec: Vec<PyObject> = Vec::with_capacity( |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
198 |
missing_vec.len()); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
199 |
for rev in missing_vec { |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
200 |
missing_pyint_vec.push(rev.to_py_object(py).into_object()); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
201 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
202 |
Ok(PyList::new(py, missing_pyint_vec.as_slice())) |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
203 |
} |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
204 |
}); |
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
205 |
|
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
206 |
/// Create the module, with __package__ given from parent |
40965
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
207 |
pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
208 |
let dotted_name = &format!("{}.ancestor", package); |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
209 |
let m = PyModule::new(py, dotted_name)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
210 |
m.add(py, "__package__", package)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
211 |
m.add( |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
212 |
py, |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
213 |
"__doc__", |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
214 |
"Generic DAG ancestor algorithms - Rust implementation", |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
215 |
)?; |
41053
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
216 |
m.add_class::<AncestorsIterator>(py)?; |
41114
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41053
diff
changeset
|
217 |
m.add_class::<LazyAncestors>(py)?; |
41188
006c9ce486fa
rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents:
41187
diff
changeset
|
218 |
m.add_class::<MissingAncestors>(py)?; |
40965
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
219 |
|
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
220 |
let sys = PyModule::import(py, "sys")?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
221 |
let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
222 |
sys_modules.set_item(py, dotted_name, &m)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
223 |
// Example C code (see pyexpat.c and import.c) will "give away the |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
224 |
// reference", but we won't because it will be consumed once the |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
225 |
// Rust PyObject is dropped. |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
226 |
Ok(m) |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
227 |
} |