annotate rust/hg-cpython/src/discovery.rs @ 49923:547d6817e0c3

rust-clippy: add `is_empty` method to please the `clippy` gods I can see it being useful anyway.
author Raphaël Gomès <rgomes@octobus.net>
date Mon, 09 Jan 2023 18:27:53 +0100
parents f98f0e3ddaa1
children 4c5f6e95df84
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
1 // discovery.rs
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
2 //
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
3 // Copyright 2018 Georges Racinet <gracinet@anybox.fr>
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
4 //
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
7
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
8 //! Bindings for the `hg::discovery` module provided by the
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
9 //! `hg-core` crate. From Python, this will be seen as `rustext.discovery`
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
10 //!
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
11 //! # Classes visible from Python:
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
12 //! - [`PartialDiscover`] is the Rust implementation of
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
13 //! `mercurial.setdiscovery.partialdiscovery`.
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
14
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents: 42332
diff changeset
15 use crate::{
43269
33fe96a5c522 rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42741
diff changeset
16 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: 42332
diff changeset
17 };
42180
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
18 use cpython::{
42740
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
19 ObjectProtocol, PyDict, PyModule, PyObject, PyResult, PyTuple, Python,
42332
163b8fd7bb72 rust-python3: compatibility fix for integer conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42180
diff changeset
20 PythonObject, ToPyObject,
42180
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
21 };
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
22 use hg::discovery::PartialDiscovery as CorePartialDiscovery;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
23 use hg::Revision;
43269
33fe96a5c522 rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42741
diff changeset
24 use std::collections::HashSet;
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
25
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
26 use std::cell::RefCell;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
27
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
28 use crate::revlog::pyindex_to_graph;
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
29
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
30 py_class!(pub class PartialDiscovery |py| {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
31 data inner: RefCell<Box<CorePartialDiscovery<Index>>>;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
32
42735
5154701a5413 rust-discovery: accept the new 'respectsize' init arg
Georges Racinet <georges.racinet@octobus.net>
parents: 42609
diff changeset
33 // `_respectsize` is currently only here to replicate the Python API and
5154701a5413 rust-discovery: accept the new 'respectsize' init arg
Georges Racinet <georges.racinet@octobus.net>
parents: 42609
diff changeset
34 // will be used in future patches inside methods that are yet to be
5154701a5413 rust-discovery: accept the new 'respectsize' init arg
Georges Racinet <georges.racinet@octobus.net>
parents: 42609
diff changeset
35 // implemented.
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
36 def __new__(
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
37 _cls,
42736
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
38 repo: PyObject,
42735
5154701a5413 rust-discovery: accept the new 'respectsize' init arg
Georges Racinet <georges.racinet@octobus.net>
parents: 42609
diff changeset
39 targetheads: PyObject,
42741
4e7bd6180b53 rust-discovery: optionally don't randomize at all, for tests
Georges Racinet <georges.racinet@octobus.net>
parents: 42740
diff changeset
40 respectsize: bool,
4e7bd6180b53 rust-discovery: optionally don't randomize at all, for tests
Georges Racinet <georges.racinet@octobus.net>
parents: 42740
diff changeset
41 randomize: bool = true
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
42 ) -> PyResult<PartialDiscovery> {
42736
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
43 let index = repo.getattr(py, "changelog")?.getattr(py, "index")?;
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
44 Self::create_instance(
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
45 py,
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
46 RefCell::new(Box::new(CorePartialDiscovery::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
47 pyindex_to_graph(py, index)?,
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
48 rev_pyiter_collect(py, &targetheads)?,
42741
4e7bd6180b53 rust-discovery: optionally don't randomize at all, for tests
Georges Racinet <georges.racinet@octobus.net>
parents: 42740
diff changeset
49 respectsize,
4e7bd6180b53 rust-discovery: optionally don't randomize at all, for tests
Georges Racinet <georges.racinet@octobus.net>
parents: 42740
diff changeset
50 randomize,
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
51 )))
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
52 )
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
53 }
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
54
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
55 def addcommons(&self, commons: PyObject) -> PyResult<PyObject> {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
56 let mut inner = self.inner(py).borrow_mut();
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
57 let commons_vec: Vec<Revision> = rev_pyiter_collect(py, &commons)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
58 inner.add_common_revisions(commons_vec)
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
59 .map_err(|e| GraphError::pynew(py, e))?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
60 Ok(py.None())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
61 }
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
62
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
63 def addmissings(&self, missings: PyObject) -> PyResult<PyObject> {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
64 let mut inner = self.inner(py).borrow_mut();
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
65 let missings_vec: Vec<Revision> = rev_pyiter_collect(py, &missings)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
66 inner.add_missing_revisions(missings_vec)
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
67 .map_err(|e| GraphError::pynew(py, e))?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
68 Ok(py.None())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
69 }
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
70
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
71 def addinfo(&self, sample: PyObject) -> PyResult<PyObject> {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
72 let mut missing: Vec<Revision> = Vec::new();
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
73 let mut common: Vec<Revision> = Vec::new();
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
74 for info in sample.iter(py)? { // info is a pair (Revision, bool)
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
75 let mut revknown = info?.iter(py)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
76 let rev: Revision = revknown.next().unwrap()?.extract(py)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
77 let known: bool = revknown.next().unwrap()?.extract(py)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
78 if known {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
79 common.push(rev);
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
80 } else {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
81 missing.push(rev);
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
82 }
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
83 }
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
84 let mut inner = self.inner(py).borrow_mut();
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
85 inner.add_common_revisions(common)
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
86 .map_err(|e| GraphError::pynew(py, e))?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
87 inner.add_missing_revisions(missing)
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
88 .map_err(|e| GraphError::pynew(py, e))?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
89 Ok(py.None())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
90 }
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
91
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
92 def hasinfo(&self) -> PyResult<bool> {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
93 Ok(self.inner(py).borrow().has_info())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
94 }
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
95
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
96 def iscomplete(&self) -> PyResult<bool> {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
97 Ok(self.inner(py).borrow().is_complete())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
98 }
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
99
42180
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
100 def stats(&self) -> PyResult<PyDict> {
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
101 let stats = self.inner(py).borrow().stats();
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
102 let as_dict: PyDict = PyDict::new(py);
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
103 as_dict.set_item(py, "undecided",
42332
163b8fd7bb72 rust-python3: compatibility fix for integer conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42180
diff changeset
104 stats.undecided.map(
163b8fd7bb72 rust-python3: compatibility fix for integer conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42180
diff changeset
105 |l| l.to_py_object(py).into_object())
163b8fd7bb72 rust-python3: compatibility fix for integer conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42180
diff changeset
106 .unwrap_or_else(|| py.None()))?;
42180
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
107 Ok(as_dict)
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
108 }
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
109
43269
33fe96a5c522 rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42741
diff changeset
110 def commonheads(&self) -> PyResult<HashSet<Revision>> {
33fe96a5c522 rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42741
diff changeset
111 self.inner(py).borrow().common_heads()
33fe96a5c522 rust-cpython: removed now useless py_set() conversion
Georges Racinet <georges.racinet@octobus.net>
parents: 42741
diff changeset
112 .map_err(|e| GraphError::pynew(py, e))
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
113 }
42740
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
114
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
115 def takefullsample(&self, _headrevs: PyObject,
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
116 size: usize) -> PyResult<PyObject> {
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
117 let mut inner = self.inner(py).borrow_mut();
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
118 let sample = inner.take_full_sample(size)
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
119 .map_err(|e| GraphError::pynew(py, e))?;
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
120 let as_vec: Vec<PyObject> = sample
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
121 .iter()
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
122 .map(|rev| rev.to_py_object(py).into_object())
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
123 .collect();
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
124 Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
125 }
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
126
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
127 def takequicksample(&self, headrevs: PyObject,
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
128 size: usize) -> PyResult<PyObject> {
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
129 let mut inner = self.inner(py).borrow_mut();
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
130 let revsvec: Vec<Revision> = rev_pyiter_collect(py, &headrevs)?;
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
131 let sample = inner.take_quick_sample(revsvec, size)
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
132 .map_err(|e| GraphError::pynew(py, e))?;
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
133 let as_vec: Vec<PyObject> = sample
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
134 .iter()
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
135 .map(|rev| rev.to_py_object(py).into_object())
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
136 .collect();
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
137 Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
138 }
1c4b5689bef5 rust-discovery: exposing sampling to python
Georges Racinet <georges.racinet@octobus.net>
parents: 42738
diff changeset
139
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
140 });
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
141
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
142 /// Create the module, with __package__ given from parent
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
143 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
144 let dotted_name = &format!("{}.discovery", package);
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
145 let m = PyModule::new(py, dotted_name)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
146 m.add(py, "__package__", package)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
147 m.add(
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
148 py,
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
149 "__doc__",
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
150 "Discovery of common node sets - Rust implementation",
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
151 )?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
152 m.add_class::<PartialDiscovery>(py)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
153
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
154 let sys = PyModule::import(py, "sys")?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
155 let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
156 sys_modules.set_item(py, dotted_name, &m)?;
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
157 // Example C code (see pyexpat.c and import.c) will "give away the
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
158 // reference", but we won't because it will be consumed once the
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
159 // Rust PyObject is dropped.
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
160 Ok(m)
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
161 }