Mercurial > public > mercurial-scm > hg
comparison rust/hg-cpython/src/revlog.rs @ 52411:c2480ac4c5e2
rust-pyo3: retrieving the InnerRevlog of hg-cpython
This allows PyO3-based code to use the InnerRevlog, access its shared data
(core InnerRevlog), which will then allow, e.g., to retrieve references on
the core Index.
On the `hg-cpython` (`rusthg` crate, `rustext` Python extension module),
we had to also build as a Rust library, and open up some accesses (see
notably the public accessor for `inner`, the core `InnerRevlog`).
Retrieving the Rust struct underlying a Python object defined by another
extension module written in Rust is tricky because the Python type objects
are duplicated in the extension modules, leading to failure of the normal
type checking. See the doc-comment of `convert_cpython::extract_inner_revlog`
for a complete explanation.
To solve this, we import the Python type object of `rustext` (defined
by `hg-cpython`) and perform a manual check. Checking the Python type is
necessary, as PyO3 documentation clearly state that downcasting an object
that has not the proper type is Undefined Behaviour.
At this point, we do not have conversion facilities for exceptions (`PyErr`
on both sides), hence the remaining unwraps).
author | Georges Racinet <georges.racinet@cloudcrane.io> |
---|---|
date | Sat, 30 Nov 2024 20:57:02 +0100 |
parents | bd8081e9fd62 |
children | 2fb13c3f4496 |
comparison
equal
deleted
inserted
replaced
52410:15011324a80b | 52411:c2480ac4c5e2 |
---|---|
14 }; | 14 }; |
15 use cpython::{ | 15 use cpython::{ |
16 buffer::{Element, PyBuffer}, | 16 buffer::{Element, PyBuffer}, |
17 exc::{IndexError, ValueError}, | 17 exc::{IndexError, ValueError}, |
18 ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyInt, PyList, | 18 ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyInt, PyList, |
19 PyModule, PyObject, PyResult, PySet, PyTuple, PyType, Python, | 19 PyModule, PyObject, PyResult, PySet, PySharedRef, PyTuple, PyType, Python, |
20 PythonObject, ToPyObject, UnsafePyLeaked, | 20 PythonObject, ToPyObject, UnsafePyLeaked, |
21 }; | 21 }; |
22 use hg::{ | 22 use hg::{ |
23 errors::HgError, | 23 errors::HgError, |
24 fncache::FnCache, | 24 fncache::FnCache, |
49 }; | 49 }; |
50 use vcsgraph::graph::Graph as VCSGraph; | 50 use vcsgraph::graph::Graph as VCSGraph; |
51 | 51 |
52 pub struct PySharedIndex { | 52 pub struct PySharedIndex { |
53 /// The underlying hg-core index | 53 /// The underlying hg-core index |
54 pub(crate) inner: &'static Index, | 54 pub inner: &'static Index, |
55 } | 55 } |
56 | 56 |
57 /// Return a Struct implementing the Graph trait | 57 /// Return a Struct implementing the Graph trait |
58 pub(crate) fn py_rust_index_to_graph( | 58 pub(crate) fn py_rust_index_to_graph( |
59 py: Python, | 59 py: Python, |
1486 | 1486 |
1487 }); | 1487 }); |
1488 | 1488 |
1489 /// Forwarded index methods? | 1489 /// Forwarded index methods? |
1490 impl InnerRevlog { | 1490 impl InnerRevlog { |
1491 pub fn pub_inner<'p, 'a: 'p>( | |
1492 &'a self, | |
1493 py: Python<'p>, | |
1494 ) -> PySharedRef<'p, CoreInnerRevlog> { | |
1495 self.inner(py) | |
1496 } | |
1497 | |
1491 fn len(&self, py: Python) -> PyResult<usize> { | 1498 fn len(&self, py: Python) -> PyResult<usize> { |
1492 let rust_index_len = self.inner(py).borrow().index.len(); | 1499 let rust_index_len = self.inner(py).borrow().index.len(); |
1493 Ok(rust_index_len) | 1500 Ok(rust_index_len) |
1494 } | 1501 } |
1495 /// This is scaffolding at this point, but it could also become | 1502 /// This is scaffolding at this point, but it could also become |