Patchwork [9,of,9] rust-cpython: remove useless PyResult<> from leak_immutable()

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 19, 2019, 10:07 a.m.
Message ID <e0367c78ba38950e0cea.1571479646@mimosa>
Download mbox | patch
Permalink /patch/42502/
State New
Headers show

Comments

Yuya Nishihara - Oct. 19, 2019, 10:07 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1570282381 14400
#      Sat Oct 05 09:33:01 2019 -0400
# Node ID e0367c78ba38950e0cea945ff0f45916ea92286d
# Parent  e325eea5dc005c8032cc577f97fcfc11a8e4b86e
rust-cpython: remove useless PyResult<> from leak_immutable()

The caller should know if the shared data is mutably borrowed or not.

Patch

diff --git a/rust/hg-cpython/src/dirstate/dirs_multiset.rs b/rust/hg-cpython/src/dirstate/dirs_multiset.rs
--- a/rust/hg-cpython/src/dirstate/dirs_multiset.rs
+++ b/rust/hg-cpython/src/dirstate/dirs_multiset.rs
@@ -92,7 +92,7 @@  py_class!(pub class Dirs |py| {
             })
     }
     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()) },
diff --git a/rust/hg-cpython/src/dirstate/dirstate_map.rs b/rust/hg-cpython/src/dirstate/dirstate_map.rs
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs
@@ -304,7 +304,7 @@  py_class!(pub class DirstateMap |py| {
     }
 
     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 @@  py_class!(pub class DirstateMap |py| {
     }
 
     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 @@  py_class!(pub class DirstateMap |py| {
     }
 
     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 @@  py_class!(pub class DirstateMap |py| {
     }
 
     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 @@  py_class!(pub class DirstateMap |py| {
     }
 
     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()) },
diff --git a/rust/hg-cpython/src/ref_sharing.rs b/rust/hg-cpython/src/ref_sharing.rs
--- a/rust/hg-cpython/src/ref_sharing.rs
+++ b/rust/hg-cpython/src/ref_sharing.rs
@@ -89,10 +89,10 @@  impl PySharedState {
         &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 @@  impl<'a, T> PySharedRef<'a, T> {
     /// # 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 @@  impl<T> DerefMut for PyLeakedRefMut<'_, 
 ///     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 @@  mod test {
     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 @@  mod test {
     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 @@  mod test {
     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 @@  mod test {
     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 @@  mod test {
     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 @@  mod test {
         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 @@  mod test {
         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 @@  mod test {
         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();
     }
 }