Skip to content

Commit 73a2b74

Browse files
committed
Code review changes.
1 parent 020c4c9 commit 73a2b74

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

vm/src/stdlib/itertools.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ mod decl {
499499

500500
impl fmt::Debug for GroupByState {
501501
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
502-
f.debug_struct("PyItertoolsGroupBy")
502+
f.debug_struct("GroupByState")
503503
.field("current_value", &self.current_value)
504504
.field("current_key", &self.current_key)
505505
.field("next_group", &self.next_group)
@@ -509,15 +509,10 @@ mod decl {
509509

510510
impl GroupByState {
511511
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))
521516
}
522517
}
523518

@@ -610,7 +605,7 @@ mod decl {
610605
.into_ref(vm);
611606

612607
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))
614609
}
615610

616611
#[pymethod(name = "__iter__")]
@@ -647,30 +642,30 @@ mod decl {
647642
impl PyItertoolsGrouper {
648643
#[pymethod(name = "__next__")]
649644
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();
651647

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+
}
655651

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);
673655
}
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))
674669
}
675670
}
676671

0 commit comments

Comments
 (0)