annotate rust/hg-cpython/src/ancestors.rs @ 50425:9fa3cda7449e

heptapod: add `.gitattributes` file to improve language detection I am fully aware of the irony.
author Raphaël Gomès <rgomes@octobus.net>
date Wed, 05 Apr 2023 16:09:08 +0200
parents 35ebe6f824be
children 4c5f6e95df84
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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 };
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
45 use hg::MissingAncestors as CoreMissing;
41053
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
46 use hg::Revision;
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
47 use std::cell::RefCell;
41188
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
48 use std::collections::HashSet;
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
49 use vcsgraph::lazy_ancestors::{
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
50 AncestorsIterator as VCGAncestorsIterator,
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
51 LazyAncestors as VCGLazyAncestors,
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
52 };
41053
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
53
41184
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 41114
diff changeset
54 py_class!(pub class AncestorsIterator |py| {
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
55 data inner: RefCell<Box<VCGAncestorsIterator<Index>>>;
41053
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
56
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
57 def __next__(&self) -> PyResult<Option<Revision>> {
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
58 match self.inner(py).borrow_mut().next() {
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
59 Some(Err(e)) => Err(GraphError::pynew_from_vcsgraph(py, e)),
41053
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
60 None => Ok(None),
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
61 Some(Ok(r)) => Ok(Some(r)),
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
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
65 def __contains__(&self, rev: Revision) -> PyResult<bool> {
41186
1b4b94bac8a0 rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents: 41185
diff changeset
66 self.inner(py).borrow_mut().contains(rev)
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
67 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e))
41053
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
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
70 def __iter__(&self) -> PyResult<Self> {
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
71 Ok(self.clone_ref(py))
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
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
74 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision,
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
75 inclusive: bool) -> PyResult<AncestorsIterator> {
41187
3bf6979a1785 rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents: 41186
diff changeset
76 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?;
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
77 let ait = VCGAncestorsIterator::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
78 pyindex_to_graph(py, index)?,
41186
1b4b94bac8a0 rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents: 41185
diff changeset
79 initvec,
1b4b94bac8a0 rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents: 41185
diff changeset
80 stoprev,
1b4b94bac8a0 rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents: 41185
diff changeset
81 inclusive,
1b4b94bac8a0 rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents: 41185
diff changeset
82 )
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
83 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e))?;
41053
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
84 AncestorsIterator::from_inner(py, ait)
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
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
89 impl AncestorsIterator {
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
90 pub fn from_inner(
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
91 py: Python,
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
92 ait: VCGAncestorsIterator<Index>,
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
93 ) -> PyResult<Self> {
41053
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
94 Self::create_instance(py, RefCell::new(Box::new(ait)))
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
95 }
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
96 }
40965
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
97
41184
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 41114
diff changeset
98 py_class!(pub class LazyAncestors |py| {
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
99 data inner: RefCell<Box<VCGLazyAncestors<Index>>>;
41114
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
100
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
101 def __contains__(&self, rev: Revision) -> PyResult<bool> {
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
102 self.inner(py)
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
103 .borrow_mut()
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
104 .contains(rev)
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
105 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e))
41114
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 __iter__(&self) -> PyResult<AncestorsIterator> {
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
109 AncestorsIterator::from_inner(py, self.inner(py).borrow().iter())
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 __bool__(&self) -> PyResult<bool> {
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
113 Ok(!self.inner(py).borrow().is_empty())
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
114 }
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 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision,
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
117 inclusive: bool) -> PyResult<Self> {
41187
3bf6979a1785 rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents: 41186
diff changeset
118 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
119
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
120 let lazy =
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
121 VCGLazyAncestors::new(pyindex_to_graph(py, index)?,
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
122 initvec, stoprev, inclusive)
48519
35ebe6f824be hg-cpython: use ancestor iterator impls from vcsgraph
pacien <pacien.trangirard@pacien.net>
parents: 44431
diff changeset
123 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e))?;
41114
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 Self::create_instance(py, RefCell::new(Box::new(lazy)))
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
126 }
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
127
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
128 });
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
129
41188
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
130 py_class!(pub class MissingAncestors |py| {
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
131 data inner: RefCell<Box<CoreMissing<Index>>>;
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
132
44431
b24721e7c5ee rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents: 43945
diff changeset
133 def __new__(
b24721e7c5ee rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents: 43945
diff changeset
134 _cls,
b24721e7c5ee rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents: 43945
diff changeset
135 index: PyObject,
b24721e7c5ee rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents: 43945
diff changeset
136 bases: PyObject
b24721e7c5ee rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents: 43945
diff changeset
137 )
b24721e7c5ee rust-format: cleanup ancestors.rs to make rustfmt happy
Raphaël Gomès <rgomes@octobus.net>
parents: 43945
diff changeset
138 -> PyResult<MissingAncestors> {
41188
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
139 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
140 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
141 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
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 hasbases(&self) -> PyResult<bool> {
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
145 Ok(self.inner(py).borrow().has_bases())
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
146 }
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
147
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
148 def addbases(&self, bases: PyObject) -> PyResult<PyObject> {
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
149 let mut inner = self.inner(py).borrow_mut();
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
150 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
151 inner.add_bases(bases_vec);
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
152 // 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
153 // the trait `cpython::ToPyObject` is not implemented for `()`
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
154 // so let's return an explicit None
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
155 Ok(py.None())
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 bases(&self) -> PyResult<HashSet<Revision>> {
33fe96a5c522 rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42609
diff changeset
159 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
160 }
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
161
43269
33fe96a5c522 rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42609
diff changeset
162 def basesheads(&self) -> PyResult<HashSet<Revision>> {
41246
619ee4039bd4 rust: MissingAncestors.basesheads()
Georges Racinet <georges.racinet@octobus.net>
parents: 41243
diff changeset
163 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
164 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
165 }
619ee4039bd4 rust: MissingAncestors.basesheads()
Georges Racinet <georges.racinet@octobus.net>
parents: 41243
diff changeset
166
41188
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
167 def removeancestorsfrom(&self, revs: PyObject) -> PyResult<PyObject> {
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
168 let mut inner = self.inner(py).borrow_mut();
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
169 // 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
170 // 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
171 // 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
172 // Leads to improve this:
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
173 // - 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
174 // discard
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
175 // - 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
176 // 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
177 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
178 inner.remove_ancestors_from(&mut revs_pyset)
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
179 .map_err(|e| GraphError::pynew(py, e))?;
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
180
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
181 // convert as Python list
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
182 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
183 revs_pyset.len());
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
184 for rev in revs_pyset {
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
185 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
186 }
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
187 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
188 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
189 }
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
190
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
191 def missingancestors(&self, revs: PyObject) -> PyResult<PyList> {
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
192 let mut inner = self.inner(py).borrow_mut();
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
193 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
194 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
195 Ok(missing) => missing,
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
196 Err(e) => {
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
197 return Err(GraphError::pynew(py, e));
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
198 }
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
199 };
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
200 // convert as Python list
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
201 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
202 missing_vec.len());
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
203 for rev in missing_vec {
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
204 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
205 }
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
206 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
207 }
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
208 });
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
209
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
210 /// Create the module, with __package__ given from parent
40965
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
211 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
212 let dotted_name = &format!("{}.ancestor", package);
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
213 let m = PyModule::new(py, dotted_name)?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
214 m.add(py, "__package__", package)?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
215 m.add(
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
216 py,
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
217 "__doc__",
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
218 "Generic DAG ancestor algorithms - Rust implementation",
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
219 )?;
41053
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40965
diff changeset
220 m.add_class::<AncestorsIterator>(py)?;
41114
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41053
diff changeset
221 m.add_class::<LazyAncestors>(py)?;
41188
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
222 m.add_class::<MissingAncestors>(py)?;
40965
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
223
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
224 let sys = PyModule::import(py, "sys")?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
225 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
226 sys_modules.set_item(py, dotted_name, &m)?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
227 // 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
228 // 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
229 // Rust PyObject is dropped.
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
230 Ok(m)
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
231 }