1
1
use super :: pytype:: PyTypeRef ;
2
2
use crate :: function:: Args ;
3
3
use crate :: iterator;
4
+ use crate :: protocol:: PyIter ;
4
5
use crate :: slots:: { IteratorIterable , SlotConstructor , SlotIterator } ;
5
6
use crate :: vm:: VirtualMachine ;
6
7
use crate :: { PyClassImpl , PyContext , PyObjectRef , PyRef , PyResult , PyValue } ;
@@ -13,7 +14,7 @@ use crate::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
13
14
#[ derive( Debug ) ]
14
15
pub struct PyMap {
15
16
mapper : PyObjectRef ,
16
- iterators : Vec < PyObjectRef > ,
17
+ iterators : Vec < PyIter > ,
17
18
}
18
19
19
20
impl PyValue for PyMap {
@@ -23,18 +24,11 @@ impl PyValue for PyMap {
23
24
}
24
25
25
26
impl SlotConstructor for PyMap {
26
- type Args = ( PyObjectRef , Args < PyObjectRef > ) ;
27
+ type Args = ( PyObjectRef , Args < PyIter > ) ;
27
28
28
- fn py_new ( cls : PyTypeRef , ( function, iterables) : Self :: Args , vm : & VirtualMachine ) -> PyResult {
29
- let iterators = iterables
30
- . into_iter ( )
31
- . map ( |iterable| iterator:: get_iter ( vm, iterable) )
32
- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
33
- PyMap {
34
- mapper : function,
35
- iterators,
36
- }
37
- . into_pyresult_with_type ( vm, cls)
29
+ fn py_new ( cls : PyTypeRef , ( mapper, iterators) : Self :: Args , vm : & VirtualMachine ) -> PyResult {
30
+ let iterators = iterators. into_vec ( ) ;
31
+ PyMap { mapper, iterators } . into_pyresult_with_type ( vm, cls)
38
32
}
39
33
}
40
34
@@ -43,7 +37,7 @@ impl PyMap {
43
37
#[ pymethod( magic) ]
44
38
fn length_hint ( & self , vm : & VirtualMachine ) -> PyResult < usize > {
45
39
self . iterators . iter ( ) . try_fold ( 0 , |prev, cur| {
46
- let cur = iterator:: length_hint ( vm, cur. clone ( ) ) ?. unwrap_or ( 0 ) ;
40
+ let cur = iterator:: length_hint ( vm, cur. as_object ( ) . clone ( ) ) ?. unwrap_or ( 0 ) ;
47
41
let max = std:: cmp:: max ( prev, cur) ;
48
42
Ok ( max)
49
43
} )
@@ -56,7 +50,7 @@ impl SlotIterator for PyMap {
56
50
let next_objs = zelf
57
51
. iterators
58
52
. iter ( )
59
- . map ( |iterator| iterator:: call_next ( vm, iterator ) )
53
+ . map ( |iterator| iterator. next ( vm) )
60
54
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
61
55
62
56
// the mapper itself can raise StopIteration which does stop the map iteration
0 commit comments