45 releases (13 stable)

5.0.0 Oct 15, 2025
4.1.2 Aug 31, 2025
4.1.1 Jun 30, 2025
3.2.0 Mar 28, 2023
0.4.0 Jun 21, 2016

#13 in FFI

Download history 18065/week @ 2025-07-23 18148/week @ 2025-07-30 19124/week @ 2025-08-06 17190/week @ 2025-08-13 19381/week @ 2025-08-20 19918/week @ 2025-08-27 18037/week @ 2025-09-03 16033/week @ 2025-09-10 15369/week @ 2025-09-17 18650/week @ 2025-09-24 18035/week @ 2025-10-01 16231/week @ 2025-10-08 18727/week @ 2025-10-15 19666/week @ 2025-10-22 23342/week @ 2025-10-29 19516/week @ 2025-11-05

83,635 downloads per month
Used in 71 crates (22 directly)

MIT/Apache

3MB
74K SLoC

C 37K SLoC // 0.2% comments GNU Style Assembly 15K SLoC // 0.2% comments M4 9K SLoC // 0.2% comments Shell 9K SLoC // 0.2% comments Rust 3K SLoC // 0.1% comments Bitbake 435 SLoC // 0.1% comments Python 306 SLoC // 0.1% comments Automake 218 SLoC // 0.1% comments Perl 170 SLoC // 0.4% comments C++ 133 SLoC // 0.1% comments Visual Studio Project 125 SLoC // 0.0% comments Visual Studio Solution 32 SLoC

libffi-rs: Rust bindings for libffi

GitHub Workflow Status Documentation Crates.io License: MIT License: Apache 2.0

The C libffi library provides two main facilities: assembling calls to functions dynamically, and creating closures that can be called as ordinary C functions. In Rust, the latter means that we can turn a Rust lambda (or any object implementing Fn/FnMut) into an ordinary C function pointer that we can pass as a callback to C.

Usage

Building libffi will build lifbffi-sys, which will in turn build the libffi C library from github, which requires that you have a working make, C compiler, automake, and autoconf first. It’s on crates.io, so you can add

[dependencies]
libffi = "5.0.0"

to your Cargo.toml.

This crate depends on the libffi-sys crate, which by default attempts to build its own version of the C libffi library. In order to use your system’s C libffi instead, enable this crate’s system feature in your Cargo.toml:

[features]
libffi = { version = "5.0.0", features = ["system"] }

See the libffi-sys documentation for more information about how it finds C libffi.

This crate supports Rust version 1.78 and later.

Examples

In this example, we convert a Rust lambda containing a free variable into an ordinary C code pointer. The type of fun below is extern "C" fn(u64, u64) -> u64.

use libffi::high::Closure2;

let x = 5u64;
let f = |y: u64, z: u64| x + y + z;

let closure = Closure2::new(&f);
let fun     = closure.code_ptr();

assert_eq!(18, fun(6, 7));

Dependencies