unicode.rs


Overview

The `unicode.rs` file provides serialization support for Python string objects within a Rust environment integrating with Python's C API (via `pyo3_ffi`). Specifically, it defines two transparent wrapper structs, `StrSerializer` and `StrSubclassSerializer`, which wrap raw Python string pointers (`PyObject*`). These wrappers implement the `serde::Serialize` trait, enabling seamless serialization of Python strings and their subclasses into Rust serializers (e.g., JSON serializers).

This file is crucial in bridging Python Unicode string types and Rust's serialization ecosystem, ensuring Python string objects—both base `str` and user-defined subclasses—can be serialized reliably with proper Unicode handling.


Detailed Documentation

Structs

StrSerializer

A transparent wrapper around a raw Python string pointer (`*mut pyo3_ffi::PyObject`) representing a Python `str` object.


StrSubclassSerializer

A transparent wrapper similar to `StrSerializer`, but for Python string subclasses (`str` subclass instances).


Important Implementation Details


Interaction With Other Parts of the System


Usage Example

use serde_json;
use unicode::StrSerializer;

fn serialize_python_str(py_str_ptr: *mut pyo3_ffi::PyObject) -> Result<String, serde_json::Error> {
    let serializer = StrSerializer::new(py_str_ptr);
    serde_json::to_string(&serializer)
}

This example shows how to wrap a raw Python string pointer with `StrSerializer` and then serialize it to a JSON string.


Mermaid Diagram

flowchart TD
    A[StrSerializer] -->|wraps| B(PyObject pointer)
    A -->|implements| C{Serialize Trait}
    C --> D[serialize method]
    D --> E[Unsafe PyStr conversion]
    E -->|Some(&str)| F[serializer.serialize_str]
    E -->|None| G[Return SerializeError::InvalidStr]
    
    H[StrSubclassSerializer] -->|wraps| B
    H -->|implements| I{Serialize Trait}
    I --> J[serialize method]
    J --> K[Unsafe PyStrSubclass conversion]
    K -->|Some(&str)| F
    K -->|None| G

Summary

The `unicode.rs` file is a specialized utility module within a Rust-Python binding project that enables serialization of Python Unicode string objects and their subclasses using Rust’s Serde framework. It provides safe abstractions over raw Python object pointers and handles potential Unicode conversion errors robustly. This file plays a vital role in ensuring Python strings can be efficiently and reliably serialized into Rust-supported formats in a type-safe manner.


*End of `unicode.rs` documentation.*