pyobject.rs


Overview

The [pyobject.rs](/projects/287/67800) file provides utility functions to facilitate the creation and management of Python objects within a Rust environment, specifically targeting interoperability with Python's C API via `pyo3` and related FFI (Foreign Function Interface) constructs. The primary focus of this module is the efficient conversion of Rust primitive types and strings into Python objects (`PyObject` pointers), leveraging internal caching mechanisms and optimizations to enhance performance, especially for commonly used immutable Python objects such as `True`, `False`, and `None`.

Key features include:

This file is typically used internally in a larger system that binds Rust and Python together, enabling seamless data exchange and Python object manipulation within Rust code.


Detailed Explanation of Functions

get_unicode_key(key_str: &str) -> PyStr

Returns a cached or newly created Python Unicode string object (`PyStr`) corresponding to the provided Rust string slice `key_str`.


parse_bool(val: bool) -> NonNull<pyo3_ffi::PyObject>

Converts a Rust boolean value into a Python `True` or `False` object pointer.


parse_true() -> NonNull<pyo3_ffi::PyObject>

Returns a pointer to the Python singleton `True` object.


parse_false() -> NonNull<pyo3_ffi::PyObject>

Returns a pointer to the Python singleton `False` object.


parse_i64(val: i64) -> NonNull<pyo3_ffi::PyObject>

Converts a Rust signed 64-bit integer into a Python integer object.


parse_u64(val: u64) -> NonNull<pyo3_ffi::PyObject>

Converts a Rust unsigned 64-bit integer into a Python integer object.


parse_f64(val: f64) -> NonNull<pyo3_ffi::PyObject>

Converts a Rust 64-bit floating-point number into a Python float object.


parse_none() -> NonNull<pyo3_ffi::PyObject>

Returns a pointer to the Python singleton `None` object.


Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class pyobject {
        +get_unicode_key(key_str: &str) PyStr
        +parse_bool(val: bool) NonNull<PyObject>
        +parse_true() NonNull<PyObject>
        +parse_false() NonNull<PyObject>
        +parse_i64(val: i64) NonNull<PyObject>
        +parse_u64(val: u64) NonNull<PyObject>
        +parse_f64(val: f64) NonNull<PyObject>
        +parse_none() NonNull<PyObject>
    }
    class PyStr {
        +from_str_with_hash()
    }
    class CachedKey {
        +new(PyStr)
        +get()
    }
    pyobject ..> PyStr : uses
    pyobject ..> CachedKey : uses

Summary

The [pyobject.rs](/projects/287/67800) file provides foundational utilities for converting Rust primitives and strings into Python objects with performance-conscious caching and safe pointer handling. It abstracts away low-level FFI details and ensures that commonly used Python singleton objects are efficiently accessed. This module is a crucial component in Rust-Python interoperability within the system and supports higher-level Python bindings and serialization/deserialization tasks.