Patchwork [06,of,10] rust-cpython: add safe wrapper representing shared data borrowed from PyObject

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 22, 2019, 6:41 a.m.
Message ID <9ac0e8fd9a78a68f5af7.1569134503@mimosa>
Download mbox | patch
Permalink /patch/41725/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 22, 2019, 6:41 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1568533848 -32400
#      Sun Sep 15 16:50:48 2019 +0900
# Node ID 9ac0e8fd9a78a68f5af74f739fbcec15d0434190
# Parent  b941b6d280e6f66e6536df80864eda1e9dfbffce
rust-cpython: add safe wrapper representing shared data borrowed from PyObject

PySharedRef is a tempoary wrapper around PySharedRefCell. It provides safe
functions for each shared data. $shared_accessor implements a safe method
to construct PySharedRefCell.

This allows us to add more than once PySharedRefCell to a Python object.

Patch

diff --git a/rust/hg-cpython/src/dirstate/dirs_multiset.rs b/rust/hg-cpython/src/dirstate/dirs_multiset.rs
--- a/rust/hg-cpython/src/dirstate/dirs_multiset.rs
+++ b/rust/hg-cpython/src/dirstate/dirs_multiset.rs
@@ -107,7 +107,7 @@  py_class!(pub class Dirs |py| {
     }
 });
 
