Пока что Я собирал значения, выдаваемые итератором, и возвращал список, что явно не лучшее решение. Вот код, иллюстрирующий мою проблему:
Код: Выделить всё
use pyo3::prelude::*;
// The Rust Iterator, from the library I'm wrapping.
pub struct RustIterator {
type Item = &'a isize;
fn next(&mut self) -> Option {
let result = self.view.get(self.position);
if let Some(_) = result { self.position += 1 };
result
}
}
// The Rust struct, from the library I'm wrapping.
struct RustStruct {
v: Vec
}
impl RustStruct {
fn iter(&self) -> RustIterator {
RustIterator{ position: 0, view: &self.v }
}
}
// The Python wrapper class, which exposes the
// functions of RustStruct in a Python-friendly way.
#[pyclass]
struct PyClass {
rust_struct: RustStruct,
}
#[pymethods]
impl PyClass {
#[new]
fn new(v: Vec) -> Self {
let rust_struct = RustStruct { v };
Self{ rust_struct }
}
// This is what I'm doing so far, which works
// but doesn't iterate lazily.
fn iter(&self) -> Vec {
let mut output_v = Vec::new();
for item in self.rust_struct.iter() {
output_v.push(*item);
}
output_v
}
}
Как могу ли я получить доступ к ленивому итератору через RustStruct.v в Python? Можно с уверенностью предположить, что тип, содержащийся в Vec, всегда наследует Copy и Clone, и ответы, требующие некоторого кода на конце Python, приемлемы (но менее идеальны).
Подробнее здесь: https://stackoverflow.com/questions/619 ... using-pyo3