numpy.rs


Overview

The [numpy.rs](/projects/287/67683) file provides comprehensive support for serializing NumPy arrays and scalars from Python into Rust data structures that implement the Serde `Serialize` trait. This enables efficient, type-safe serialization of NumPy data for further processing or transmission, typically in JSON or other serialization formats.

This file bridges Python's NumPy data structures and Rust, handling low-level Python FFI interactions, NumPy's C array interface, and the various NumPy data types including multi-dimensional arrays, scalars, and datetime64 types. It enforces constraints such as C-contiguous memory layout and native endianness to guarantee correctness and performance.


Detailed Description of Key Components

Structs and Enums


NumpySerializer<'a>


NumpyArray


ItemType


NumpyScalar


NumPy Scalar Structs

These structs represent individual NumPy scalar types and implement `Serialize` to convert them into Rust primitives.

Examples include:

Each has a `value` field of the corresponding Rust primitive type and implements Serde serialization accordingly.


NumPy Datetime Handling


Utility Functions


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class NumpySerializer {
        -previous: &PyObjectSerializer
        +new(previous: &PyObjectSerializer)
        +serialize<S: Serializer>(serializer: S) -> Result<S::Ok, S::Error>
    }

    class NumpyArray {
        -array: *mut PyArrayInterface
        -position: Vec<isize>
        -children: Vec<NumpyArray>
        -depth: usize
        -capsule: *mut PyCapsule
        -kind: ItemType
        -opts: Opt
        +new(ptr: *mut PyObject, opts: Opt) -> Result<Self, PyArrayError>
        +serialize<S: Serializer>(serializer: S) -> Result<S::Ok, S::Error>
        -build()
        -child_from_parent(position: Vec<isize>, num_children: usize) -> Self
        -data() -> *const c_void
        -num_items() -> usize
        -shape() -> &[isize]
        -strides() -> &[isize]
        -dimensions() -> usize
    }

    class ItemType {
        <<enumeration>>
        +BOOL
        +DATETIME64(NumpyDatetimeUnit)
        +F16
        +F32
        +F64
        +I8
        +I16
        +I32
        +I64
        +U8
        +U16
        +U32
        +U64
        +find(array: *mut PyArrayInterface, ptr: *mut PyObject) -> Option<ItemType>
    }

    class NumpyScalar {
        -ptr: *mut PyObject
        -opts: Opt
        +new(ptr: *mut PyObject, opts: Opt)
        +serialize<S: Serializer>(serializer: S) -> Result<S::Ok, S::Error>
    }

    NumpySerializer --> PyObjectSerializer : wraps
    NumpySerializer --> NumpyArray : creates and serializes
    NumpyArray --> NumpyArray : contains children (nested arrays)
    NumpyArray --> ItemType : identifies element type
    NumpyScalar --> NumpyScalarTypes : dispatches based on type

Summary

The [numpy.rs](/projects/287/67683) file is a core serialization module converting Python NumPy arrays and scalars into Rust serializable types. It handles low-level interfacing with Python and NumPy C APIs, enforces memory layout constraints, supports a wide range of NumPy data types including datetime64, and provides recursive serialization for multi-dimensional arrays. This functionality integrates closely with the overall Python object serialization system, ensuring that NumPy data can be serialized efficiently and accurately for Rust-based processing or external output formats.