-
Notifications
You must be signed in to change notification settings - Fork 801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How can I make a pymethod return a generator? #945
Comments
AFAIK you can't have generators on the Rust side, but you can return an iterator being declared in Rust as a Btw, have you been declaring you class with |
RE: the first point, I've only been able to get this to work when the iterator owns the data it is iterating over. If the class had a single function which returned an iterator, I could implement I am using |
That sounds like a bug. I've opened a new issue #947 to track it when someone has a chance to investigate. @thesketh , regarding the ownership of the data, that's correct. There's no way to express a Rust lifetime safely in a To avoid having to copy the vector, you could consider using a shared pointer like
|
Thanks for opening that, I'll keep an eye on it. And thanks for the pointer (no pun intended), I'm quite new to using Rust and I didn't realise that using the reference counted types was the right solution to this. That answers my question! |
Hi all,
I'm writing a Python wrapper for a Rust lib, and PyO3 has been great so far! It's been a pleasure to use, and I was so impressed by the fact that PyPy was supported with literally no effort on my part.
Some of the Rust methods I've wrapped would normally return Iterators over
Vec
s in the data, which mostly look a bit like this:I can't wrap these using
#[pyclass]
because they contain a lifetime annotation. I came up with something like this instead:I was hoping to be able to subclass
PyClass
like so, and define some functions which return a Python generator:But it seems that I can't properly subclass PyO3
#[pyclass]
structs:Class.__new__
actually returns an instance ofPyClass
so I'm unable to useiter_v1
anditer_v2
.I'm trying to avoid cloning the whole
Vec
at once: if I was going to clone theVec
s anyway, I could add them as members ofPyClass
and use#[pyo3(get)]
to access them as lists in Python.Any ideas?
The text was updated successfully, but these errors were encountered: