rust-cpython: keep Python<'a> token in PyRefMut
authorYuya Nishihara <yuya@tcha.org>
Sat, 21 Sep 2019 17:05:01 +0900
changeset 43287 0df8312463ae
parent 43286 f8c114f20d2d
child 43288 434d7a3e92e3
rust-cpython: keep Python<'a> token in PyRefMut This just clarifies that the GIL is obtained while PyRefMut is dereferenced, so there's no need of extra acquire_gil() to drop the reference.
rust/hg-cpython/src/ref_sharing.rs
--- a/rust/hg-cpython/src/ref_sharing.rs	Sat Sep 21 17:15:50 2019 +0900
+++ b/rust/hg-cpython/src/ref_sharing.rs	Sat Sep 21 17:05:01 2019 +0900
@@ -205,6 +205,7 @@
 
 /// Holds a mutable reference to data shared between Python and Rust.
 pub struct PyRefMut<'a, T> {
+    py: Python<'a>,
     inner: RefMut<'a, T>,
     py_shared_state: &'a PySharedState,
 }
@@ -213,11 +214,12 @@
     // Must be constructed by PySharedState after checking its leak_count.
     // Otherwise, drop() would incorrectly update the state.
     fn new(
-        _py: Python<'a>,
+        py: Python<'a>,
         inner: RefMut<'a, T>,
         py_shared_state: &'a PySharedState,
     ) -> Self {
         Self {
+            py,
             inner,
             py_shared_state,
         }
@@ -239,10 +241,8 @@
 
 impl<'a, T> Drop for PyRefMut<'a, T> {
     fn drop(&mut self) {
-        let gil = Python::acquire_gil();
-        let py = gil.python();
         unsafe {
-            self.py_shared_state.decrease_leak_count(py, true);
+            self.py_shared_state.decrease_leak_count(self.py, true);
         }
     }
 }