- Home
- Skills
- Huiali
- Rust Skills
- Rust Ffi
rust-ffi_skill
- Shell
20
GitHub Stars
3
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill huiali/rust-skills --skill rust-ffi- SKILL_EN.md1.7 KB
- SKILL_ZH.md8.0 KB
- SKILL.md8.3 KB
Overview
This skill is an FFI cross-language interop expert for Rust, covering C/C++ bindings, bindgen, cbindgen, PyO3, JNI, memory layout, data conversion, and safe FFI patterns. It guides selection of tools, applies proven safety patterns, and helps design tested, maintainable interfaces between Rust and other languages. The focus is on practical fixes, clear ownership, and minimizing unsafe surface area.
How this skill works
The skill inspects FFI boundaries, type mappings, and safety invariants and recommends concrete changes: generated bindings (bindgen/cbindgen), high-level wrappers, and ABI-stable exports. It validates pointer lifetimes, string conversions, panic handling, and memory ownership protocols, and suggests testing strategies like Miri and valgrind. It also proposes language-specific bridges (PyO3, jni, cxx, napi-rs) when appropriate.
When to use it
- When exposing Rust libraries to C or C++ consumers
- When calling existing C/C++ libraries from Rust (bindgen)
- When building Python, Java, Node, or C# extensions from Rust
- When diagnosing crashes or undefined behavior at FFI boundaries
- When preparing cross-platform or cross-ABI builds and headers
Best practices
- Minimize unsafe: encapsulate unsafe calls in small, well-documented functions
- Document safety invariants: pointer validity, ownership, lifetimes, and thread assumptions
- Use CStr/CString for string conversions and validate encodings
- Mark exported functions #[no_mangle] extern "C" and use cbindgen for headers
- Catch panics at FFI boundaries with catch_unwind to avoid UB
- Test thoroughly: Miri for UB detection, valgrind for leaks, and cross-target builds
Example use cases
- Auto-generate Rust bindings for a C library with bindgen and wrap them in safe APIs
- Export a Rust API to C consumers using cbindgen and #[no_mangle] extern "C" functions with documented ownership
- Build a Python extension using PyO3 that safely shares memory buffers between Rust and Python
- Integrate a C++ class via cxx::bridge and provide a lightweight Rust wrapper for ergonomic use
- Implement callback-based C APIs by passing a boxed UserData pointer and restoring it safely in the callback
FAQ
Use CString when passing owned strings to C and CStr when borrowing C strings in Rust. Always keep the owned CString alive for the call duration and validate encoding when required.
Who should free memory allocated on the Rust side?
Agree on a clear protocol: if Rust allocates and returns a pointer, provide a matching free function exported from Rust. Use Vec::into_raw_parts or mem::forget on allocation and Vec::from_raw_parts on free.