changeset 43482:8418b77132c1

rust-cpython: remove useless PyResult<> from leak_immutable() The caller should know if the shared data is mutably borrowed or not.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 05 Oct 2019 09:33:01 -0400
parents 75b4eb98ad97
children 21a1b2094649
files rust/hg-cpython/src/dirstate/dirs_multiset.rs rust/hg-cpython/src/dirstate/dirstate_map.rs rust/hg-cpython/src/ref_sharing.rs
diffstat 3 files changed, 20 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-cpython/src/dirstate/dirs_multiset.rs	Sat Oct 05 09:01:25 2019 -0400
+++ b/rust/hg-cpython/src/dirstate/dirs_multiset.rs	Sat Oct 05 09:33:01 2019 -0400
@@ -92,7 +92,7 @@
             })
     }
     def __iter__(&self) -> PyResult<DirsMultisetKeysIterator> {
-        let leaked_ref = self.inner_shared(py).leak_immutable()?;
+        let leaked_ref = self.inner_shared(py).leak_immutable();
         DirsMultisetKeysIterator::from_inner(
             py,
             unsafe { leaked_ref.map(py, |o| o.iter()) },
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs	Sat Oct 05 09:01:25 2019 -0400
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs	Sat Oct 05 09:33:01 2019 -0400
@@ -304,7 +304,7 @@
     }
 
     def keys(&self) -> PyResult<DirstateMapKeysIterator> {
-        let leaked_ref = self.inner_shared(py).leak_immutable()?;
+        let leaked_ref = self.inner_shared(py).leak_immutable();
         DirstateMapKeysIterator::from_inner(
             py,
             unsafe { leaked_ref.map(py, |o| o.iter()) },
@@ -312,7 +312,7 @@
     }
 
     def items(&self) -> PyResult<DirstateMapItemsIterator> {
-        let leaked_ref = self.inner_shared(py).leak_immutable()?;
+        let leaked_ref = self.inner_shared(py).leak_immutable();
         DirstateMapItemsIterator::from_inner(
             py,
             unsafe { leaked_ref.map(py, |o| o.iter()) },
@@ -320,7 +320,7 @@
     }
 
     def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
-        let leaked_ref = self.inner_shared(py).leak_immutable()?;
+        let leaked_ref = self.inner_shared(py).leak_immutable();
         DirstateMapKeysIterator::from_inner(
             py,
             unsafe { leaked_ref.map(py, |o| o.iter()) },
@@ -437,7 +437,7 @@
     }
 
     def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
-        let leaked_ref = self.inner_shared(py).leak_immutable()?;
+        let leaked_ref = self.inner_shared(py).leak_immutable();
         CopyMapKeysIterator::from_inner(
             py,
             unsafe { leaked_ref.map(py, |o| o.copy_map.iter()) },
@@ -445,7 +445,7 @@
     }
 
     def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
-        let leaked_ref = self.inner_shared(py).leak_immutable()?;
+        let leaked_ref = self.inner_shared(py).leak_immutable();
         CopyMapItemsIterator::from_inner(
             py,
             unsafe { leaked_ref.map(py, |o| o.copy_map.iter()) },
--- a/rust/hg-cpython/src/ref_sharing.rs	Sat Oct 05 09:01:25 2019 -0400
+++ b/rust/hg-cpython/src/ref_sharing.rs	Sat Oct 05 09:33:01 2019 -0400
@@ -89,10 +89,10 @@
         &self,
         _py: Python,
         data: Ref<T>,
-    ) -> PyResult<(&'static T, &'static PySharedState)> {
+    ) -> (&'static T, &'static PySharedState) {
         let ptr: *const T = &*data;
         let state_ptr: *const PySharedState = self;
-        Ok((&*ptr, &*state_ptr))
+        (&*ptr, &*state_ptr)
     }
 
     fn current_borrow_count(&self, _py: Python) -> usize {
@@ -208,20 +208,15 @@
     /// # Panics
     ///
     /// Panics if this is mutably borrowed.
-    pub fn leak_immutable(&self) -> PyResult<PyLeaked<&'static T>> {
+    pub fn leak_immutable(&self) -> PyLeaked<&'static T> {
         let state = &self.data.py_shared_state;
         // make sure self.data isn't mutably borrowed; otherwise the
         // generation number can't be trusted.
         let data_ref = self.borrow();
         unsafe {
             let (static_ref, static_state_ref) =
-                state.leak_immutable(self.py, data_ref)?;
-            Ok(PyLeaked::new(
-                self.py,
-                self.owner,
-                static_ref,
-                static_state_ref,
-            ))
+                state.leak_immutable(self.py, data_ref);
+            PyLeaked::new(self.py, self.owner, static_ref, static_state_ref)
         }
     }
 }
@@ -459,7 +454,7 @@
 ///     data inner: PySharedRefCell<MyStruct>;
 ///
 ///     def __iter__(&self) -> PyResult<MyTypeItemsIterator> {
-///         let leaked_ref = self.inner_shared(py).leak_immutable()?;
+///         let leaked_ref = self.inner_shared(py).leak_immutable();
 ///         MyTypeItemsIterator::from_inner(
 ///             py,
 ///             unsafe { leaked_ref.map(py, |o| o.iter()) },
@@ -551,7 +546,7 @@
     fn test_leaked_borrow() {
         let (gil, owner) = prepare_env();
         let py = gil.python();
-        let leaked = owner.string_shared(py).leak_immutable().unwrap();
+        let leaked = owner.string_shared(py).leak_immutable();
         let leaked_ref = leaked.try_borrow(py).unwrap();
         assert_eq!(*leaked_ref, "new");
     }
@@ -560,7 +555,7 @@
     fn test_leaked_borrow_mut() {
         let (gil, owner) = prepare_env();
         let py = gil.python();
-        let leaked = owner.string_shared(py).leak_immutable().unwrap();
+        let leaked = owner.string_shared(py).leak_immutable();
         let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
         let mut leaked_ref = leaked_iter.try_borrow_mut(py).unwrap();
         assert_eq!(leaked_ref.next(), Some('n'));
@@ -573,7 +568,7 @@
     fn test_leaked_borrow_after_mut() {
         let (gil, owner) = prepare_env();
         let py = gil.python();
-        let leaked = owner.string_shared(py).leak_immutable().unwrap();
+        let leaked = owner.string_shared(py).leak_immutable();
         owner.string_shared(py).borrow_mut().unwrap().clear();
         assert!(leaked.try_borrow(py).is_err());
     }
@@ -582,7 +577,7 @@
     fn test_leaked_borrow_mut_after_mut() {
         let (gil, owner) = prepare_env();
         let py = gil.python();
-        let leaked = owner.string_shared(py).leak_immutable().unwrap();
+        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();
         assert!(leaked_iter.try_borrow_mut(py).is_err());
@@ -593,7 +588,7 @@
     fn test_leaked_map_after_mut() {
         let (gil, owner) = prepare_env();
         let py = gil.python();
-        let leaked = owner.string_shared(py).leak_immutable().unwrap();
+        let leaked = owner.string_shared(py).leak_immutable();
         owner.string_shared(py).borrow_mut().unwrap().clear();
         let _leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
     }
@@ -603,7 +598,7 @@
         let (gil, owner) = prepare_env();
         let py = gil.python();
         assert!(owner.string_shared(py).borrow_mut().is_ok());
-        let leaked = owner.string_shared(py).leak_immutable().unwrap();
+        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());
@@ -621,7 +616,7 @@
         let (gil, owner) = prepare_env();
         let py = gil.python();
         assert!(owner.string_shared(py).borrow_mut().is_ok());
-        let leaked = owner.string_shared(py).leak_immutable().unwrap();
+        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();
@@ -636,6 +631,6 @@
         let (gil, owner) = prepare_env();
         let py = gil.python();
         let _mut_ref = owner.string_shared(py).borrow_mut();
-        let _ = owner.string_shared(py).leak_immutable();
+        owner.string_shared(py).leak_immutable();
     }
 }