fragment.rs


Overview

The [fragment.rs](/projects/287/67759) file defines a low-level Rust extension module component for Python, specifically implementing a custom Python object type named `Fragment`. This type serves as a lightweight container wrapping a single Python object (`contents`). It is designed for efficient interaction with Python's C API, integrating closely with PyO3's FFI layer to extend Python with Rust-backed objects.

The file focuses on:

This component is likely part of a larger project (possibly `orjson`), where `Fragment` objects hold intermediate or raw JSON fragments or similar data structures for high-performance serialization or manipulation.


Detailed Explanation

Struct: Fragment

#[repr(C)]
pub(crate) struct Fragment {
    // GIL-disabled runtime atomic/thread fields
    #[cfg(Py_GIL_DISABLED)]
    pub ob_tid: usize,
    #[cfg(all(Py_GIL_DISABLED, Py_3_14))]
    pub ob_flags: u16,
    #[cfg(all(Py_GIL_DISABLED, not(Py_3_14)))]
    pub _padding: u16,
    #[cfg(Py_GIL_DISABLED)]
    pub ob_mutex: pyo3_ffi::PyMutex,
    #[cfg(Py_GIL_DISABLED)]
    pub ob_gc_bits: u8,
    #[cfg(Py_GIL_DISABLED)]
    pub ob_ref_local: AtomicU32,
    #[cfg(Py_GIL_DISABLED)]
    pub ob_ref_shared: AtomicIsize,
    // Standard reference counting
    #[cfg(not(Py_GIL_DISABLED))]
    pub ob_refcnt: pyo3_ffi::Py_ssize_t,
    // Pointer to Python type object
    pub ob_type: *mut pyo3_ffi::PyTypeObject,
    // Pointer to contained Python object
    pub contents: *mut pyo3_ffi::PyObject,
}

**Implementation Detail:**


Function: raise_args_exception

#[cold]
#[inline(never)]
#[cfg_attr(feature = "optimize", optimize(size))]
fn raise_args_exception()

Function: orjson_fragment_tp_new

#[unsafe(no_mangle)]
#[cold]
#[cfg_attr(feature = "optimize", optimize(size))]
pub(crate) unsafe extern "C" fn orjson_fragment_tp_new(
    _subtype: *mut PyTypeObject,
    args: *mut PyObject,
    kwds: *mut PyObject,
) -> *mut PyObject

**Example usage in Python:**

f = orjson.Fragment(some_python_object)

Function: orjson_fragment_dealloc

#[unsafe(no_mangle)]
#[cold]
#[cfg_attr(feature = "optimize", optimize(size))]
pub(crate) unsafe extern "C" fn orjson_fragment_dealloc(object: *mut PyObject)

Constant: FRAGMENT_TP_FLAGS

#[cfg(Py_GIL_DISABLED)]
const FRAGMENT_TP_FLAGS: AtomicCULong = AtomicCULong::new(Py_TPFLAGS_DEFAULT | pyo3_ffi::Py_TPFLAGS_IMMUTABLETYPE);

#[cfg(all(Py_3_10, not(Py_GIL_DISABLED)))]
const FRAGMENT_TP_FLAGS: core::ffi::c_ulong = Py_TPFLAGS_DEFAULT | pyo3_ffi::Py_TPFLAGS_IMMUTABLETYPE;

#[cfg(not(Py_3_10))]
const FRAGMENT_TP_FLAGS: core::ffi::c_ulong = Py_TPFLAGS_DEFAULT;

Function: orjson_fragmenttype_new

#[unsafe(no_mangle)]
#[cold]
#[cfg_attr(feature = "optimize", optimize(size))]
pub(crate) unsafe extern "C" fn orjson_fragmenttype_new() -> *mut PyTypeObject

Important Implementation Details


Interaction with Other System Components


Usage Examples

Creating a Fragment Object in Python (conceptual)

import orjson

some_obj = {"key": "value"}

# Wrap a Python object inside a Fragment
fragment = orjson.Fragment(some_obj)

# fragment.contents would internally hold a reference to some_obj

Underlying Rust Construction (conceptual)

// Called when Python executes: Fragment(some_obj)
let py_obj_ptr = ...; // pointer to some_obj
let fragment_ptr = orjson_fragment_tp_new(fragment_type_ptr, args_tuple_ptr, null_mut());

Mermaid Diagram: Class Structure of Fragment

classDiagram
    class Fragment {
        +usize ob_tid
        +u16 ob_flags
        +PyMutex ob_mutex
        +u8 ob_gc_bits
        +AtomicU32 ob_ref_local
        +AtomicIsize ob_ref_shared
        +Py_ssize_t ob_refcnt
        +*PyTypeObject ob_type
        +*PyObject contents
    }

    class PyTypeObject {
        +PyVarObject ob_base
        +*c_char tp_name
        +isize tp_basicsize
        +Option<fn(*mut PyObject)> tp_dealloc
        +Option<fn(*mut PyTypeObject, *mut PyObject, *mut PyObject)> tp_new
        +c_ulong tp_flags
        // ...
    }

    Fragment --> PyTypeObject : ob_type points to
    Fragment --> PyObject : contents points to

Summary

The [fragment.rs](/projects/287/67759) file provides the Rust-side definition and Python binding for a custom Python type `Fragment` used in the `orjson` project or a similar Rust-Python integration context. It carefully manages Python object lifecycle via FFI, supports multi-threaded reference counting in GIL-disabled mode, and exposes minimal but essential Python type slots to integrate seamlessly with Python's runtime.

This file is crucial for representing lightweight, Rust-backed Python objects that encapsulate Python objects internally, enabling efficient serialization or manipulation workflows within the larger application.