@@ -1258,9 +1258,7 @@ mod array {
12581258
12591259 impl Iterable for PyArray {
12601260 fn iter ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyResult {
1261- Ok ( PyArrayIterator {
1262- position : AtomicUsize :: new ( 0 ) ,
1263- array : zelf. clone ( ) ,
1261+ Ok ( PyArrayIter {
12641262 internal : PyMutex :: new ( PositionIterInternal :: new ( zelf. clone ( ) , 0 ) ) ,
12651263 }
12661264 . into_pyobject ( vm) )
@@ -1279,37 +1277,31 @@ mod array {
12791277 #[ pyattr]
12801278 #[ pyclass( name = "arrayiterator" ) ]
12811279 #[ derive( Debug , PyPayload ) ]
1282- pub struct PyArrayIterator {
1283- position : AtomicUsize ,
1284- array : PyArrayRef ,
1280+ pub struct PyArrayIter {
12851281 internal : PyMutex < PositionIterInternal < PyArrayRef > > ,
12861282 }
12871283
1288- #[ pyimpl( with( IterNext ) ) ]
1289-
1290- impl PyArrayIterator {
1284+ #[ pyimpl( with( IterNext ) , flags( HAS_DICT ) ) ]
1285+ impl PyArrayIter {
12911286 #[ pymethod( magic) ]
12921287 fn reduce ( & self , vm : & VirtualMachine ) -> PyTupleRef {
1293- let tuple = self . internal
1288+ self . internal
12941289 . lock ( )
1295- . builtins_iter_reduce ( |x| x. clone ( ) . into ( ) , vm) ;
1296- let func = tuple[ 0 ] . clone ( ) ;
1297- let obj = tuple[ 1 ] . clone ( ) ;
1298- let pos = self . position . load ( atomic:: Ordering :: SeqCst ) ;
1299- vm. new_tuple ( ( func, obj, pos, ) )
1290+ . builtins_iter_reduce ( |x| x. clone ( ) . into ( ) , vm)
13001291 }
13011292 }
13021293
1303- impl IterNextIterable for PyArrayIterator { }
1304- impl IterNext for PyArrayIterator {
1294+ impl IterNextIterable for PyArrayIter { }
1295+ impl IterNext for PyArrayIter {
13051296 fn next ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1306- let pos = zelf. position . fetch_add ( 1 , atomic:: Ordering :: SeqCst ) ;
1307- let r = if let Some ( item) = zelf. array . read ( ) . get ( pos, vm) {
1308- PyIterReturn :: Return ( item?)
1309- } else {
1310- PyIterReturn :: StopIteration ( None )
1311- } ;
1312- Ok ( r)
1297+ zelf. internal . lock ( ) . next ( |array, pos| {
1298+ let r = if let Some ( item) = array. read ( ) . get ( pos, vm) {
1299+ PyIterReturn :: Return ( item?)
1300+ } else {
1301+ PyIterReturn :: StopIteration ( None )
1302+ } ;
1303+ Ok ( r)
1304+ } )
13131305 }
13141306 }
13151307
0 commit comments