ezffi 0.1.1

Generate C-FFI bindings from Rust types/functions via a single proc-macro attribute
docs.rs failed to build ezffi-0.1.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

ezffi

Generate idiomatic C-FFI bindings from Rust types and functions with a single attribute, without having to worry about whether your types use C-compatible fields or not.

#[ezffi::export]
pub struct Car { km: u64 }

#[ezffi::export]
impl Car {
    pub fn new() -> Self { Self { km: 0 } }
    pub fn drive(&mut self, km: u64) { self.km += km; }
    pub fn km(&self) -> u64 { self.km }
}

Pair it with cbindgen and you get:

typedef struct MyCrateCar { uint64_t km; } MyCrateCar;

MyCrateCar my_crate_car_new(void);
void       my_crate_car_drive(MyCrateCar *this_, uint64_t km);
uint64_t   my_crate_car_km(const MyCrateCar *this_);

Ideas

  • One attribute. #[ezffi::export] on structs, enums, impl blocks emits the C wrappers, the type definitions, and the free function. No #[repr(C)] needed.
  • Picks the layout for you. Types whose fields are all C-compatible cross the boundary as #[repr(C)] values; everything else goes behind an opaque pointer struct. The macro reads the source, no annotations — ezffi always makes the right decision.
  • Honest C signatures. &selfconst T*, &mut selfT*, owned TT by value. The C side actually reflects which calls mutate (with a few exceptions for performance reasons).
  • Built-ins ship with their own headers. Option, Result, String, and (with std) Vec, HashMap, Arc, BTreeMap, etc., come pre-wrapped, each in its own ezffi/…h, so the consumer #includes only what they touch. This lets you export almost any function you write in Rust without having to worry about type compatibility.
  • Cross-crate aware. A crate can take a type another ezffi crate exports and the macro resolves it without re-declaration.

Quick start and Documentation

📖 The ezffi Book contains all the information you need to set up and use this crate. It also explains how ezffi works internally and how to correctly use all the features it ships. It'll definitely help you decide if this crate is the right tool for your work.

Right now the book tracks the master branch only, I'll eventually set up per-version hosting so users can read the docs that match their installed version.

For examples, look at the crates under ffi-c-tests/, I'll be adding proper example files that are more readable than the test ones.

Features

  • std — bundles wrappers for Vec, HashMap, BTreeMap, HashSet, BTreeSet, Arc, Rc, VecDeque. Off by default. I'll keep adding more if I see it's needed so users can #[ezffi::export] everything they need. See the book for more.
  • async — lets you export async functions and plug in your own async dispatcher, see the book for more.