changeset 52554:64a618048ba8

rust-pyo3: intermediate ProxyIndex extraction Retrieving the `UnsafePyLeaked` without borrowing it will be necessary for the upcoming classes that need to store an inner object derived from it.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Sat, 07 Dec 2024 18:05:47 +0100
parents dd052842fc8e
children 1dd673c1ab3b
files rust/hg-pyo3/src/convert_cpython.rs
diffstat 1 files changed, 12 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-pyo3/src/convert_cpython.rs	Thu Dec 05 15:16:43 2024 +0000
+++ b/rust/hg-pyo3/src/convert_cpython.rs	Sat Dec 07 18:05:47 2024 +0100
@@ -166,17 +166,26 @@
     Ok(unsafe { leaked.map(py, |idx| PySharedIndex { inner: &idx.index }) })
 }
 
+pub(crate) fn proxy_index_py_leak<'py>(
+    index_proxy: &Bound<'py, PyAny>,
+) -> PyResult<(cpython::Python<'py>, cpython::UnsafePyLeaked<PySharedIndex>)> {
+    let (py, idx_proxy) = to_cpython_py_object(index_proxy);
+    let py_leaked = py_rust_index_to_graph(py, idx_proxy)?;
+    Ok((py, py_leaked))
+}
+
 /// Full extraction of the proxy index object as received in PyO3 to a
 /// [`CoreIndex`] reference.
 ///
-/// The safety invariants to maintain are those of the underlying
+/// # Safety
+///
+/// The invariants to maintain are those of the underlying
 /// [`UnsafePyLeaked::try_borrow`]: the caller must not leak the inner
 /// reference.
 pub(crate) unsafe fn proxy_index_extract<'py>(
     index_proxy: &Bound<'py, PyAny>,
 ) -> PyResult<&'py CoreIndex> {
-    let (py, idx_proxy) = to_cpython_py_object(index_proxy);
-    let py_leaked = py_rust_index_to_graph(py, idx_proxy)?;
+    let (py, py_leaked) = proxy_index_py_leak(index_proxy)?;
     let py_shared = &*unsafe {
         py_leaked
             .try_borrow(py)