简体   繁体   中英

How to create a PyArray to feed into method

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 ?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM