changeset 44074:0df8312463ae

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.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 21 Sep 2019 17:05:01 +0900
parents f8c114f20d2d
children 434d7a3e92e3
files rust/hg-cpython/src/ref_sharing.rs
diffstat 1 files changed, 4 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-cpython/src/ref_sharing.rs
+++ b/rust/hg-cpython/src/ref_sharing.rs
@@ -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);
         }
     }
 }