pystr.rs

Overview

The [pystr.rs](/projects/287/67734) file provides a Rust abstraction layer for working with Python string objects (`str`) at the FFI (Foreign Function Interface) boundary using the `pyo3` crate's low-level Python C API bindings (`pyo3_ffi`). It defines safe and efficient wrappers around raw Python string pointers (`PyObject`), enabling Rust code to create, manipulate, and convert Python strings while handling Python's internal string representations and optimizations.

Key functionalities include:

This file is integral to bridging Python string handling in Rust code that interoperates with Python via the PyO3 project, especially for performance-sensitive or low-level string operations.


Detailed Explanation of Components

Imports and Conditional Compilation


Free Function: to_str_via_ffi

fn to_str_via_ffi(op: *mut PyObject) -> Option<&'static str>

Type Alias and Static Variable (Conditional)

#[cfg(feature = "avx512")]
pub type StrDeserializer = unsafe fn(&str) -> *mut pyo3_ffi::PyObject;

#[cfg(feature = "avx512")]
static mut STR_CREATE_FN: StrDeserializer = super::scalar::str_impl_kind_scalar;

Function: set_str_create_fn

pub fn set_str_create_fn()

Constants for State Bit Masks and Shifts

These constants help interpret and manipulate the internal Python string object's state flags:

These are conditioned on platform endianness and Python version features, reflecting Python internal implementation details.


Struct: PyStr

#[repr(transparent)]
#[derive(Copy, Clone)]
pub(crate) struct PyStr {
    ptr: NonNull<PyObject>,
}

Methods:

from_ptr_unchecked
pub unsafe fn from_ptr_unchecked(ptr: *mut PyObject) -> PyStr
from_str_with_hash
pub fn from_str_with_hash(buf: &str) -> PyStr
from_str
pub fn from_str(buf: &str) -> PyStr
hash
pub fn hash(&mut self)
to_str
pub fn to_str(self) -> Option<&'static str>
as_ptr and as_non_null_ptr
pub fn as_ptr(self) -> *mut PyObject
pub fn as_non_null_ptr(self) -> NonNull<PyObject>

Struct: PyStrSubclass

#[repr(transparent)]
pub(crate) struct PyStrSubclass {
    ptr: NonNull<PyObject>,
}

Methods:

from_ptr_unchecked
pub unsafe fn from_ptr_unchecked(ptr: *mut PyObject) -> PyStrSubclass
to_str
pub fn to_str(&self) -> Option<&'static str>

Important Implementation Details


Interaction with Other System Components


Usage Examples

// Creating a Python string from a Rust &str
let py_str = PyStr::from_str("Hello, Python!");

// Accessing the underlying Python object pointer
let py_obj_ptr = py_str.as_ptr();

// Getting a Rust &str back from PyStr (if possible)
if let Some(rust_str) = py_str.to_str() {
    println!("Python string content: {}", rust_str);
}

// Creating string with precomputed hash for optimization
let py_str_with_hash = PyStr::from_str_with_hash("hashed string");

// Working with Python string subclass (assuming a valid pointer `subclass_ptr`)
unsafe {
    let py_str_subclass = PyStrSubclass::from_ptr_unchecked(subclass_ptr);
    if let Some(content) = py_str_subclass.to_str() {
        println!("Subclass string content: {}", content);
    }
}

Mermaid Diagram

classDiagram
    class PyStr {
        -ptr: NonNull<PyObject>
        +unsafe from_ptr_unchecked(ptr: *mut PyObject) PyStr
        +from_str(buf: &str) PyStr
        +from_str_with_hash(buf: &str) PyStr
        +hash()
        +to_str() Option<&'static str>
        +as_ptr() *mut PyObject
        +as_non_null_ptr() NonNull<PyObject>
    }

    class PyStrSubclass {
        -ptr: NonNull<PyObject>
        +unsafe from_ptr_unchecked(ptr: *mut PyObject) PyStrSubclass
        +to_str() Option<&'static str>
    }

    PyStrSubclass --> PyObject : wraps pointer
    PyStr --> PyObject : wraps pointer

Summary

[pystr.rs](/projects/287/67734) is a low-level Rust module providing efficient and safe wrappers around Python string objects for Rust code interacting with Python via FFI. It handles the creation, conversion, and hashing of Python strings, optimized for various CPU features and Python internals. It also supports Python string subclasses with careful type checks. This file is a crucial component within the PyO3 ecosystem for string interoperability between Rust and Python.