@@ -499,7 +499,7 @@ mod decl {
499
499
500
500
impl fmt:: Debug for GroupByState {
501
501
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
502
- f. debug_struct ( "PyItertoolsGroupBy " )
502
+ f. debug_struct ( "GroupByState " )
503
503
. field ( "current_value" , & self . current_value )
504
504
. field ( "current_key" , & self . current_key )
505
505
. field ( "next_group" , & self . next_group )
@@ -509,15 +509,10 @@ mod decl {
509
509
510
510
impl GroupByState {
511
511
fn is_current ( & self , grouper : & PyItertoolsGrouperRef ) -> bool {
512
- if let Some ( ref current_weak_grouper) = self . grouper {
513
- if let Some ( current_grouper) = current_weak_grouper. upgrade ( ) {
514
- grouper. is ( & current_grouper)
515
- } else {
516
- false
517
- }
518
- } else {
519
- false
520
- }
512
+ self . grouper
513
+ . as_ref ( )
514
+ . and_then ( |g| g. upgrade ( ) )
515
+ . map_or ( false , |ref current_grouper| grouper. is ( current_grouper) )
521
516
}
522
517
}
523
518
@@ -610,7 +605,7 @@ mod decl {
610
605
. into_ref ( vm) ;
611
606
612
607
state. grouper = Some ( PyRc :: downgrade ( & grouper. clone ( ) . into_typed_pyobj ( ) ) ) ;
613
- Ok ( ( state. current_key . clone ( ) . unwrap ( ) , grouper) )
608
+ Ok ( ( state. current_key . as_ref ( ) . unwrap ( ) . clone ( ) , grouper) )
614
609
}
615
610
616
611
#[ pymethod( name = "__iter__" ) ]
@@ -647,30 +642,30 @@ mod decl {
647
642
impl PyItertoolsGrouper {
648
643
#[ pymethod( name = "__next__" ) ]
649
644
fn next ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyResult {
650
- let mut state = zelf. groupby . state . lock ( ) ;
645
+ let old_key = {
646
+ let mut state = zelf. groupby . state . lock ( ) ;
651
647
652
- if !state. is_current ( & zelf) {
653
- return Err ( new_stop_iteration ( vm) ) ;
654
- }
648
+ if !state. is_current ( & zelf) {
649
+ return Err ( new_stop_iteration ( vm) ) ;
650
+ }
655
651
656
- if let Some ( val) = state. current_value . clone ( ) {
657
- state. current_value = None ;
658
- Ok ( val)
659
- } else {
660
- let old_key = state. current_key . clone ( ) . unwrap ( ) ;
661
- // Shouldn't mutex over call to advance as that executes arbitrary Python code
662
- drop ( state) ;
663
- let ( value, key) = zelf. groupby . advance ( vm) ?;
664
- if vm. bool_eq ( key. clone ( ) , old_key) ? {
665
- Ok ( value)
666
- } else {
667
- let mut state = zelf. groupby . state . lock ( ) ;
668
- state. current_value = Some ( value) ;
669
- state. current_key = Some ( key) ;
670
- state. next_group = true ;
671
- state. grouper = None ;
672
- Err ( new_stop_iteration ( vm) )
652
+ // check to see if the value has already been retrieved from the iterator
653
+ if let Some ( val) = state. current_value . take ( ) {
654
+ return Ok ( val) ;
673
655
}
656
+
657
+ state. current_key . as_ref ( ) . unwrap ( ) . clone ( )
658
+ } ;
659
+ let ( value, key) = zelf. groupby . advance ( vm) ?;
660
+ if vm. bool_eq ( key. clone ( ) , old_key) ? {
661
+ Ok ( value)
662
+ } else {
663
+ let mut state = zelf. groupby . state . lock ( ) ;
664
+ state. current_value = Some ( value) ;
665
+ state. current_key = Some ( key) ;
666
+ state. next_group = true ;
667
+ state. grouper = None ;
668
+ Err ( new_stop_iteration ( vm) )
674
669
}
675
670
}
676
671
0 commit comments