-
Notifications
You must be signed in to change notification settings - Fork 678
/
dict_iter.rs
42 lines (37 loc) · 953 Bytes
/
dict_iter.rs
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
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::types::PyDict;
#[pymodule]
pub fn dict_iter(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<DictSize>()?;
Ok(())
}
#[pyclass]
pub struct DictSize {
expected: u32,
}
#[pymethods]
impl DictSize {
#[new]
fn new(expected: u32) -> Self {
DictSize { expected }
}
fn iter_dict(&mut self, _py: Python<'_>, dict: &PyDict) -> PyResult<u32> {
let mut seen = 0u32;
for (sym, values) in dict.iter() {
seen += 1;
println!(
"{:4}/{:4} iterations:{}=>{}",
seen, self.expected, sym, values
);
}
if seen == self.expected {
Ok(seen)
} else {
Err(PyErr::new::<PyRuntimeError, _>(format!(
"Expected {} iterations - performed {}",
self.expected, seen
)))
}
}
}