::safer_ffi
See the user guide.
This is currently still being developed and at an experimental stage, hence its not being published to crates.io yet.
Minimum Supported Rust Version: 1.43.0
Cargo.toml
Edit your Cargo.toml
like so:
```toml [package] name = "crate_name" version = "0.1.0" edition = "2018"
[lib] crate-type = ["staticlib"]
[dependencies] safer-ffi = { git = "https://github.com/getditto/rust-saferffi.git", features = ["procmacros"] }
[features] c-headers = ["safer_ffi/headers"] ```
src/lib.rs
```rust use ::safer_ffi::prelude::*;
pub struct Point { x: f64, y: f64, }
fn midpoint ( left: &' Point, right: &'_ Point, ) -> Point { Point { x: (left.x + right.x) / 2., y: (left.y + right.y) / 2., } }
fn printpoint (point: &' Point) { println!("{:?}", point); }
fn generateheaders () -> ::std::io::Result<()> { ::saferffi::headers::builder() .tofile("rustpoints.h")? .generate() } ```
```shell
target/{debug,release}/libcrate_name.ext
)cargo build # --release
cargo test --features c-headers -- generate_headers ```
Generated C header
``C
/*! \file */
/*******************************************
* *
* File auto-generated by
::safer_ffi`. *
* *
* Do not manually edit this file. *
* *
*************/
extern "C" {
typedef struct { double x;
double y;
} Point_t;
Pointt midpoint ( Pointt const * left, Pointt const * right);
void printpoint ( Pointt const * point);
} /* extern "C" */
```
main.c
```C
int main (int argc, char const * const argv[]) { Pointt a = { .x = 84, .y = 45 }; Pointt b = { .x = 0, .y = 39 }; Pointt m = midpoint(&a, &b); printpoint(&m); return EXITSUCCESS; } ```
```bash cc main.c -o main -L target/debug -l crate_name
./main ```
which outputs:
text
Point { x: 42.0, y: 42.0 }