-
Notifications
You must be signed in to change notification settings - Fork 136
Support array.array('f', ...) -> Vec<f32> #19
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
Comments
array.array('f', ...)
-> Vec<f32>
The buffer protocol could be used to optimize extracting a |
Maybe I misunderstood the approach. Are Sequence, Mapping, Iterator, and Buffer protocols supported? If these are supported then I don't think we need to copy data into a Vec and the bug title is completely misleading. Sorry for that. So in this case, I think it makes sense to make the Iterator protocol map to something like |
Note: the Sequence, Mapping, Iterator and Buffer protocols are not yet supported in the safe On the "extract to The buffer protocol allows accessing the data without allocations. But that's not possible for arbitrary My recent change from Of course, an easy workaround to the impl specialization problem is to have the |
Thanks for the explanations. So, if I understand correctly, ExtractPyObject can only work in converting Python objects into the appropriate Rust objects without impl specializations if there are multiple versions of the function for defining which conversion to use. e.g. That doesn't seem too bad. It probably makes sense to have these traits anyway and Perhaps another option is to allow for a template argument defining the template extractor. let arg0 = match args.get_item(0).extract::<Vec<f32>, PyObjectExtractor>() {
//...
};
let arg1 = match args.get_item(0).extract::<Vec<f32>, PySequenceExtractor() {
// ...
}; This allows for functions like I smashed together some code to do this, but it hits a compiler bug. It's probably not terribly useful, but it might illustrate the idea I'm trying to get across:
It also would need the obvious changes in So that's an idea on how to approach the extraction without impl specialization. |
@arielb1 claims that rust-lang/rust#22066 seems to be working in 1.10 so maybe we can return to this. |
The new logic is: |
So does this also give us raw access to numpy arrays!? I think they have buffer protocol. Also if I am reading the sequence protocol corectly; we can use |
Yes, this should give access to numpy arrays without going through the python runtime for each element. On |
That is so cool! Thank you! Can we support buffer -> |
There's
|
As usual you already have it covered! 👍 |
@dgrunwald What about multi-dimensional numpy arrays? |
@b52: |
rust-cpython can convert Python lists to Rust vectors, but it can't yet convert arrays created by the array module. It also doesn't support numpy arrays, which is probably a bigger project, but also valuable goal.
For example, I have an example where I am writing a dot product function:
It works pretty well for a toy example:
However, it won't convert
numpy array
s orarray.array
s:The text was updated successfully, but these errors were encountered: