int.rs

Overview

The `int.rs` file provides functionality to serialize Python integer objects (`PyLong`) into Rust's serialization framework using Serde. It defines the `IntSerializer` struct, which wraps a raw pointer to a Python integer object and an options struct (`Opt`) to control serialization behavior. The main purpose of this file is to convert Python integer values into Rust's primitive integer types (`i64` or `u64`) for serialization, while respecting certain constraints such as integer size limits and strictness options aligned with JSON number specifications (RFC 7159).

Key features include:

Entities

Constants

Name

Type

Description

`STRICT_INT_MIN`

i64

Minimum allowed integer in strict mode, `-(2^53)+1` as per RFC 7159 JSON integer safe range.

`STRICT_INT_MAX`

i64

Maximum allowed integer in strict mode, `(2^53)-1` as per RFC 7159 JSON integer safe range.

Structs

IntSerializer

A serializer wrapper for Python integer objects.

Detailed Explanation


IntSerializer Struct

pub(crate) struct IntSerializer {
    ptr: *mut pyo3_ffi::PyObject,
    opts: Opt,
}
impl IntSerializer {
    pub fn new(ptr: *mut pyo3_ffi::PyObject, opts: Opt) -> Self {
        IntSerializer {
            ptr,
            opts,
        }
    }
}

Serialize Trait Implementation for IntSerializer

Provides the core logic to convert the Python integer into a serializable Rust integer.

There are two variants of the `serialize` method, selected at compile time via the `inline_int` feature flag.

1. serialize with inline_int feature enabled

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer
let py_int_ptr: *mut pyo3_ffi::PyObject = ...; // obtained from Python environment
let options = Opt::default();
let serializer = IntSerializer::new(py_int_ptr, options);

// Assuming `json_serializer` implements serde::Serializer
let result = serializer.serialize(json_serializer);
match result {
    Ok(serialized) => println!("Serialized integer successfully"),
    Err(e) => eprintln!("Serialization error: {:?}", e),
}

2. serialize without inline_int feature

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

Important Implementation Details & Algorithms


Interaction with Other System Components


Visual Diagram: Structure Class Diagram

classDiagram
    class IntSerializer {
        -ptr: *mut PyObject
        -opts: Opt
        +new(ptr: *mut PyObject, opts: Opt) IntSerializer
        +serialize<S: Serializer>(serializer: S) -> Result<S::Ok, S::Error>
    }
    IntSerializer ..> Opt : uses
    IntSerializer ..|> Serialize

Summary

The `int.rs` file is a focused utility module that bridges Python integer objects with Rust's serialization ecosystem. It carefully manages the conversion of Python's arbitrary precision integers into Rust's primitive integer types, ensuring safety, correctness, and compliance with serialization constraints. It is a critical component when serializing Python data structures into formats like JSON or other serialized representations within this project.


References