comparison rust/hg-cpython/src/ref_sharing.rs @ 43426:6f9f15a476a4

rust-cpython: remove useless Option<$leaked> from py_shared_iterator We no longer need to carefully drop the iterator when it's consumed. Mutation is allowed even if the iterator exists. There's a minor behavior change: next(iter) may return/raise something other than StopIteration if it's called after the iterator has been fully consumed, and if the Rust object isn't a FusedIterator.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 12 Oct 2019 20:48:30 +0900
parents ed50f2c31a4c
children b7ab3a0a9e57
comparison
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 }