dataclass.rs


Overview

This Rust source file implements serialization logic for Python dataclasses within a Rust-based Python serialization framework. Its primary purpose is to efficiently convert Python dataclass instances into a serializable map format (e.g., JSON) by leveraging the Rust `serde` serialization traits. The file defines multiple serializer structs that handle serialization in different scenarios:

The implementation carefully handles Python C API calls, reference counting, recursion limits, and error handling to ensure safe and correct serialization.


Detailed Explanation of Components

Struct: DataclassGenericSerializer<'a>

Purpose

Acts as the generic serializer for dataclasses, deciding at runtime whether to use the fast dictionary-based serialization or fallback field-based serialization depending on the dataclass instance characteristics.

Fields

Methods

Trait Implementations

Usage Example

let py_obj_serializer = PyObjectSerializer::new(py_dataclass_obj, state, default);
let dataclass_serializer = DataclassGenericSerializer::new(&py_obj_serializer);
let serialized = serde_json::to_string(&dataclass_serializer)?;

Struct: DataclassFastSerializer

Purpose

Implements a fast serialization path by serializing the dataclass instance directly from its `__dict__` attribute, which is a Python dictionary of attribute names to values.

Fields

Methods

Trait Implementations

Important Details

Usage Example

Internally used by `DataclassGenericSerializer`, but can be used standalone if you have the `__dict__` pointer:

let fast_serializer = DataclassFastSerializer::new(dict_ptr, state, default);
let serialized = serde_json::to_string(&fast_serializer)?;

Struct: DataclassFallbackSerializer

Purpose

Fallback serializer that serializes dataclass instances by iterating over the dataclass's declared fields (from `__dataclass_fields__`), rather than relying on `__dict__`. Used when the fast path is not viable (e.g., if `__dict__` is missing or class uses `__slots__`).

Fields

Methods

Trait Implementations

Important Details


Implementation Details & Algorithms


Integration with Other Components


Mermaid Diagram: Class Structure

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

    class DataclassFastSerializer {
        - ptr: *mut PyObject
        - state: SerializerState
        - default: Option<NonNull<PyObject>>
        + new(ptr: *mut PyObject, state: SerializerState, default: Option<NonNull<PyObject>>) DataclassFastSerializer
        + serialize<S: Serializer>(serializer: S) -> Result<S::Ok, S::Error>
    }

    class DataclassFallbackSerializer {
        - ptr: *mut PyObject
        - state: SerializerState
        - default: Option<NonNull<PyObject>>
        + new(ptr: *mut PyObject, state: SerializerState, default: Option<NonNull<PyObject>>) DataclassFallbackSerializer
        + serialize<S: Serializer>(serializer: S) -> Result<S::Ok, S::Error>
    }

    DataclassGenericSerializer --> DataclassFastSerializer : uses
    DataclassGenericSerializer --> DataclassFallbackSerializer : uses

Summary

The [dataclass.rs](/projects/287/67677) file provides a highly optimized and robust mechanism to serialize Python dataclasses in Rust using Serde. It intelligently chooses between a fast dictionary-based serialization and a fallback field-based approach to handle various dataclass configurations, including those with `__slots__`. The file demonstrates careful use of Python C API, Rust safety practices, and serialization protocol adherence, making it a critical component in the Python-Rust serialization bridge.


Additional Notes


End of Documentation for dataclass.rs