0

I have a method foo with the following signature:

pub fn foo(data: PyReadonlyArrayDyn<f64>) {
    ...
}

In my test I'm trying to create a test array to feed into foo

pyo3::Python::with_gil(|py| {
    let vec = vec![1.0, 2.0, 3.0, 4.0];
    py_array = PyArray::from_vec(py, vec);
    readonly = py_array.readonly();
    foo(readonly);
}

Unfortunately, rust tells me:

mismatched types
expected struct `PyReadonlyArray<'_, f64, Dim<IxDynImpl>>`
   found struct `PyReadonlyArray<'_, {float}, Dim<[usize; 1]>>`

Apparently, I'm running into a problem regarding dimensionality. How do I have to create the PyReadonlyArray to be able to feed it into foo?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
grmmgrmm
  • 994
  • 10
  • 29

1 Answers1

0

Call PyArray::to_dyn():

pyo3::Python::with_gil(|py| {
    let vec = vec![1.0, 2.0, 3.0, 4.0];
    py_array = PyArray::from_vec(py, vec);
    readonly = py_array.to_dyn().readonly();
    foo(readonly);
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77