author | Georges Racinet <georges.racinet@octobus.net> |
Wed, 20 Feb 2019 09:04:39 +0100 | |
changeset 42179 | 13b64247f48f |
child 42180 | 1b0be75cb61f |
permissions | -rw-r--r-- |
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 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
15 |
use crate::conversion::{py_set, rev_pyiter_collect}; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
16 |
use cindex::Index; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
17 |
use cpython::{ObjectProtocol, PyDict, PyModule, PyObject, PyResult, Python}; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
18 |
use exceptions::GraphError; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
19 |
use hg::discovery::PartialDiscovery as CorePartialDiscovery; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
20 |
use hg::Revision; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
21 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
22 |
use std::cell::RefCell; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
23 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
24 |
py_class!(pub class PartialDiscovery |py| { |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
25 |
data inner: RefCell<Box<CorePartialDiscovery<Index>>>; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
26 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
27 |
def __new__( |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
28 |
_cls, |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
29 |
index: PyObject, |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
30 |
targetheads: PyObject |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
31 |
) -> PyResult<PartialDiscovery> { |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
32 |
Self::create_instance( |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
33 |
py, |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
34 |
RefCell::new(Box::new(CorePartialDiscovery::new( |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
35 |
Index::new(py, index)?, |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
36 |
rev_pyiter_collect(py, &targetheads)?, |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
37 |
))) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
38 |
) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
39 |
} |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
40 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
inner.add_common_revisions(commons_vec) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
45 |
.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
|
46 |
Ok(py.None()) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
47 |
} |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
48 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
49 |
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
|
50 |
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
|
51 |
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
|
52 |
inner.add_missing_revisions(missings_vec) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
53 |
.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
|
54 |
Ok(py.None()) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
55 |
} |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
56 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
let mut revknown = info?.iter(py)?; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
if known { |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
65 |
common.push(rev); |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
66 |
} else { |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
67 |
missing.push(rev); |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
68 |
} |
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 |
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
|
71 |
inner.add_common_revisions(common) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
72 |
.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
|
73 |
inner.add_missing_revisions(missing) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
74 |
.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
|
75 |
Ok(py.None()) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
76 |
} |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
77 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
78 |
def hasinfo(&self) -> PyResult<bool> { |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
79 |
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
|
80 |
} |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
81 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
82 |
def iscomplete(&self) -> PyResult<bool> { |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
83 |
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
|
84 |
} |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
85 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
86 |
def commonheads(&self) -> PyResult<PyObject> { |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
87 |
py_set( |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
88 |
py, |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
89 |
&self.inner(py).borrow().common_heads() |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
90 |
.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
|
91 |
) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
92 |
} |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
93 |
}); |
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 |
/// 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
|
96 |
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
|
97 |
let dotted_name = &format!("{}.discovery", package); |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
98 |
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
|
99 |
m.add(py, "__package__", package)?; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
100 |
m.add( |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
101 |
py, |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
102 |
"__doc__", |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
103 |
"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
|
104 |
)?; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
105 |
m.add_class::<PartialDiscovery>(py)?; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
106 |
|
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
107 |
let sys = PyModule::import(py, "sys")?; |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
// 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
|
111 |
// 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
|
112 |
// Rust PyObject is dropped. |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
113 |
Ok(m) |
13b64247f48f
rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
114 |
} |