rust-cpython: make inner functions and structs of ref_sharing private
authorYuya Nishihara <yuya@tcha.org>
Sat, 05 Oct 2019 10:21:34 -0400
changeset 43288 434d7a3e92e3
parent 43287 0df8312463ae
child 43289 8d432d3a2d7c
rust-cpython: make inner functions and structs of ref_sharing private Most of these methods were public because they had to be accessible from macro-generated functions. Some "unsafe" can be removed since we can guarantee the data consistency across non-public operations.
rust/hg-cpython/src/ref_sharing.rs
--- a/rust/hg-cpython/src/ref_sharing.rs	Sat Sep 21 17:05:01 2019 +0900
+++ b/rust/hg-cpython/src/ref_sharing.rs	Sat Oct 05 10:21:34 2019 -0400
@@ -28,7 +28,7 @@
 
 /// Manages the shared state between Python and Rust
 #[derive(Debug, Default)]
-pub struct PySharedState {
+struct PySharedState {
     leak_count: Cell<usize>,
     mutably_borrowed: Cell<bool>,
 }
@@ -38,7 +38,7 @@
 unsafe impl Sync for PySharedState {}
 
 impl PySharedState {
-    pub fn borrow_mut<'a, T>(
+    fn borrow_mut<'a, T>(
         &'a self,
         py: Python<'a>,
         pyrefmut: RefMut<'a, T>,
@@ -82,7 +82,7 @@
     ///
     /// This is highly unsafe since the lifetime of the given data can be
     /// extended. Do not call this function directly.
-    pub unsafe fn leak_immutable<T>(
+    unsafe fn leak_immutable<T>(
         &self,
         py: Python,
         data: &PySharedRefCell<T>,
@@ -104,9 +104,9 @@
 
     /// # Safety
     ///
-    /// It's unsafe to update the reference count without knowing the
-    /// reference is deleted. Do not call this function directly.
-    pub unsafe fn decrease_leak_count(&self, _py: Python, mutable: bool) {
+    /// It's up to you to make sure the reference is about to be deleted
+    /// when updating the leak count.
+    fn decrease_leak_count(&self, _py: Python, mutable: bool) {
         if mutable {
             assert_eq!(self.leak_count.get(), 0);
             assert!(self.mutably_borrowed.get());
@@ -121,7 +121,8 @@
 
 /// `RefCell` wrapper to be safely used in conjunction with `PySharedState`.
 ///
-/// Only immutable operation is allowed through this interface.
+/// This object can be stored in a `py_class!` object as a data field. Any
+/// operation is allowed through the `PySharedRef` interface.
 #[derive(Debug)]
 pub struct PySharedRefCell<T> {
     inner: RefCell<T>,
@@ -136,14 +137,14 @@
         }
     }
 
-    pub fn borrow<'a>(&'a self, _py: Python<'a>) -> Ref<'a, T> {
+    fn borrow<'a>(&'a self, _py: Python<'a>) -> Ref<'a, T> {
         // py_shared_state isn't involved since
         // - inner.borrow() would fail if self is mutably borrowed,
         // - and inner.borrow_mut() would fail while self is borrowed.
         self.inner.borrow()
     }
 
-    pub fn as_ptr(&self) -> *mut T {
+    fn as_ptr(&self) -> *mut T {
         self.inner.as_ptr()
     }
 
@@ -151,10 +152,7 @@
     // inner.try_borrow_mut(). The current implementation panics if
     // self.inner has been borrowed, but returns error if py_shared_state
     // refuses to borrow.
-    pub fn borrow_mut<'a>(
-        &'a self,
-        py: Python<'a>,
-    ) -> PyResult<PyRefMut<'a, T>> {
+    fn borrow_mut<'a>(&'a self, py: Python<'a>) -> PyResult<PyRefMut<'a, T>> {
         self.py_shared_state.borrow_mut(py, self.inner.borrow_mut())
     }
 }
@@ -241,9 +239,7 @@
 
 impl<'a, T> Drop for PyRefMut<'a, T> {
     fn drop(&mut self) {
-        unsafe {
-            self.py_shared_state.decrease_leak_count(self.py, true);
-        }
+        self.py_shared_state.decrease_leak_count(self.py, true);
     }
 }
 
@@ -324,9 +320,7 @@
     /// # Safety
     ///
     /// The `py_shared_state` must be owned by the `inner` Python object.
-    // Marked as unsafe so client code wouldn't construct PyLeakedRef
-    // struct by mistake. Its drop() is unsafe.
-    pub unsafe fn new(
+    fn new(
         py: Python,
         inner: &PyObject,
         data: T,
@@ -391,9 +385,7 @@
         if self.data.is_none() {
             return; // moved to another PyLeakedRef
         }
-        unsafe {
-            self.py_shared_state.decrease_leak_count(py, false);
-        }
+        self.py_shared_state.decrease_leak_count(py, false);
     }
 }