pub trait IntoPyArray: Sized {
type Item: Element;
type Dim: Dimension;
// Required method
fn into_pyarray<'py>(
self,
py: Python<'py>,
) -> Bound<'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, PyArrayMethods};
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());
}
});
Required Associated Types§
Required Methods§
Sourcefn into_pyarray<'py>(
self,
py: Python<'py>,
) -> Bound<'py, PyArray<Self::Item, Self::Dim>>
fn into_pyarray<'py>( self, py: Python<'py>, ) -> Bound<'py, PyArray<Self::Item, Self::Dim>>
Consumes self
and moves its data into a NumPy array.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.