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§

source

type Item: Element

The element type of resulting array.

source

type Dim: Dimension

The dimension type of the resulting array.

Required Methods§

source

fn into_pyarray_bound<'py>( self, py: Python<'py> ) -> Bound<'py, PyArray<Self::Item, Self::Dim>>

Consumes self and moves its data into a NumPy array.

Provided Methods§

source

fn 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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<A, D> IntoPyArray for ArrayBase<OwnedRepr<A>, D>
where A: Element, D: Dimension,

§

type Item = A

§

type Dim = D

source§

fn into_pyarray_bound<'py>( self, py: Python<'py> ) -> Bound<'py, PyArray<Self::Item, Self::Dim>>

source§

impl<T: Element> IntoPyArray for Box<[T]>

§

type Item = T

§

type Dim = Dim<[usize; 1]>

source§

fn into_pyarray_bound<'py>( self, py: Python<'py> ) -> Bound<'py, PyArray<Self::Item, Self::Dim>>

source§

impl<T: Element> IntoPyArray for Vec<T>

§

type Item = T

§

type Dim = Dim<[usize; 1]>

source§

fn into_pyarray_bound<'py>( self, py: Python<'py> ) -> Bound<'py, PyArray<Self::Item, Self::Dim>>

Implementors§