40 releases (8 stable)

3.2.0 Mar 28, 2023
3.0.1 Sep 2, 2022
3.0.0 Mar 7, 2022
2.0.1 Feb 13, 2022
0.4.0 Jun 21, 2016

#18 in FFI

Download history 7316/week @ 2023-12-15 5741/week @ 2023-12-22 5183/week @ 2023-12-29 8569/week @ 2024-01-05 8645/week @ 2024-01-12 8833/week @ 2024-01-19 9004/week @ 2024-01-26 10176/week @ 2024-02-02 10802/week @ 2024-02-09 11665/week @ 2024-02-16 11616/week @ 2024-02-23 12503/week @ 2024-03-01 12521/week @ 2024-03-08 12396/week @ 2024-03-15 11445/week @ 2024-03-22 10130/week @ 2024-03-29

48,177 downloads per month
Used in 44 crates (15 directly)

MIT/Apache

3MB
70K SLoC

C 35K SLoC // 0.2% comments GNU Style Assembly 14K SLoC // 0.2% comments M4 9K SLoC // 0.2% comments Shell 9K SLoC // 0.2% comments Rust 2.5K SLoC // 0.0% comments Bitbake 435 SLoC // 0.1% comments Automake 257 SLoC // 0.1% comments Python 246 SLoC Perl 170 SLoC // 0.4% comments C++ 127 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 = "3.2.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 = "3.2.0", features = ["system"] }

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

This crate supports Rust version 1.48 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