Trait IntoPyArray

Source
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§

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<'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.

Implementations on Foreign Types§

Source§

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

Source§

type Item = A

Source§

type Dim = D

Source§

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

Source§

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

Source§

type Item = T

Source§

type Dim = Dim<[usize; 1]>

Source§

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

Source§

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

Source§

type Item = T

Source§

type Dim = Dim<[usize; 1]>

Source§

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

Implementors§