Front Matter
- Preface
- Who This Book Is For
- How to Use This Book
- The Running Project: Pyre
- Toolchain Requirements
- Conventions Used in This Book
Chapter 1: The Bonfire — Why Rust, Why Now, Why From Python
- What We’re Building: Pyre
- The Parser
- The Allocation You Can’t See
- The Index
- The Query Engine
- Watching It Work (and Then Watching It Struggle)
- When Not to Reach for Rust
- What’s Coming
Chapter 2: Striking the Match — Rust for Pythonistas
- Variables:
letIs Not Assignment - Ownership: Reference Counting Made Explicit
- Ownership Is Not New
- Structs: Dataclasses With Teeth
- Enums: Python’s Enum + Pattern Matching + Algebraic Superpowers
- Traits: Protocols the Compiler Enforces
- Iterators: Generators That Are Zero-Cost
- The Module System: Cargo as Opinionated pyproject.toml
- Putting It Together: A Counter
- What We Didn’t Cover (Yet)
- What’s Coming
Chapter 3: First Sparks — PyO3 and Maturin
- The Tools: PyO3 and Maturin
- Setting Up the Crate
- The Rust Parser
- The Border Crossing Tax
- Building and Testing
- The Speedup
- Anatomy of a PyO3 Module
- The PyO3 Trinity:
#[pyclass],#[pymethods],#[pyfunction] - The
Python<'py>Token - Side Example: Wrapping the
regexCrate - What We Just Did
Chapter 4: Fanning the Flames — Lifetimes, Borrowing, and the Python Programmer’s Intuition
- The Newspaper Problem
- Lifetimes in Rust: The Syntax
- When Lifetimes Get Explicit
- The
'pyLifetime in PyO3 - Zero-Copy Parsing for Pyre
- Slices: Python’s
memoryview, But Everywhere - When to Clone and When to Borrow
- Side Example: Zero-Copy CSV Field Iterator
- What’s Coming
Chapter 5: Stoking the Coals — Error Handling and Testing Across the Boundary
- Errors as Values, Errors as Control Flow
- Building Pyre’s Error Hierarchy
- The
?Operator: Exception Propagation, Made Visible - The Hidden Cost of Python Exceptions
- Testing Across the Boundary
- The “Test at the Boundary” Principle
- Side Example: A Validation Library
- What’s Coming
Act II: The Forge
- Chapters 6–10
Chapter 6: The Crucible — Data Structures That Cross Worlds
- The Lie of the List
- Building the Rust Index
- Implementing Python’s Dunder Methods
- The Search Method
- Side Example: A Trie With Dict-Like Ergonomics
- What’s Coming
Chapter 7: Parallel Universes — Concurrency Without the GIL
- The GIL Is Not Your Enemy (It’s Your Frenemy)
py.allow_threads(): The Escape Hatch- Rayon: Data Parallelism Made Easy
SendandSync: Compile-Time Thread Safety- Pipeline Concurrency with Crossbeam
- What Happens When a Rayon Worker Panics?
- Threads vs. Processes vs. Async: When to Use What
- Side Example: Parallel File Hasher
- The Story So Far, In Numbers
Chapter 8: Async Tango — Rust Futures Meet Python Coroutines
- Two Event Loops Walk Into a Bar
- Bridging Tokio and Asyncio
- Backpressure and Flow Control
- Cancellation Doesn’t Cross the Border
- Fork Safety: The Runtime That Wasn’t
- What Happens When a Tokio Task Panics?
- Side Example: Async DNS Resolver
- The Borders of Two Worlds
Chapter 9: Unsafe at Any Speed — FFI, Raw Pointers, and When to Break the Rules
- The
unsafeKeyword Is Honesty - Memory-Mapped Files for Pyre
- Real CVEs That Rust Would Have Caught
ctypesandcffi: Python-Side FFI- Side Example: Three-Layer FFI
- What’s Coming
Chapter 10: The Shape of Things — Generics, Traits, and Protocol Design
- Generics vs. Duck Typing
- Monomorphization, or, Why Rust Generics Are Free
- Pyre’s
ParseableTrait - Static vs. Dynamic Dispatch
- Calling Python from Rust: The Trampoline
- Side Example: A Plugin System
- What We’ve Forged
Act III: Tempered Steel
- Chapters 11–15
Chapter 11: Ship It — Packaging, Distribution, and CI/CD
- The Wheel Was Invented Twice
- maturin Build vs. maturin Develop
- Wheel Tags and Platform Compatibility
- When Builds Go Wrong
- pyproject.toml Configuration
- Cross-Compilation
- GitHub Actions CI
- The Source Distribution Fallback
- Versioning Strategy
- Side Example: Minimal maturin Project From Scratch
- Shipping Is Believing
Chapter 12: Measure Twice — Profiling and Benchmarking Hybrid Systems
- The Benchmark That Lied
- Profiling the Python Side
- Profiling the Rust Side
- Benchmarking the Boundary
- Memory Profiling
- The Profile-Before-You-Optimize Discipline
- Side Example: The Full Optimization Loop
- pytest-benchmark
- The Numbers So Far
Chapter 13: Staying Alive — Observability, Logging, and Debugging Across the Boundary
- printf Debugging Is Underrated
- Connecting Rust’s
tracingwith Python’slogging - Metrics: Counting What Matters
- Propagating Request Context
- When Rust Panics in Production
- Debugging Tips: Rust Inside Python
- Side Example: Unified Trace Decorator
- Staying Alive
Chapter 14: The Architecture of Hybrid Systems — Patterns and Anti-Patterns
- The Second System Effect, Bilingual Edition
- The Rewrite That Worked and the One That Didn’t
- The Thin Boundary Principle
- API Design at the Boundary
- Batch APIs: Amortizing the Border Tax
- Where to Put Validation
- Ownership at the Architecture Level
- When Two Languages Disagree
- The Two Architectures
- When to Use Subprocess Instead of In-Process FFI
- Side Example: Refactoring a Messy Boundary
Chapter 15: From Pyre to Fire — Where to Go From Here
- The Capstone: FastAPI + Rust Index
- Pyre’s Final Architecture
- The Ecosystem: Who Else Does This?
- The Future
- The Best Language Is Two Languages
- What You’ve Learned
- Starting Your Own Fire