Skip to content

Commit bbfca26

Browse files
committed
Move PyIteratorValue to objiter.rs
1 parent cba8aa9 commit bbfca26

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use std::ops::Deref;
66
use num_traits::ToPrimitive;
77

88
use crate::function::OptionalArg;
9-
use crate::pyobject::{PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue};
9+
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
1010
use crate::vm::VirtualMachine;
1111

1212
use super::objint;
13+
use super::objiter::PyIteratorValue;
1314
use super::objtype::PyClassRef;
1415

1516
#[derive(Debug)]

vm/src/obj/objdict.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use std::ops::{Deref, DerefMut};
55

66
use crate::function::{KwArgs, OptionalArg};
77
use crate::pyobject::{
8-
DictProtocol, PyAttributes, PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue,
8+
DictProtocol, PyAttributes, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
99
};
1010
use crate::vm::{ReprGuard, VirtualMachine};
1111

1212
use super::objiter;
13+
use super::objiter::PyIteratorValue;
1314
use super::objstr::{self, PyStringRef};
1415
use super::objtype;
1516
use crate::obj::objtype::PyClassRef;

vm/src/obj/objiter.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
* Various types to support iteration.
33
*/
44

5-
use crate::pyobject::{PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult};
5+
use std::cell::Cell;
6+
7+
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
68
use crate::vm::VirtualMachine;
79

810
use super::objbytearray::PyByteArray;
911
use super::objbytes::PyBytes;
1012
use super::objrange::PyRange;
1113
use super::objsequence;
1214
use super::objtype;
15+
use super::objtype::PyClassRef;
1316

1417
/*
1518
* This helper function is called at multiple places. First, it is called
@@ -71,6 +74,21 @@ pub fn new_stop_iteration(vm: &VirtualMachine) -> PyObjectRef {
7174
vm.new_exception(stop_iteration_type, "End of iterator".to_string())
7275
}
7376

77+
// TODO: This is a workaround and shouldn't exist.
78+
// Each iterable type should have its own distinct iterator type.
79+
// (however, this boilerplate can be reused for "generic iterator" for types with only __getiter__)
80+
#[derive(Debug)]
81+
pub struct PyIteratorValue {
82+
pub position: Cell<usize>,
83+
pub iterated_obj: PyObjectRef,
84+
}
85+
86+
impl PyValue for PyIteratorValue {
87+
fn class(vm: &VirtualMachine) -> PyClassRef {
88+
vm.ctx.iter_type()
89+
}
90+
}
91+
7492
type PyIteratorValueRef = PyRef<PyIteratorValue>;
7593

7694
impl PyIteratorValueRef {

vm/src/obj/objlist.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ use std::fmt;
44
use num_traits::ToPrimitive;
55

66
use crate::function::{OptionalArg, PyFuncArgs};
7-
use crate::pyobject::{
8-
IdProtocol, PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
9-
};
7+
use crate::pyobject::{IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
108
use crate::vm::{ReprGuard, VirtualMachine};
119

1210
use super::objbool;
1311
use super::objint;
12+
use super::objiter::PyIteratorValue;
1413
use super::objsequence::{
1514
get_elements, get_elements_cell, get_item, seq_equal, seq_ge, seq_gt, seq_le, seq_lt, seq_mul,
1615
PySliceableSequence,

vm/src/obj/objrange.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use num_integer::Integer;
66
use num_traits::{One, Signed, Zero};
77

88
use crate::function::{OptionalArg, PyFuncArgs};
9-
use crate::pyobject::{Either, PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue};
9+
use crate::pyobject::{Either, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
1010
use crate::vm::VirtualMachine;
1111

1212
use super::objint::{PyInt, PyIntRef};
13+
use super::objiter::PyIteratorValue;
1314
use super::objslice::PySliceRef;
1415
use super::objtype::PyClassRef;
1516

vm/src/obj/objset.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use std::fmt;
88
use std::hash::{Hash, Hasher};
99

1010
use crate::function::{OptionalArg, PyFuncArgs};
11-
use crate::pyobject::{
12-
PyContext, PyIteratorValue, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
13-
};
11+
use crate::pyobject::{PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
1412
use crate::vm::{ReprGuard, VirtualMachine};
1513

1614
use super::objbool;
1715
use super::objint;
1816
use super::objiter;
17+
use super::objiter::PyIteratorValue;
1918
use super::objtype;
2019
use super::objtype::PyClassRef;
2120

vm/src/obj/objtuple.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ use std::fmt;
33
use std::hash::{Hash, Hasher};
44

55
use crate::function::OptionalArg;
6-
use crate::pyobject::{
7-
IdProtocol, PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue,
8-
};
6+
use crate::pyobject::{IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
97
use crate::vm::{ReprGuard, VirtualMachine};
108

119
use super::objbool;
1210
use super::objint;
11+
use super::objiter::PyIteratorValue;
1312
use super::objsequence::{
1413
get_elements, get_item, seq_equal, seq_ge, seq_gt, seq_le, seq_lt, seq_mul,
1514
};

vm/src/pyobject.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::any::Any;
2-
use std::cell::{Cell, RefCell};
2+
use std::cell::RefCell;
33
use std::collections::HashMap;
44
use std::fmt;
55
use std::marker::PhantomData;
@@ -1127,20 +1127,6 @@ where
11271127
}
11281128
}
11291129

1130-
// TODO: This is a workaround and shouldn't exist.
1131-
// Each iterable type should have its own distinct iterator type.
1132-
#[derive(Debug)]
1133-
pub struct PyIteratorValue {
1134-
pub position: Cell<usize>,
1135-
pub iterated_obj: PyObjectRef,
1136-
}
1137-
1138-
impl PyValue for PyIteratorValue {
1139-
fn class(vm: &VirtualMachine) -> PyClassRef {
1140-
vm.ctx.iter_type()
1141-
}
1142-
}
1143-
11441130
impl<T> PyObject<T>
11451131
where
11461132
T: Sized + PyObjectPayload,

0 commit comments

Comments
 (0)