buffer.rs
Overview
The [buffer.rs](/projects/287/67769) file provides low-level, unsafe Rust bindings to Python’s internal memoryview buffer structures. It exposes the C representation of Python’s `PyMemoryViewObject` and its associated managed buffer (`_PyManagedBufferObject`), along with an accessor function to retrieve the internal `Py_buffer` view from a Python memoryview object.
This module is part of the foreign function interface (FFI) layer that allows Rust code to interoperate directly with Python objects implementing the buffer protocol, particularly memoryviews. By mapping Python’s internal structs and exposing raw pointers, this file enables zero-copy, high-performance access to potentially complex multi-dimensional buffers (e.g., numpy arrays, binary data slices) from Rust. It is critical for serialization and deserialization components that need to efficiently read/write raw memory from Python objects.
Detailed Descriptions
Struct: _PyManagedBufferObject
#[repr(C)]
pub(crate) struct _PyManagedBufferObject {
pub ob_base: *mut pyo3_ffi::PyObject,
pub flags: c_int,
pub exports: Py_ssize_t,
pub master: *mut Py_buffer,
}
Purpose:
Represents Python’s internal managed buffer object that underlies a memoryview. This struct manages the buffer's lifecycle, including reference counts, flags describing buffer properties, and a pointer to the "master"Py_bufferobject that owns the underlying memory.Fields:
ob_base: Raw pointer to the base Python object (PyObject). This is the generic Python object header.flags: Integer flags describing buffer properties (e.g., whether the buffer is readonly, contiguous, etc.).exports: Count of active exports of the buffer (how many objects currently access it).master: Pointer to the masterPy_bufferthat actually owns the underlying memory.
Usage:
Rust code generally does not manipulate this struct directly but keeps a pointer to it insidePyMemoryViewObjectto track buffer ownership and lifecycle.
Struct: PyMemoryViewObject
#[repr(C)]
pub(crate) struct PyMemoryViewObject {
pub ob_base: PyVarObject,
pub mbuf: *mut _PyManagedBufferObject,
pub hash: Py_hash_t,
pub flags: c_int,
pub exports: Py_ssize_t,
pub view: Py_buffer,
pub weakreflist: *mut PyObject,
pub ob_array: [Py_ssize_t; 1],
}
Purpose:
Represents Python’smemoryviewobject at the C level. This struct contains metadata and a direct buffer view (Py_buffer) that describes the memory accessible through the memoryview.Fields:
ob_base: Base variable-sized Python object header (PyVarObject), includes reference count and type info.mbuf: Pointer to the managed buffer (_PyManagedBufferObject) that manages the underlying memory.hash: Cached hash value of the memoryview, used for dictionary keys or comparisons.flags: Integer flags representing the state and properties of the memoryview.exports: Number of active exports referencing this memoryview.view: The embeddedPy_bufferstruct containing detailed info about the buffer: pointer to memory, length, format, shape, strides, etc.weakreflist: Pointer to a list of weak references to this object.ob_array: Array of dimension sizes (used for multi-dimensional memoryviews).
Usage:
Rust code typically receives a raw pointer to a Python object (*mut PyObject) and casts it to this struct to gain access to the underlying buffer and metadata.
Function: PyMemoryView_GET_BUFFER
#[allow(non_snake_case)]
#[inline(always)]
pub(crate) unsafe fn PyMemoryView_GET_BUFFER(op: *mut PyObject) -> *const Py_buffer {
unsafe { &(*op.cast::<PyMemoryViewObject>()).view }
}
Purpose:
Provides unsafe access to the internalPy_bufferinside a Python memoryview object.Parameters:
op: A raw pointer to a Python object (*mut PyObject), expected to be a memoryview object.
Returns:
A raw pointer to the contained
Py_buffer(*const Py_buffer), which describes the buffer interface (memory pointer, length, format, shape, strides, etc.).
Safety:
Caller must ensure that
opis a valid pointer to a Pythonmemoryviewobject.Accessing the returned pointer or fields is unsafe and requires care to avoid undefined behavior.
Example Usage:
unsafe {
let memview_ptr: *mut PyObject = /* obtained from Python */;
let buf_ptr: *const Py_buffer = PyMemoryView_GET_BUFFER(memview_ptr);
let buf_ref: &Py_buffer = &*buf_ptr;
// Now buf_ref.buf points to raw memory,
// buf_ref.len gives the buffer size, etc.
}
This function is typically used in serialization/deserialization logic to extract raw byte buffers from Python memoryviews efficiently.
Important Implementation Details
The structs use
#[repr(C)]to guarantee the same memory layout as the equivalent C structs in CPython. This is critical to safely cast pointers between Rust and Python types.The file only exposes raw pointers and struct layouts without any safety abstractions. This design reflects the need for maximum performance and minimal overhead when accessing Python buffers from Rust.
The
PyMemoryView_GET_BUFFERfunction is marked#[inline(always)]to minimize function call overhead for this frequently used accessor.The module depends on the
pyo3_fficrate for Python FFI type definitions (PyObject,PyVarObject,Py_buffer, etc.).The use of raw pointers and unsafe code requires that callers uphold strict invariants about the validity and type correctness of pointers passed into these functions.
Interaction with Other Parts of the System
FFI Layer:
This file belongs to the FFI boundary of the Rust project, bridging Python’s buffer protocol C API to Rust code.Serialization/Deserialization Modules:
The buffer accessors defined here are used by serialization and deserialization cores to read from or write to Python buffers directly without copying data.Complementary Files:
Works alongside other files likebytes.rswhich provide similar unsafe accessors for Pythonbytesobjects.Higher-Level APIs:
Higher-level safe Rust abstractions or Python-facing APIs rely on this low-level buffer access to efficiently handle Python bytes, memoryviews, and other buffer protocol objects.Performance Critical Path:
This module is critical for achieving zero-copy or minimal-copy operations in JSON serialization workflows, enabling high throughput and low latency.
Mermaid Class Diagram
classDiagram
class _PyManagedBufferObject {
+*PyObject ob_base
+int flags
+Py_ssize_t exports
+*Py_buffer master
}
class PyMemoryViewObject {
+PyVarObject ob_base
+*_PyManagedBufferObject mbuf
+Py_hash_t hash
+int flags
+Py_ssize_t exports
+Py_buffer view
+*PyObject weakreflist
+Py_ssize_t[1] ob_array
}
PyMemoryViewObject --> _PyManagedBufferObject : mbuf
PyMemoryViewObject "1" *-- "1" Py_buffer : view
Explanation:
The diagram illustrates the composition relationship wherePyMemoryViewObjectcontains a pointer to_PyManagedBufferObject(mbuf) and embeds aPy_bufferstructure (view). This reflects how the Python memoryview object internally manages and exposes its buffer.
Summary
The [buffer.rs](/projects/287/67769) file is a foundational FFI module enabling direct Rust access to Python memoryview buffer internals. It provides:
Struct definitions mirroring Python’s internal memoryview and managed buffer objects.
An unsafe inline function to retrieve the internal
Py_bufferfrom a memoryview.Critical low-level primitives that enable zero-copy buffer access for high-performance serialization/deserialization.
Unsafe raw pointer operations that require strict correctness guarantees but deliver minimal overhead.
This file integrates tightly with the Rust project’s Python interop layer and serialization core, facilitating efficient manipulation of Python buffer protocol objects within Rust code.