##// END OF EJS Templates
rust-cpython: make PySharedRef::try_borrow_mut() return BorrowMutError...
Yuya Nishihara -
r44667:b556400b default draft
parent child Browse files
Show More
@@ -40,5 +40,3 b' impl GraphError {'
40 40 }
41 41
42 42 py_exception!(rustext, HgPathPyError, RuntimeError);
43
44 py_exception!(shared_ref, AlreadyBorrowed, RuntimeError);
@@ -22,10 +22,10 b''
22 22
23 23 //! Macros for use in the `hg-cpython` bridge library.
24 24
25 use crate::exceptions::AlreadyBorrowed;
26 25 use cpython::{exc, PyClone, PyErr, PyObject, PyResult, Python};
27 use std::cell::{Ref, RefCell, RefMut};
26 use std::cell::{BorrowMutError, Ref, RefCell, RefMut};
28 27 use std::ops::{Deref, DerefMut};
28 use std::result;
29 29 use std::sync::atomic::{AtomicUsize, Ordering};
30 30
31 31 /// Manages the shared state between Python and Rust
@@ -139,17 +139,14 b' impl<T> PySharedRefCell<T> {'
139 139 fn try_borrow_mut<'a>(
140 140 &'a self,
141 141 py: Python<'a>,
142 ) -> PyResult<RefMut<'a, T>> {
142 ) -> result::Result<RefMut<'a, T>, BorrowMutError> {
143 143 if self.py_shared_state.current_borrow_count(py) > 0 {
144 return Err(AlreadyBorrowed::new(
145 py,
146 "Cannot borrow mutably while immutably borrowed",
147 ));
144 // propagate borrow-by-leaked state to inner to get BorrowMutError
145 let _dummy = self.inner.borrow();
146 self.inner.try_borrow_mut()?;
147 unreachable!("BorrowMutError must be returned");
148 148 }
149 let inner_ref = self
150 .inner
151 .try_borrow_mut()
152 .map_err(|e| AlreadyBorrowed::new(py, e.to_string()))?;
149 let inner_ref = self.inner.try_borrow_mut()?;
153 150 self.py_shared_state.increment_generation(py);
154 151 Ok(inner_ref)
155 152 }
@@ -191,7 +188,9 b" impl<'a, T> PySharedRef<'a, T> {"
191 188
192 189 /// Mutably borrows the wrapped value, returning an error if the value
193 190 /// is currently borrowed.
194 pub fn try_borrow_mut(&self) -> PyResult<RefMut<'a, T>> {
191 pub fn try_borrow_mut(
192 &self,
193 ) -> result::Result<RefMut<'a, T>, BorrowMutError> {
195 194 self.data.try_borrow_mut(self.py)
196 195 }
197 196
General Comments 0
You need to be logged in to leave comments. Login now