# HG changeset patch # User Yuya Nishihara # Date 1570285294 14400 # Node ID 434d7a3e92e3465161572768577c0f9ac4a98ef5 # Parent 0df8312463ae0558bab306a074c8cfcb68ac5099 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. diff -r 0df8312463ae -r 434d7a3e92e3 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, mutably_borrowed: Cell, } @@ -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( + unsafe fn leak_immutable( &self, py: Python, data: &PySharedRefCell, @@ -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 { inner: RefCell, @@ -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> { + fn borrow_mut<'a>(&'a self, py: Python<'a>) -> PyResult> { 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); } }