pyo3-maturin-bindings
PyO3 + Maturin Python Bindings
Project Structure
my-rust-lib/
├── Cargo.toml # [lib] crate-type = ["cdylib", "rlib"]
├── pyproject.toml # maturin build config
├── src/
│ ├── lib.rs # Core Rust library
│ └── python/ # Python binding module
│ ├── mod.rs # #[pymodule] definition
│ ├── types.rs # #[pyclass] wrappers
│ └── errors.rs # Python exception types
├── python/
│ └── my_lib/ # Pure Python additions (optional)
│ ├── __init__.py
│ └── py.typed # PEP 561 marker
└── tests/
├── rust/ # Rust unit tests
└── python/ # Python integration tests
Core Workflow
1. Expose Rust Types
use pyo3::prelude::*;
#[pyclass]
#[derive(Clone)]
pub struct MyType {
inner: RustType, // Keep Rust type private
}
#[pymethods]
impl MyType {
#[new]
fn new(value: i64) -> PyResult<Self> {
Ok(Self { inner: RustType::new(value)? })
}
fn process(&self) -> PyResult<String> {
self.inner.process().map_err(|e| e.into())
}
}
2. Register Module
#[pymodule]
fn my_lib(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<MyType>()?;
m.add_function(wrap_pyfunction!(my_function, m)?)?;
Ok(())
}
3. Build & Test
maturin develop # Install in current venv for testing
maturin build --release # Build wheel
pytest tests/python/ # Run Python tests
Reference Guides
Load these as needed based on the task:
| Task | Reference |
|---|---|
| Type mapping between Rust ↔ Python | type-conversions.md |
| Converting Rust errors to Python exceptions | error-handling.md |
| GIL management and memory safety | memory-gil.md |
| Building wheels and publishing to PyPI | build-publish.md |
| Testing strategies (Rust + Python) | testing.md |
| Debugging common binding issues | debugging.md |
| CQLite CQL feature parity checklist | cqlite-parity.md |
Quick Reference
Common Cargo.toml Setup
[lib]
name = "my_lib"
crate-type = ["cdylib", "rlib"]
[dependencies]
pyo3 = { version = "0.22", features = ["extension-module"] }
[features]
extension-module = ["pyo3/extension-module"]
Common pyproject.toml
[build-system]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"
[project]
name = "my-lib"
requires-python = ">=3.8"
classifiers = ["Programming Language :: Rust"]
[tool.maturin]
features = ["pyo3/extension-module"]
python-source = "python" # If you have pure Python code
PyO3 Attribute Quick Reference
| Attribute | Use |
|---|---|
#[pyclass] |
Expose struct to Python |
#[pymethods] |
Impl block with Python-visible methods |
#[new] |
__init__ constructor |
#[getter] / #[setter] |
Property access |
#[staticmethod] |
No self parameter |
#[classmethod] |
Receives cls: &Bound<'_, PyType> |
#[pyo3(name = "...")] |
Rename in Python |
#[pyo3(signature = (...))] |
Custom signature with defaults |
More from pmcfadin/cqlite
napi-rs-node-bindings
Node.js bindings for Rust libraries using napi-rs. Use when working on Node.js/JavaScript bindings to Rust code, including creating or modifying napi macros and structs, converting types between Rust and JavaScript, handling errors across the FFI boundary, async/Promise patterns, building native modules, publishing to npm, testing binding code, or debugging binding issues. Tuned for CQLite (Cassandra CQL bindings) with feature parity tracking.
14rust performance & safety patterns
Zero-copy deserialization, async I/O patterns, lifetime management, memory-efficient parsing, and safe handling of unsafe code for SSTable parsing. Use when working with performance optimization, memory efficiency, async/await, borrowing/lifetimes, zero-copy patterns, or memory usage under 128MB target.
1cql type system & schema handling
Implement and deserialize all CQL types including primitives (int, text, timestamp, uuid, varint, decimal), collections (list, set, map), tuples, UDTs (user-defined types), and frozen types. Use when working with CQL type deserialization, schema validation, collection parsing, UDT handling, or type-correct data generation.
1cassandra sstable format parsing
Guide parsing of Cassandra 5.0+ SSTable components (Data.db, Index.db, Statistics.db, Summary.db, TOC) with compression support (LZ4, Snappy, Deflate). Use when working with SSTable files, binary format parsing, hex dumps, compression issues, offset calculations, BTI index, partition layout, or debugging parsing errors.
1test data generation & validation
Generate real Cassandra 5.0 test data using Docker containers, export SSTables with proper directory structure, validate parsing against sstabledump, and manage test datasets. Use when working with test data generation, dataset creation, SSTable export, validation, fixture management, or sstabledump comparison.
1ci/cd validation & merge workflow
Pre-push validation checklist (cargo fmt, clippy with zero warnings, feature flag testing, test suite), CI monitoring, merge process, and release quality gates. Use when preparing to push code, validating changes before PR, running CI checks, merging PRs, or preparing releases.
1