Skip to content

Commit f920982

Browse files
author
j30ng
committed
Python 'None' Value -> Option
1 parent 708fd9b commit f920982

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

vm/src/stdlib/itertools.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ impl PyItertoolsFilterFalse {
542542
struct PyItertoolsAccumulate {
543543
iterable: PyObjectRef,
544544
binop: PyObjectRef,
545-
acc_value: RefCell<PyObjectRef>,
545+
acc_value: RefCell<Option<PyObjectRef>>,
546546
}
547547

548548
impl PyValue for PyItertoolsAccumulate {
@@ -566,28 +566,29 @@ impl PyItertoolsAccumulate {
566566
Ok(PyItertoolsAccumulate {
567567
iterable: iter,
568568
binop: binop.unwrap_or_else(|| vm.get_none()),
569-
acc_value: RefCell::from(vm.get_none()),
569+
acc_value: RefCell::from(Option::None),
570570
}
571571
.into_ref(vm)
572572
.into_object())
573573
}
574574

575575
#[pymethod(name = "__next__")]
576576
fn next(&self, vm: &VirtualMachine) -> PyResult {
577-
let acc_value = self.acc_value.borrow().clone();
578577
let iterable = &self.iterable;
579-
580578
let obj = call_next(vm, iterable)?;
581579

582-
let next_acc_value = if acc_value.is(&vm.get_none()) {
583-
obj.clone()
584-
} else if self.binop.is(&vm.get_none()) {
585-
vm._add(acc_value, obj.clone())?
586-
} else {
587-
vm.invoke(&self.binop, vec![acc_value, obj.clone()])?
580+
let next_acc_value = match &*self.acc_value.borrow() {
581+
Option::None => obj.clone(),
582+
Option::Some(value) => {
583+
if self.binop.is(&vm.get_none()) {
584+
vm._add(value.clone(), obj.clone())?
585+
} else {
586+
vm.invoke(&self.binop, vec![value.clone(), obj.clone()])?
587+
}
588+
}
588589
};
590+
self.acc_value.replace(Option::from(next_acc_value.clone()));
589591

590-
self.acc_value.replace(next_acc_value.clone());
591592
Ok(next_acc_value)
592593
}
593594

0 commit comments

Comments
 (0)