diff rust/hg-cpython/src/ref_sharing.rs @ 44203:2a24ead003f0

rust-cpython: add panicking version of borrow_mut() and use it The original borrow_mut() is renamed to try_borrow_mut(). Since leak_immutable() no longer incref the borrow count, the caller should know if the underlying value is borrowed or not. No Python world is involved. That's why we can simply use the panicking borrow_mut().
author Yuya Nishihara <yuya@tcha.org>
date Sat, 12 Oct 2019 23:34:05 +0900
parents 4a4c3b9fd91b
children bafdaf4858d8
line wrap: on
line diff
--- a/rust/hg-cpython/src/ref_sharing.rs	Tue Jan 28 22:27:30 2020 -0500
+++ b/rust/hg-cpython/src/ref_sharing.rs	Sat Oct 12 23:34:05 2019 +0900
@@ -202,7 +202,19 @@
         self.data.borrow(self.py)
     }
 
-    pub fn borrow_mut(&self) -> PyResult<RefMut<'a, T>> {
+    /// Mutably borrows the wrapped value.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the value is currently borrowed through `PySharedRef`
+    /// or `PyLeaked`.
+    pub fn borrow_mut(&self) -> RefMut<'a, T> {
+        self.try_borrow_mut().expect("already borrowed")
+    }
+
+    /// Mutably borrows the wrapped value, returning an error if the value
+    /// is currently borrowed.
+    pub fn try_borrow_mut(&self) -> PyResult<RefMut<'a, T>> {
         self.data.try_borrow_mut(self.py)
     }
 
@@ -572,7 +584,7 @@
         let (gil, owner) = prepare_env();
         let py = gil.python();
         let leaked = owner.string_shared(py).leak_immutable();
-        owner.string_shared(py).borrow_mut().unwrap().clear();
+        owner.string_shared(py).borrow_mut().clear();
         assert!(leaked.try_borrow(py).is_err());
     }
 
@@ -582,7 +594,7 @@
         let py = gil.python();
         let leaked = owner.string_shared(py).leak_immutable();
         let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
-        owner.string_shared(py).borrow_mut().unwrap().clear();
+        owner.string_shared(py).borrow_mut().clear();
         assert!(leaked_iter.try_borrow_mut(py).is_err());
     }
 
@@ -592,40 +604,40 @@
         let (gil, owner) = prepare_env();
         let py = gil.python();
         let leaked = owner.string_shared(py).leak_immutable();
-        owner.string_shared(py).borrow_mut().unwrap().clear();
+        owner.string_shared(py).borrow_mut().clear();
         let _leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
     }
 
     #[test]
-    fn test_borrow_mut_while_leaked_ref() {
+    fn test_try_borrow_mut_while_leaked_ref() {
         let (gil, owner) = prepare_env();
         let py = gil.python();
-        assert!(owner.string_shared(py).borrow_mut().is_ok());
+        assert!(owner.string_shared(py).try_borrow_mut().is_ok());
         let leaked = owner.string_shared(py).leak_immutable();
         {
             let _leaked_ref = leaked.try_borrow(py).unwrap();
-            assert!(owner.string_shared(py).borrow_mut().is_err());
+            assert!(owner.string_shared(py).try_borrow_mut().is_err());
             {
                 let _leaked_ref2 = leaked.try_borrow(py).unwrap();
-                assert!(owner.string_shared(py).borrow_mut().is_err());
+                assert!(owner.string_shared(py).try_borrow_mut().is_err());
             }
-            assert!(owner.string_shared(py).borrow_mut().is_err());
+            assert!(owner.string_shared(py).try_borrow_mut().is_err());
         }
-        assert!(owner.string_shared(py).borrow_mut().is_ok());
+        assert!(owner.string_shared(py).try_borrow_mut().is_ok());
     }
 
     #[test]
-    fn test_borrow_mut_while_leaked_ref_mut() {
+    fn test_try_borrow_mut_while_leaked_ref_mut() {
         let (gil, owner) = prepare_env();
         let py = gil.python();
-        assert!(owner.string_shared(py).borrow_mut().is_ok());
+        assert!(owner.string_shared(py).try_borrow_mut().is_ok());
         let leaked = owner.string_shared(py).leak_immutable();
         let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
         {
             let _leaked_ref = leaked_iter.try_borrow_mut(py).unwrap();
-            assert!(owner.string_shared(py).borrow_mut().is_err());
+            assert!(owner.string_shared(py).try_borrow_mut().is_err());
         }
-        assert!(owner.string_shared(py).borrow_mut().is_ok());
+        assert!(owner.string_shared(py).try_borrow_mut().is_ok());
     }
 
     #[test]
@@ -638,10 +650,19 @@
     }
 
     #[test]
+    fn test_try_borrow_mut_while_borrow() {
+        let (gil, owner) = prepare_env();
+        let py = gil.python();
+        let _ref = owner.string_shared(py).borrow();
+        assert!(owner.string_shared(py).try_borrow_mut().is_err());
+    }
+
+    #[test]
+    #[should_panic(expected = "already borrowed")]
     fn test_borrow_mut_while_borrow() {
         let (gil, owner) = prepare_env();
         let py = gil.python();
         let _ref = owner.string_shared(py).borrow();
-        assert!(owner.string_shared(py).borrow_mut().is_err());
+        owner.string_shared(py).borrow_mut();
     }
 }