Mercurial > public > mercurial-scm > hg
view rust/hg-cpython/src/ancestors.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 | d9f439fcdb4c |
line wrap: on
line source
// ancestors.rs // // Copyright 2018 Georges Racinet <gracinet@anybox.fr> // // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. //! Bindings for the hg::ancestors module provided by the //! `hg-core` crate. From Python, this will be seen as `rustext.ancestor` use cpython::{PyDict, PyModule, PyResult, Python}; /// Create the module, with __package__ given from parent pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { let dotted_name = &format!("{}.ancestor", package); let m = PyModule::new(py, dotted_name)?; m.add(py, "__package__", package)?; m.add( py, "__doc__", "Generic DAG ancestor algorithms - Rust implementation", )?; let sys = PyModule::import(py, "sys")?; let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?; sys_modules.set_item(py, dotted_name, &m)?; // Example C code (see pyexpat.c and import.c) will "give away the // reference", but we won't because it will be consumed once the // Rust PyObject is dropped. Ok(m) }