-py_shared_ref!(Dirs, DirsMultiset, inner);
+py_shared_ref!(Dirs, DirsMultiset, inner, inner_shared);
 
 impl Dirs {
     pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> {
diff --git a/rust/hg-cpython/src/dirstate/dirstate_map.rs b/rust/hg-cpython/src/dirstate/dirstate_map.rs
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs
@@ -501,7 +501,7 @@  impl DirstateMap {
     }
 }
 
-py_shared_ref!(DirstateMap, RustDirstateMap, inner);
+py_shared_ref!(DirstateMap, RustDirstateMap, inner, inner_shared);
 
 py_shared_iterator!(
     DirstateMapKeysIterator,
diff --git a/rust/hg-cpython/src/ref_sharing.rs b/rust/hg-cpython/src/ref_sharing.rs
--- a/rust/hg-cpython/src/ref_sharing.rs
+++ b/rust/hg-cpython/src/ref_sharing.rs
@@ -110,7 +110,7 @@  impl PySharedState {
 #[derive(Debug)]
 pub struct PySharedRefCell<T> {
     inner: RefCell<T>,
-    pub py_shared_state: PySharedState, // TODO: remove pub
+    py_shared_state: PySharedState,
 }
 
 impl<T> PySharedRefCell<T> {
@@ -144,6 +144,54 @@  impl<T> PySharedRefCell<T> {
     }
 }
 
+/// Sharable data member of type `T` borrowed from the `PyObject`.
+pub struct PySharedRef<'a, T> {
+    py: Python<'a>,
+    owner: &'a PyObject,
+    data: &'a PySharedRefCell<T>,
+}
+
+impl<'a, T> PySharedRef<'a, T> {
+    /// # Safety
+    ///
+    /// The `data` must be owned by the `owner`. Otherwise, the leak count
+    /// would get wrong.
+    pub unsafe fn new(
+        py: Python<'a>,
+        owner: &'a PyObject,
+        data: &'a PySharedRefCell<T>,
+    ) -> Self {
+        Self { py, owner, data }
+    }
+
+    pub fn borrow(&self) -> Ref<T> {
+        self.data.borrow()
+    }
+
+    pub fn borrow_mut(&self) -> PyResult<PyRefMut<'a, T>> {
+        self.data.borrow_mut(self.py)
+    }
+
+    /// Returns a leaked reference and its management object.
+    ///
+    /// # Safety
+    ///
+    /// It's up to you to make sure that the management object lives
+    /// longer than the leaked reference. Otherwise, you'll get a
+    /// dangling reference.
+    pub unsafe fn leak_immutable(
+        &self,
+    ) -> PyResult<(PyLeakedRef, &'static T)> {
+        let (static_ref, static_state_ref) = self
+            .data
+            .py_shared_state
+            .leak_immutable(self.py, self.data)?;
+        let leak_handle =
+            PyLeakedRef::new(self.py, self.owner, static_state_ref);
+        Ok((leak_handle, static_ref))
+    }
+}
+
 /// Holds a mutable reference to data shared between Python and Rust.
 pub struct PyRefMut<'a, T> {
     inner: RefMut<'a, T>,
@@ -204,6 +252,13 @@  impl<'a, T> Drop for PyRefMut<'a, T> {
 /// * `$inner_struct` is the identifier of the underlying Rust struct
 /// * `$data_member` is the identifier of the data member of `$inner_struct`
 /// that will be shared.
+/// * `$shared_accessor` is the function name to be generated, which allows
+/// safe access to the data member.
+///
+/// # Safety
+///
+/// `$data_member` must persist while the `$name` object is alive. In other
+/// words, it must be an accessor to a data field of the Python object.
 ///
 /// # Example
 ///
@@ -216,47 +271,46 @@  impl<'a, T> Drop for PyRefMut<'a, T> {
 ///     data inner: PySharedRefCell<MyStruct>;
 /// });
 ///
-/// py_shared_ref!(MyType, MyStruct, inner);
+/// py_shared_ref!(MyType, MyStruct, inner, inner_shared);
 /// ```
 macro_rules! py_shared_ref {
     (
         $name: ident,
         $inner_struct: ident,
-        $data_member: ident
+        $data_member: ident,
+        $shared_accessor: ident
     ) => {
         impl $name {
-            // TODO: remove this function in favor of inner(py).borrow_mut()
+            /// Returns a safe reference to the shared `$data_member`.
+            ///
+            /// This function guarantees that `PySharedRef` is created with
+            /// the valid `self` and `self.$data_member(py)` pair.
+            fn $shared_accessor<'a>(
+                &'a self,
+                py: Python<'a>,
+            ) -> $crate::ref_sharing::PySharedRef<'a, $inner_struct> {
+                use cpython::PythonObject;
+                use $crate::ref_sharing::PySharedRef;
+                let owner = self.as_object();
+                let data = self.$data_member(py);
+                unsafe { PySharedRef::new(py, owner, data) }
+            }
+
+            // TODO: remove this function in favor of $shared_accessor(py)
             fn borrow_mut<'a>(
                 &'a self,
                 py: Python<'a>,
             ) -> PyResult<crate::ref_sharing::PyRefMut<'a, $inner_struct>>
             {
-                // assert $data_member type
-                use crate::ref_sharing::PySharedRefCell;
-                let data: &PySharedRefCell<_> = self.$data_member(py);
-                data.borrow_mut(py)
+                self.$shared_accessor(py).borrow_mut()
             }
 
-            /// Returns a leaked reference and its management object.
-            ///
-            /// # Safety
-            ///
-            /// It's up to you to make sure that the management object lives
-            /// longer than the leaked reference. Otherwise, you'll get a
-            /// dangling reference.
+            // TODO: remove this function in favor of $shared_accessor(py)
             unsafe fn leak_immutable<'a>(
                 &'a self,
                 py: Python<'a>,
             ) -> PyResult<(PyLeakedRef, &'static $inner_struct)> {
-                use cpython::PythonObject;
-                // assert $data_member type
-                use crate::ref_sharing::PySharedRefCell;
-                let data: &PySharedRefCell<_> = self.$data_member(py);
-                let (static_ref, static_state_ref) =
-                    data.py_shared_state.leak_immutable(py, data)?;
-                let leak_handle =
-                    PyLeakedRef::new(py, self.as_object(), static_state_ref);
-                Ok((leak_handle, static_ref))
+                self.$shared_accessor(py).leak_immutable()
             }
         }
     };