fusion-engineering / inline-python

Inline Python code directly in your Rust code
https://docs.rs/inline-python
BSD 2-Clause "Simplified" License
1.15k stars 37 forks source link

Cannot use array ([T; N]) in python #51

Open PigeonF opened 2 years ago

PigeonF commented 2 years ago

Hi!

First, thanks for the awesome project, it is very useful for plotting 👍 .

I am trying to use an array in inline_python, but it cannot be converted, because [T; N] does not implement ToPyObject.

let array = [0.0f64; 64];
inline_python::python! {
    a = 'array
};
Error Message ``` error[E0277]: the trait bound `[f64; 64]: ToPyObject` is not satisfied --> src\main.rs:69:13 | 68 | / inline_python::python! { 69 | | a = 'array | | ^^^^^^ the trait `ToPyObject` is not implemented for `[f64; 64]` 70 | | }; | |_____- required by a bound introduced by this call ```

One solution is to convert the array to a slice, but I think supporting to an array should still be supported.

While searching, I found https://github.com/PyO3/pyo3/blob/main/src/conversions/array.rs, which allows for conversion between arrays and python objects, but as far as I can tell, this is not picked up because it only implements IntoPy<PyObject> for [T; N], not ToPyObject.

Thus, I do not know if this is an issue with inline_python, or with PyO3. Since I am directly depending on inline_python I decided to open an issue here.

Maybe related: #10

de-vri-es commented 2 years ago

Hey! Thanks for reporting the issue.

In this case, it can only be fixed by pyo3. We can not implement the ToPyObject trait for arrays in this crate.

It sounds like pyo3 is simply missing a impl ToPyObject for [T; N]. At a first glance, I don't see a reason why that trait wouldn't be possible to implement for arrays. It's probably just missing because nobody thought to add it.