Trait numpy::convert::IntoPyArray
source · pub trait IntoPyArray {
type Item: Element;
type Dim: Dimension;
// Required method
fn into_pyarray<'py>(
self,
py: Python<'py>
) -> &'py PyArray<Self::Item, Self::Dim>;
}
Expand description
Conversion trait from owning Rust types into PyArray
.
This trait takes ownership of self
, which means it holds a pointer into the Rust heap.
In addition, some destructive methods like resize
cannot be used with NumPy arrays constructed using this trait.
Example
use numpy::{PyArray, IntoPyArray};
use pyo3::Python;
Python::with_gil(|py| {
let py_array = vec![1, 2, 3].into_pyarray(py);
assert_eq!(py_array.readonly().as_slice().unwrap(), &[1, 2, 3]);
// Array cannot be resized when its data is owned by Rust.
unsafe {
assert!(py_array.resize(100).is_err());
}
});