rust/hg-cpython/src/ref_sharing.rs
changeset 43426 6f9f15a476a4
parent 43425 ed50f2c31a4c
child 43427 b7ab3a0a9e57
equal deleted inserted replaced
43425:ed50f2c31a4c 43426:6f9f15a476a4
   568         $leaked: ty,
   568         $leaked: ty,
   569         $success_func: expr,
   569         $success_func: expr,
   570         $success_type: ty
   570         $success_type: ty
   571     ) => {
   571     ) => {
   572         py_class!(pub class $name |py| {
   572         py_class!(pub class $name |py| {
   573             data inner: RefCell<Option<$leaked>>;
   573             data inner: RefCell<$leaked>;
   574 
   574 
   575             def __next__(&self) -> PyResult<$success_type> {
   575             def __next__(&self) -> PyResult<$success_type> {
   576                 let mut inner_opt = self.inner(py).borrow_mut();
   576                 let mut leaked = self.inner(py).borrow_mut();
   577                 if let Some(leaked) = inner_opt.as_mut() {
   577                 let mut iter = leaked.try_borrow_mut(py)?;
   578                     let mut iter = leaked.try_borrow_mut(py)?;
   578                 match iter.next() {
   579                     match iter.next() {
   579                     None => Ok(None),
   580                         None => {
   580                     Some(res) => $success_func(py, res),
   581                             drop(iter);
       
   582                             // replace Some(inner) by None, drop $leaked
       
   583                             inner_opt.take();
       
   584                             Ok(None)
       
   585                         }
       
   586                         Some(res) => {
       
   587                             $success_func(py, res)
       
   588                         }
       
   589                     }
       
   590                 } else {
       
   591                     Ok(None)
       
   592                 }
   581                 }
   593             }
   582             }
   594 
   583 
   595             def __iter__(&self) -> PyResult<Self> {
   584             def __iter__(&self) -> PyResult<Self> {
   596                 Ok(self.clone_ref(py))
   585                 Ok(self.clone_ref(py))
   602                 py: Python,
   591                 py: Python,
   603                 leaked: $leaked,
   592                 leaked: $leaked,
   604             ) -> PyResult<Self> {
   593             ) -> PyResult<Self> {
   605                 Self::create_instance(
   594                 Self::create_instance(
   606                     py,
   595                     py,
   607                     RefCell::new(Some(leaked)),
   596                     RefCell::new(leaked),
   608                 )
   597                 )
   609             }
   598             }
   610         }
   599         }
   611     };
   600     };
   612 }
   601 }