forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.rs
More file actions
52 lines (45 loc) · 1.39 KB
/
zip.rs
File metadata and controls
52 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use super::pytype::PyTypeRef;
use crate::function::Args;
use crate::iterator;
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::slots::PyIter;
use crate::vm::VirtualMachine;
pub type PyZipRef = PyRef<PyZip>;
#[pyclass(module = false, name = "zip")]
#[derive(Debug)]
pub struct PyZip {
iterators: Vec<PyObjectRef>,
}
impl PyValue for PyZip {
fn class(vm: &VirtualMachine) -> &PyTypeRef {
&vm.ctx.types.zip_type
}
}
#[pyimpl(with(PyIter), flags(BASETYPE))]
impl PyZip {
#[pyslot]
fn tp_new(cls: PyTypeRef, iterables: Args, vm: &VirtualMachine) -> PyResult<PyZipRef> {
let iterators = iterables
.into_iter()
.map(|iterable| iterator::get_iter(vm, iterable))
.collect::<Result<Vec<_>, _>>()?;
PyZip { iterators }.into_ref_with_type(vm, cls)
}
}
impl PyIter for PyZip {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if zelf.iterators.is_empty() {
Err(vm.new_stop_iteration())
} else {
let next_objs = zelf
.iterators
.iter()
.map(|iterator| iterator::call_next(vm, iterator))
.collect::<Result<Vec<_>, _>>()?;
Ok(vm.ctx.new_tuple(next_objs))
}
}
}
pub fn init(context: &PyContext) {
PyZip::extend_class(context, &context.types.zip_type);
}