@startuml Rustpy Architecture
!theme plain
skinparam componentStyle rectangle
title Rustpy: Python-Rust Integration Architecture
package "User Code" {
[Rust Application] as RustApp
}
package "Rustpy Library" {
package "Macro Layer" {
[python! Macro] as PythonMacro
[py_exec! Macro] as ExecMacro
[py_eval! Macro] as EvalMacro
[py_locals! Macro] as LocalsMacro
}
package "Runtime Layer" {
[Runtime Manager] as Runtime
[Global State] as GlobalState
note right of GlobalState
OnceCell<Mutex<Python>>
Thread-safe Python instance
end note
}
package "Type Conversion" {
[IntoPython Trait] as IntoPython
[FromPython Trait] as FromPython
[Type Converters] as Converters
note right of Converters
• String ↔ PyString
• i32/i64 ↔ PyInt
• f32/f64 ↔ PyFloat
• bool ↔ PyBool
• Vec<T> ↔ PyList
• HashMap ↔ PyDict
end note
}
package "Error Handling" {
[RustpyError] as Error
}
}
package "PyO3 Layer" {
[Python FFI] as PyO3FFI
[Python Types] as PyTypes
[GIL Management] as GIL
}
package "Python Runtime" {
[CPython] as CPython
[Python Modules] as PyModules
[NumPy] as NumPy
[scikit-learn] as SKLearn
}
' Relationships
RustApp --> PythonMacro : uses macros
RustApp --> ExecMacro
RustApp --> EvalMacro
RustApp --> LocalsMacro
PythonMacro --> Runtime : initializes
ExecMacro --> Runtime : executes code
EvalMacro --> Runtime : evaluates expression
LocalsMacro --> Runtime : creates locals dict
Runtime --> GlobalState : manages
Runtime --> PyO3FFI : calls
Runtime --> IntoPython : uses
Runtime --> FromPython : uses
IntoPython --> Converters
FromPython --> Converters
Converters --> PyO3FFI : converts via
Runtime --> Error : returns
Error --> RustApp : propagates
PyO3FFI --> GIL : acquires
PyO3FFI --> PyTypes : provides
PyO3FFI --> CPython : FFI bindings
CPython --> PyModules : imports
PyModules --> NumPy
PyModules --> SKLearn
note bottom of RustApp
Example Usage:
python! {
import numpy as np
data = np.array([1, 2, 3])
}
end note
@enduml