What is safer_ffi
?
safer_ffi
is a framework that helps you write foreign function interfaces (FFI) without polluting your Rust code with unsafe { ... }
code blocks while making functions far easier to read and maintain.
📚 Read The User Guide 📚
Prerequisites
Minimum Supported Rust Version: 1.43.0
Quickstart
Cargo.toml
Edit your Cargo.toml
like so:
[]
= "crate_name"
= "0.1.0"
= "2018"
[]
= ["staticlib"]
[]
= { = "*", = ["proc_macros"] }
[]
= ["safer-ffi/headers"]
- Where
"*"
ought to be replaced by the last released version, which you can find by runningcargo search safer-ffi
.
src/lib.rs
use *;
/// A `struct` usable from both Rust and C
pub
/* Export a Rust function to the C world. */
/// Returns the middle point of `[a, b]`.
/// Pretty-prints a point using Rust's formatting logic.
/// The following test function is necessary for the header generation.
Compilation & header generation
# Compile the C library (in `target/{debug,release}/libcrate_name.ext`)
# Generate the C header
/*! \file */
/*******************************************
* *
* File auto-generated by `::safer_ffi`. *
* *
* Do not manually edit this file. *
* *
*******************************************/
extern "C" __cplusplus
} /* extern "C" */
/* __RUST_CRATE_NAME__ */
Testing it
main.c
int
Compilation command
# Now feel free to run the compiled binary
which outputs:
Point { x: 42.0, y: 42.0 }
🚀🚀