Trait numpy::Element

source ·
pub unsafe trait Element: Clone + Send {
    const IS_COPY: bool;

    // Required method
    fn get_dtype_bound(py: Python<'_>) -> Bound<'_, PyArrayDescr>;

    // Provided method
    fn get_dtype<'py>(py: Python<'py>) -> &'py PyArrayDescr { ... }
}
Expand description

Represents that a type can be an element of PyArray.

Currently, only integer/float/complex/object types are supported. The NumPy documentation list the other built-in types which we are not yet implemented.

Note that NumPy’s integer types like numpy.int_ and numpy.uint are based on C’s integer hierarchy which implies that their widths change depending on the platform’s data model. For example, numpy.int_ matches C’s long which is 32 bits wide on Windows (using the LLP64 data model) but 64 bits wide on Linux (using the LP64 data model).

In contrast, Rust’s isize and usize types are defined to have the same width as a pointer and are therefore always 64 bits wide on 64-bit platforms. If you want to match NumPy’s behaviour, consider using the c_long and c_ulong type aliases.

§Safety

A type T that implements this trait should be safe when managed by a NumPy array, thus implementing this trait is marked unsafe. Data types that don’t contain Python objects (i.e., either the object type itself or record types containing object-type fields) are assumed to be trivially copyable, which is reflected in the IS_COPY flag. Furthermore, it is assumed that for the object type the elements are pointers into the Python heap and that the corresponding Clone implemenation will never panic as it only increases the reference count.

§Custom element types

Note that we cannot safely store Py<T> where T: PyClass, because the type information would be eliminated in the resulting NumPy array. In other words, objects are always treated as Py<PyAny> (a.k.a. PyObject) by Python code, and only Py<PyAny> can be stored in a type safe manner.

You can however create Array<Py<T>, D> and turn that into a NumPy array safely and efficiently using from_owned_object_array.

Required Associated Constants§

source

const IS_COPY: bool

Flag that indicates whether this type is trivially copyable.

It should be set to true for all trivially copyable types (like scalar types and record/array types only containing trivially copyable fields and elements).

This flag should always be set to false for object types or record types that contain object-type fields.

Required Methods§

source

fn get_dtype_bound(py: Python<'_>) -> Bound<'_, PyArrayDescr>

Returns the associated type descriptor (“dtype”) for the given element type.

Provided Methods§

source

fn get_dtype<'py>(py: Python<'py>) -> &'py PyArrayDescr

👎Deprecated since 0.21.0: This will be replaced by get_dtype_bound in the future.

Returns the associated type descriptor (“dtype”) for the given element type.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Element for bool

source§

impl Element for f32

source§

impl Element for f64

source§

impl Element for i8

source§

impl Element for i16

source§

impl Element for i32

source§

impl Element for i64

source§

impl Element for isize

source§

impl Element for u8

source§

impl Element for u16

source§

impl Element for u32

source§

impl Element for u64

source§

impl Element for usize

source§

impl Element for bf16

source§

impl Element for f16

source§

impl Element for PyObject

Implementors§

source§

impl Element for Complex32

Complex type with f32 components which maps to numpy.csingle (numpy.complex64).

source§

const IS_COPY: bool = true

source§

impl Element for Complex64

Complex type with f64 components which maps to numpy.cdouble (numpy.complex128).

source§

const IS_COPY: bool = true

source§

impl<U: Unit> Element for Datetime<U>

source§

const IS_COPY: bool = true

source§

impl<U: Unit> Element for Timedelta<U>

source§

const IS_COPY: bool = true

source§

impl<const N: usize> Element for PyFixedString<N>

source§

const IS_COPY: bool = true

source§

impl<const N: usize> Element for PyFixedUnicode<N>

source§

const IS_COPY: bool = true