Mercurial > public > mercurial-scm > hg
view rust/hg-cpython/src/filepatterns.rs @ 42752:30320c7bf79f
rust-cpython: add macro for sharing references
Following an experiment done by Georges Racinet, we now have a working way of
sharing references between Python and Rust. This is needed in many points of
the codebase, for example every time we need to expose an iterator to a
Rust-backed Python class.
In a few words, references are (unsafely) marked as `'static` and coupled
with manual reference counting; we are doing manual borrow-checking.
This changes introduces two declarative macro to help reduce boilerplate.
While it is better than not using macros, they are not perfect. They need to:
- Integrate with the garbage collector for container types (not needed
as of yet), as stated in the docstring
- Allow for leaking multiple attributes at the same time
- Inject the `py_shared_state` data attribute in `py_class`-generated
structs
- Automatically namespace the functions and attributes they generate
For at least the last two points, we will need to write a procedural macro
instead of a declarative one.
While this reference-sharing mechanism is being ironed out I thought it best
not to implement it yet.
Lastly, and implementation detail renders our Rust-backed Python iterators too
strict to be proper drop-in replacements, as will be illustrated in a future
patch: if the data structure referenced by a non-depleted iterator is mutated,
an `AlreadyBorrowed` exception is raised, whereas Python would allow it, only
to raise a `RuntimeError` if `next` is called on said iterator. This will have
to be addressed at some point.
Differential Revision: https://phab.mercurial-scm.org/D6631
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Tue, 09 Jul 2019 15:15:54 +0200 |
parents | 0247601869ba |
children | ce6797ef6eab |
line wrap: on
line source
// filepatterns.rs // // Copyright 2019, Georges Racinet <gracinet@anybox.fr>, // Raphaël Gomès <rgomes@octobus.net> // // 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::filepatterns` module provided by the //! `hg-core` crate. From Python, this will be seen as `rustext.filepatterns` //! and can be used as replacement for the the pure `filepatterns` Python module. //! use crate::exceptions::{PatternError, PatternFileError}; use cpython::{ PyBytes, PyDict, PyModule, PyObject, PyResult, PyTuple, Python, ToPyObject, }; use hg::{build_single_regex, read_pattern_file, LineNumber, PatternTuple}; /// Rust does not like functions with different return signatures. /// The 3-tuple version is always returned by the hg-core function, /// the (potential) conversion is handled at this level since it is not likely /// to have any measurable impact on performance. /// /// The Python implementation passes a function reference for `warn` instead /// of a boolean that is used to emit warnings while parsing. The Rust /// implementation chooses to accumulate the warnings and propagate them to /// Python upon completion. See the `readpatternfile` function in `match.py` /// for more details. fn read_pattern_file_wrapper( py: Python, file_path: PyObject, warn: bool, source_info: bool, ) -> PyResult<PyTuple> { match read_pattern_file(file_path.extract::<PyBytes>(py)?.data(py), warn) { Ok((patterns, warnings)) => { if source_info { let itemgetter = |x: &PatternTuple| { (PyBytes::new(py, &x.0), x.1, PyBytes::new(py, &x.2)) }; let results: Vec<(PyBytes, LineNumber, PyBytes)> = patterns.iter().map(itemgetter).collect(); return Ok((results, warnings_to_py_bytes(py, &warnings)) .to_py_object(py)); } let itemgetter = |x: &PatternTuple| PyBytes::new(py, &x.0); let results: Vec<PyBytes> = patterns.iter().map(itemgetter).collect(); Ok( (results, warnings_to_py_bytes(py, &warnings)) .to_py_object(py), ) } Err(e) => Err(PatternFileError::pynew(py, e)), } } fn warnings_to_py_bytes( py: Python, warnings: &[(Vec<u8>, Vec<u8>)], ) -> Vec<(PyBytes, PyBytes)> { warnings .iter() .map(|(path, syn)| (PyBytes::new(py, path), PyBytes::new(py, syn))) .collect() } fn build_single_regex_wrapper( py: Python, kind: PyObject, pat: PyObject, globsuffix: PyObject, ) -> PyResult<PyBytes> { match build_single_regex( kind.extract::<PyBytes>(py)?.data(py), pat.extract::<PyBytes>(py)?.data(py), globsuffix.extract::<PyBytes>(py)?.data(py), ) { Ok(regex) => Ok(PyBytes::new(py, ®ex)), Err(e) => Err(PatternError::pynew(py, e)), } } pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { let dotted_name = &format!("{}.filepatterns", package); let m = PyModule::new(py, dotted_name)?; m.add(py, "__package__", package)?; m.add( py, "__doc__", "Patterns files parsing - Rust implementation", )?; m.add( py, "build_single_regex", py_fn!( py, build_single_regex_wrapper( kind: PyObject, pat: PyObject, globsuffix: PyObject ) ), )?; m.add( py, "read_pattern_file", py_fn!( py, read_pattern_file_wrapper( file_path: PyObject, warn: bool, source_info: bool ) ), )?; m.add(py, "PatternError", py.get_type::<PatternError>())?; let sys = PyModule::import(py, "sys")?; let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?; sys_modules.set_item(py, dotted_name, &m)?; Ok(m) }