Mercurial > public > mercurial-scm > hg
annotate rust/hg-cpython/src/lib.rs @ 41052:4c25038c112c
rust-cpython: implement Graph using C parents function
We introduce the `Index` struct that wraps the C index. It is
not intrinsically protected by the GIL (see the lengthy
discussion in its docstring). Improving on this seems
prematurate at this point.
A pointer to the parents function is stored on the parsers
C extension module as a capsule object.
This is the recommended way to export a C API for consumption
from other extensions.
See also: https://docs.python.org/2.7/c-api/capsule.html
In our case, we use it in cindex.rs, retrieving function
pointer from the capsule and storing it within the CIndex
struct, alongside with a pointer to the index. From there,
the implementation is very close to the one from hg-direct-ffi.
The naming convention for the capsule is inspired from the
one in datetime:
>>> import datetime
>>> datetime.datetime_CAPI
<capsule object "datetime.datetime_CAPI" at 0x7fb51201ecf0>
although in datetime's case, the capsule points to a struct holding
several type objects and methods.
Differential Revision: https://phab.mercurial-scm.org/D5438
author | Georges Racinet <gracinet@anybox.fr> |
---|---|
date | Mon, 03 Dec 2018 07:44:08 +0100 |
parents | 5532823e8c18 |
children | d43719bd01f0 |
rev | line source |
---|---|
40965
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
1 // lib.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 |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
8 //! Python bindings of `hg-core` objects using the `cpython` crate. |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
9 //! Once compiled, the resulting single shared library object can be placed in |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
10 //! the `mercurial` package directly as `rustext.so` or `rustext.dll`. |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
11 //! It holds several modules, so that from the point of view of Python, |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
12 //! it behaves as the `cext` package. |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
13 //! |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
14 //! Example: |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
15 //! ``` |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
16 //! >>> from mercurial.rustext import ancestor |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
17 //! >>> ancestor.__doc__ |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
18 //! 'Generic DAG ancestor algorithms - Rust implementation' |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
19 //! ``` |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
20 |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
21 #[macro_use] |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
22 extern crate cpython; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
23 extern crate hg; |
41052
4c25038c112c
rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
24 extern crate libc; |
40965
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
25 |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
26 mod ancestors; |
41052
4c25038c112c
rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
40965
diff
changeset
|
27 mod cindex; |
40965
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
28 mod exceptions; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
29 |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
30 py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| { |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
31 m.add( |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
32 py, |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
33 "__doc__", |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
34 "Mercurial core concepts - Rust implementation", |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
35 )?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
36 |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
37 let dotted_name: String = m.get(py, "__name__")?.extract(py)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
38 m.add(py, "__package__", "mercurial")?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
39 m.add(py, "ancestor", ancestors::init_module(py, &dotted_name)?)?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
40 m.add(py, "GraphError", py.get_type::<exceptions::GraphError>())?; |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
41 Ok(()) |
5532823e8c18
rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff
changeset
|
42 }); |