Trait numpy::convert::IntoPyArray
source · pub trait IntoPyArray: Sized {
type Item: Element;
type Dim: Dimension;
// Required method
fn into_pyarray_bound<'py>(
self,
py: Python<'py>
) -> Bound<'py, PyArray<Self::Item, Self::Dim>>;
// Provided 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, PyArrayMethods};
use pyo3::Python;
Python::with_gil(|py| {
let py_array = vec![1, 2, 3].into_pyarray_bound(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§
Provided Methods§
sourcefn into_pyarray<'py>(
self,
py: Python<'py>
) -> &'py PyArray<Self::Item, Self::Dim>
👎Deprecated since 0.21.0: will be replaced by IntoPyArray::into_pyarray_bound
in the future
fn into_pyarray<'py>( self, py: Python<'py> ) -> &'py PyArray<Self::Item, Self::Dim>
IntoPyArray::into_pyarray_bound
in the futureDeprecated form of IntoPyArray::into_pyarray_bound
Object Safety§
This trait is not object safe.