rust/hg-cpython/src/ref_sharing.rs
changeset 43287 0df8312463ae
parent 43286 f8c114f20d2d
child 43288 434d7a3e92e3
equal deleted inserted replaced
43286:f8c114f20d2d 43287:0df8312463ae
   203     }
   203     }
   204 }
   204 }
   205 
   205 
   206 /// Holds a mutable reference to data shared between Python and Rust.
   206 /// Holds a mutable reference to data shared between Python and Rust.
   207 pub struct PyRefMut<'a, T> {
   207 pub struct PyRefMut<'a, T> {
       
   208     py: Python<'a>,
   208     inner: RefMut<'a, T>,
   209     inner: RefMut<'a, T>,
   209     py_shared_state: &'a PySharedState,
   210     py_shared_state: &'a PySharedState,
   210 }
   211 }
   211 
   212 
   212 impl<'a, T> PyRefMut<'a, T> {
   213 impl<'a, T> PyRefMut<'a, T> {
   213     // Must be constructed by PySharedState after checking its leak_count.
   214     // Must be constructed by PySharedState after checking its leak_count.
   214     // Otherwise, drop() would incorrectly update the state.
   215     // Otherwise, drop() would incorrectly update the state.
   215     fn new(
   216     fn new(
   216         _py: Python<'a>,
   217         py: Python<'a>,
   217         inner: RefMut<'a, T>,
   218         inner: RefMut<'a, T>,
   218         py_shared_state: &'a PySharedState,
   219         py_shared_state: &'a PySharedState,
   219     ) -> Self {
   220     ) -> Self {
   220         Self {
   221         Self {
       
   222             py,
   221             inner,
   223             inner,
   222             py_shared_state,
   224             py_shared_state,
   223         }
   225         }
   224     }
   226     }
   225 }
   227 }
   237     }
   239     }
   238 }
   240 }
   239 
   241 
   240 impl<'a, T> Drop for PyRefMut<'a, T> {
   242 impl<'a, T> Drop for PyRefMut<'a, T> {
   241     fn drop(&mut self) {
   243     fn drop(&mut self) {
   242         let gil = Python::acquire_gil();
       
   243         let py = gil.python();
       
   244         unsafe {
   244         unsafe {
   245             self.py_shared_state.decrease_leak_count(py, true);
   245             self.py_shared_state.decrease_leak_count(self.py, true);
   246         }
   246         }
   247     }
   247     }
   248 }
   248 }
   249 
   249 
   250 /// Allows a `py_class!` generated struct to share references to one of its
   250 /// Allows a `py_class!` generated struct to share references to one of its