Mercurial > hg-stable
annotate rust/hg-cpython/src/ancestors.rs @ 41187:3bf6979a1785
rust-cpython: generalised conversion function
Because `hg::ancestors::MissingAncestors` has methods taking
some `HashSet<Revision>` besides `impl IntoIterator<Item = Revision>`
as parameters we'll need the more generic `rev_pyiter_collect()` function
to also build these
Differential Revision: https://phab.mercurial-scm.org/D5549
author | Georges Racinet <georges.racinet@octobus.net> |
---|---|
date | Wed, 09 Jan 2019 17:31:36 +0100 |
parents | 1b4b94bac8a0 |
children | 006c9ce486fa |
rev | line source |
---|---|
40978
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:
41117
diff
changeset
|
8 //! Bindings for the `hg::ancestors` module provided by the |
40978
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:
41117
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:
41117
diff
changeset
|
11 //! |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
12 //! # Classes visible from Python: |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
13 //! - [`LazyAncestors`] is the Rust implementation of |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
14 //! `mercurial.ancestor.lazyancestors`. |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
15 //! The only difference is that it is instantiated with a C `parsers.index` |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
16 //! instance instead of a parents function. |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
17 //! |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
18 //! - [`AncestorsIterator`] is the Rust counterpart of the |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
19 //! `ancestor._lazyancestorsiter` Python generator. |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
20 //! From Python, instances of this should be mainly obtained by calling |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
21 //! `iter()` on a [`LazyAncestors`] instance. |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
22 //! |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
23 //! [`LazyAncestors`]: struct.LazyAncestors.html |
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
24 //! [`AncestorsIterator`]: struct.AncestorsIterator.html |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
25 use cindex::Index; |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
26 use cpython::{ |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
27 ObjectProtocol, PyClone, PyDict, PyModule, PyObject, PyResult, Python, |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
28 }; |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
29 use exceptions::GraphError; |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
30 use hg::Revision; |
41117
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
31 use hg::{AncestorsIterator as CoreIterator, LazyAncestors as CoreLazy}; |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
32 use std::cell::RefCell; |
41187
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
33 use std::iter::FromIterator; |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
34 |
41187
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
35 /// Utility function to convert a Python iterable into various collections |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
36 /// |
41187
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
37 /// We need this in particular to feed to various methods of inner objects |
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
38 /// with `impl IntoIterator<Item=Revision>` arguments, because |
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
39 /// a `PyErr` can arise at each step of iteration, whereas these methods |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
40 /// expect iterables over `Revision`, not over some `Result<Revision, PyErr>` |
41187
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
41 fn rev_pyiter_collect<C>(py: Python, revs: &PyObject) -> PyResult<C> |
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
42 where |
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
43 C: FromIterator<Revision>, |
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
44 { |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
45 revs.iter(py)? |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
46 .map(|r| r.and_then(|o| o.extract::<Revision>(py))) |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
47 .collect() |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
48 } |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
49 |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
50 py_class!(pub class AncestorsIterator |py| { |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
51 data inner: RefCell<Box<CoreIterator<Index>>>; |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
52 |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
53 def __next__(&self) -> PyResult<Option<Revision>> { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
54 match self.inner(py).borrow_mut().next() { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
55 Some(Err(e)) => Err(GraphError::pynew(py, e)), |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
56 None => Ok(None), |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
57 Some(Ok(r)) => Ok(Some(r)), |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
58 } |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
59 } |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
60 |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
61 def __contains__(&self, rev: Revision) -> PyResult<bool> { |
41186
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
62 self.inner(py).borrow_mut().contains(rev) |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
63 .map_err(|e| GraphError::pynew(py, e)) |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
64 } |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
65 |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
66 def __iter__(&self) -> PyResult<Self> { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
67 Ok(self.clone_ref(py)) |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
68 } |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
69 |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
70 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
71 inclusive: bool) -> PyResult<AncestorsIterator> { |
41187
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
72 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
|
73 let ait = CoreIterator::new( |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
74 Index::new(py, index)?, |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
75 initvec, |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
76 stoprev, |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
77 inclusive, |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
78 ) |
1b4b94bac8a0
rust-cpython: style consistency leftovers
Georges Racinet <georges.racinet@octobus.net>
parents:
41185
diff
changeset
|
79 .map_err(|e| GraphError::pynew(py, e))?; |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
80 AncestorsIterator::from_inner(py, ait) |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
81 } |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
82 |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
83 }); |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
84 |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
85 impl AncestorsIterator { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
86 pub fn from_inner(py: Python, ait: CoreIterator<Index>) -> PyResult<Self> { |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
87 Self::create_instance(py, RefCell::new(Box::new(ait))) |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
88 } |
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
89 } |
40978
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
90 |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
91 py_class!(pub class LazyAncestors |py| { |
41117
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
92 data inner: RefCell<Box<CoreLazy<Index>>>; |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
93 |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
94 def __contains__(&self, rev: Revision) -> PyResult<bool> { |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
95 self.inner(py) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
96 .borrow_mut() |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
97 .contains(rev) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
98 .map_err(|e| GraphError::pynew(py, e)) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
99 } |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
100 |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
101 def __iter__(&self) -> PyResult<AncestorsIterator> { |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
102 AncestorsIterator::from_inner(py, self.inner(py).borrow().iter()) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
103 } |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
104 |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
105 def __bool__(&self) -> PyResult<bool> { |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
106 Ok(!self.inner(py).borrow().is_empty()) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
107 } |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
108 |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
109 def __new__(_cls, index: PyObject, initrevs: PyObject, stoprev: Revision, |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
110 inclusive: bool) -> PyResult<Self> { |
41187
3bf6979a1785
rust-cpython: generalised conversion function
Georges Racinet <georges.racinet@octobus.net>
parents:
41186
diff
changeset
|
111 let initvec: Vec<Revision> = rev_pyiter_collect(py, &initrevs)?; |
41117
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
112 |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
113 let lazy = |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
114 CoreLazy::new(Index::new(py, index)?, initvec, stoprev, inclusive) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
115 .map_err(|e| GraphError::pynew(py, e))?; |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
116 |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
117 Self::create_instance(py, RefCell::new(Box::new(lazy))) |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
118 } |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
119 |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
120 }); |
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
121 |
41184
dcf818267bc1
rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents:
41117
diff
changeset
|
122 /// Create the module, with `__package__` given from parent |
40978
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
123 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
|
124 let dotted_name = &format!("{}.ancestor", package); |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
125 let m = PyModule::new(py, dotted_name)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
126 m.add(py, "__package__", package)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
127 m.add( |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
128 py, |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
129 "__doc__", |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
130 "Generic DAG ancestor algorithms - Rust implementation", |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
131 )?; |
41056
d9f439fcdb4c
rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents:
40978
diff
changeset
|
132 m.add_class::<AncestorsIterator>(py)?; |
41117
b31a41f24864
rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents:
41056
diff
changeset
|
133 m.add_class::<LazyAncestors>(py)?; |
40978
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
134 |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
135 let sys = PyModule::import(py, "sys")?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
136 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
|
137 sys_modules.set_item(py, dotted_name, &m)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
138 // 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
|
139 // 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
|
140 // Rust PyObject is dropped. |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
141 Ok(m) |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
142 } |