Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Design and review Rust FFI boundaries that keep raw declarations isolated behind safe ownership, lifetime, error, and cleanup wrappers. Use when writing, refactoring, or reviewing unsafe extern blocks, C ABI wrappers, repr(C) types, CString, CStr, raw handles, Drop cleanup, bindgen, cbindgen, or panic-safe foreign boundaries.
.claude/skills/hashgraph-online-rust-ffi-safe-wrappers/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 18% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 1% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 8% | 0% |
| case-05 | ✓→✓ | = Same ✓ | -8% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 17% | 0% |
Use this skill to wrap foreign interfaces in Rust APIs that make ownership, lifetimes, errors, and cleanup explicit. FFI code should be unsafe at the raw boundary and safe for normal Rust callers.
unsafe externbindings in a small private module.
threading, and error conventions against the foreign header or docs.
unsafe extern "C" blocks for modern Rust, especially Rust 2024 editioncode. Leave safety-conditional items unqualified or explicitly unsafe, and mark individual extern items safe only when calling them is safe for all Rust inputs.
#[repr(C)] for structs and enums that cross the ABI boundary. Do notexpose Rust-only layout types across C.
CString for owned nul-terminated strings sent to C and CStr forborrowed C strings. Never treat arbitrary C memory as Rust-owned.
Drop cleanup usingthe matching foreign deallocator.
extern "C" boundaries. Catch panics orexpose an ABI that permits unwinding only when deliberately using "C-unwind".
Read references/ffi-boundary-patterns.md before adding or reviewing FFI bindings.
core::ffi or std::ffi C types over guessing integer sizes.Result at the wrapper boundary.Option<NonNull<T>> internally when useful,but expose safe Rust types to callers.
PhantomData when the compiler cannot see the relationship.
OnceLock, LazyLock, orOnce when the foreign library requires process-wide setup.
bindgen for large or changing C headers and cbindgen when exporting aRust API to C, but still review generated unsafe signatures.
rustunsafe extern "C" { pub fn library_open(path: *const core::ffi::c_char) -> *mut RawHandle; pub fn library_close(handle: *mut RawHandle); // Mark an item `safe` only when calling it is valid for all Rust inputs: // pub safe fn library_version() -> core::ffi::c_int; } // SAFETY: This exported symbol name is unique for this library. #[unsafe(no_mangle)] pub extern "C" fn plugin_version() -> core::ffi::c_int { 1 }
Unsafe attributes such as no_mangle, export_name, and link_section need the unsafe(...) form in Rust 2024 edition code. Include a SAFETY comment where symbol uniqueness or linker behavior is part of the contract.
deallocator.
Other measured skills in the registry, with their headline benchmark lift.