bytes.rs

Overview

The `bytes.rs` source file provides low-level, unsafe Rust FFI (Foreign Function Interface) bindings to access the internal data and size fields of Python `bytes` objects. Its primary purpose is to enable zero-copy, high-performance retrieval of the raw byte buffer and its length directly from a Python `bytes` object pointer (`PyObject *`), bypassing the Python API overhead.

By exposing these raw pointers and sizes through minimal, inline unsafe functions, this file supports critical buffer manipulation operations in the broader system, particularly for JSON serialization and deserialization workflows that require fast, direct access to Python byte sequences.


Detailed Explanation of Functions

PyBytes_AS_STRING

pub(crate) unsafe fn PyBytes_AS_STRING(op: *mut PyObject) -> *const c_char

Description

Returns a raw pointer to the internal byte buffer (`ob_sval`) of the Python `bytes` object referenced by `op`.

Parameters

Returns

Usage

This function enables Rust code to obtain the starting address of the byte array contained in the Python `bytes` object, allowing direct read access.

Safety

Example

unsafe {
    let py_bytes_ptr: *mut PyObject = /* obtained from Python API */;
    let data_ptr: *const c_char = PyBytes_AS_STRING(py_bytes_ptr);
    // data_ptr can now be used to read the bytes data directly
}

PyBytes_GET_SIZE

pub(crate) unsafe fn PyBytes_GET_SIZE(op: *mut PyObject) -> Py_ssize_t

Description

Retrieves the size (length) of the Python `bytes` object referenced by `op` by accessing its `ob_size` field.

Parameters

Returns

Usage

This function allows Rust code to determine the exact length of the byte buffer, necessary for safe slicing or iteration over the bytes.

Safety

Example

unsafe {
    let py_bytes_ptr: *mut PyObject = /* obtained from Python API */;
    let size: Py_ssize_t = PyBytes_GET_SIZE(py_bytes_ptr);
    // size can now be used to safely read the buffer of length `size`
}

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

The following Mermaid class diagram illustrates the relationship between Python internal structures and the Rust accessor functions in this file:

classDiagram
    class PyObject {
        <<opaque>>
    }
    class PyBytesObject {
        +ob_sval: [u8]  "Raw byte buffer"
    }
    class PyVarObject {
        +ob_size: Py_ssize_t  "Buffer size"
    }
    class RustAccessor {
        +PyBytes_AS_STRING(op: *mut PyObject) -> *const c_char
        +PyBytes_GET_SIZE(op: *mut PyObject) -> Py_ssize_t
    }

    PyBytesObject --|> PyObject : extends
    PyVarObject --|> PyObject : extends
    RustAccessor ..> PyBytesObject : accesses ob_sval
    RustAccessor ..> PyVarObject : accesses ob_size

**Diagram Explanation:**


Summary


If you are integrating `bytes.rs` into your Rust code interfacing with Python bytes objects, always ensure pointers passed are valid, and use these functions within `unsafe` blocks to respect Rust’s safety model.