| Crates.io | libffi |
| lib.rs | libffi |
| version | 4.1.2 |
| created_at | 2016-06-06 05:29:25.738712+00 |
| updated_at | 2025-08-31 06:03:33.760708+00 |
| description | Rust bindings for libffi |
| homepage | |
| repository | https://github.com/tov/libffi-rs |
| max_upload_size | |
| id | 5303 |
| size | 113,448 |
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.
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 = "4.1.2"
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 = "4.1.2", 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.
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));