default.rs


Overview

The [default.rs](/projects/287/68131) file provides an internal Rust implementation of a **default value serialization mechanism** used within a Python object serialization context. Specifically, it defines the `DefaultSerializer` struct, which acts as a fallback serializer that delegates to a user-provided Python callable (the "default" function) when the usual serialization fails or is unsupported.

This file is part of a serialization framework that bridges Python objects with Rust serialization traits (from Serde), allowing complex Python objects to be serialized into formats supported by Serde serializers (e.g., JSON, YAML). The `DefaultSerializer` handles cases where Python objects require a custom fallback serialization by invoking a Python callable provided by the user.


Detailed Documentation

Struct: DefaultSerializer<'a>

pub(crate) struct DefaultSerializer<'a> {
    previous: &'a PyObjectSerializer,
}

Description

Fields

Name

Type

Description

`previous`

`&'a PyObjectSerializer`

Reference to the previous serializer state, including the Python object and configuration.

Usage Example

let default_serializer = DefaultSerializer::new(&py_object_serializer);
let result = default_serializer.serialize(serde_json::Serializer::new(...));

Methods

new

pub fn new(previous: &'a PyObjectSerializer) -> Self

Trait Implementation: Serialize for DefaultSerializer<'_>

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,

Description

Implements Serde's `Serialize` trait for `DefaultSerializer`. This method attempts to serialize the underlying Python object using a default fallback function defined in Python.

Behavior

  1. Checks if a user-defined default callable (self.previous.default) is available.

  2. Enforces a recursion limit on default calls to avoid infinite recursion.

  3. Calls the Python default function with the Python object as the argument.

    • The call is performed differently depending on the Python version (Py_3_10 conditional compilation).

  4. If the call returns null, signals an unsupported type error.

  5. Otherwise, creates a new PyObjectSerializer for the result of the default call and serializes it recursively.

  6. Decrements the Python reference count for the returned object.

  7. Returns the serialization result or an error.

Parameters

Returns

Errors

Usage Example

This method is usually called internally by Serde when serializing with the fallback default serializer:

let default_serializer = DefaultSerializer::new(&py_object_serializer);
match default_serializer.serialize(serde_json::Serializer::new(...)) {
    Ok(serialized) => println!("Serialization successful"),
    Err(e) => eprintln!("Serialization failed: {:?}", e),
}

Important Implementation Details


Interaction with Other Components


File Structure and Workflow Diagram

flowchart TD
    A[DefaultSerializer Struct]
    A --> B[new() Constructor]
    A --> C[serialize() Method]
    C --> D{Check default callable exists}
    D -- Yes --> E[Check recursion limit]
    E -- Limit reached --> F[Return DefaultRecursionLimit Error]
    E -- Limit OK --> G[Call Python default callable]
    G --> H{Call success?}
    H -- No --> I[Return UnsupportedType Error]
    H -- Yes --> J[Create new PyObjectSerializer for result]
    J --> K[Recursively serialize result]
    K --> L[Decrement Python ref count]
    L --> M[Return serialization result]
    D -- No --> I

Summary

The [default.rs](/projects/287/68131) file implements a critical fallback serialization feature for Python objects in Rust, allowing custom Python callables to define how unsupported types should be serialized. It carefully manages Python reference counting, recursion limits, and compatibility across Python versions, integrating tightly with the overall Python-to-Rust serialization framework. This ensures extensible, safe, and efficient fallback serialization behavior within the broader system.


End of Documentation for default.rs