Rechercher

PythonReactSEOPDF
GitHub Skill

binding-crate-architecture-patterns

Instructions for binding crate architecture patterns.

kreuzberg-dev☆ 560 Étoiles du dépôt50 Forks du dépôt

Installation

Suivez les instructions du dépôt pour votre agent. Consultez les fichiers et dépendances avant l’installation.

Voir la source ↗Guides

Instructions


priority: high

Binding Crate Architecture Patterns

Principles

  1. Minimal wrapper: Bindings are glue only — call Rust core, convert types, convert errors
  2. Type translation: Clear Rust ↔ host language mapping
  3. Error conversion: Rust errors → native exceptions/error types
  4. Memory safety: Respect language-specific ownership models
  5. Testing: Language-native test suite, not just Rust tests

Crate Naming

{lib}-{language}: -py (PyO3), -node (NAPI-RS), -rb (Magnus), -php (ext-php-rs), -wasm (wasm-bindgen), -ffi (C FFI for Go/Java/C#)

Framework Patterns

Framework Macro Constructor Error Pattern
PyO3 #[pyclass] / #[pymethods] #[new] .map_err(|e| PyException::new_err(e.to_string()))
NAPI-RS #[napi] #[napi(constructor)] napi::Error::new(Status::GenericFailure, msg)
Magnus #[magnus::wrap] define_method("new", ...) Into<magnus::RError>
ext-php-rs #[php_class] / #[php_impl] pub fn new() Return error string
wasm-bindgen #[wasm_bindgen] #[wasm_bindgen(constructor)] JsValue::from_str(&e.to_string())
C FFI #[no_mangle] extern "C" _new() -> Handle Return null + error code

Type Mapping

Rust Python Node.js Ruby C/FFI
String/&str str string String char*/const char*
u64 int BigInt Integer uint64_t
Result<T> Exception Error thrown raises null/error code
Option<T> None/T null/T nil/T NULL/T

Distribution (packages/)

python/ (PyPI), typescript/ (npm), ruby/ (RubyGems), php/ (Composer), go/ (Go module), java/ (Maven), csharp/ (.NET NuGet)

Anti-Patterns

  • Exposing Rust internals to users
  • Blocking in async contexts (use spawn_blocking)
  • Dangling pointers in FFI returns
  • Panicking instead of returning errors (unwrap() in binding code)

Exemples

Consultez les exemples du dépôt source.

Voir la source ↗

Historique source

Date d’indexation
2026-03-11
Mise à jour de l’index
2026-03-11
Branch
main

Les dates concernent ce répertoire. Consultez le dépôt pour l’historique du code.

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!