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

    fn get_dtype(py: Python<'_>) -> &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.

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.

Associated Constants

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

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

Implementations on Foreign Types

Implementors

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

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