7 releases

0.0.7 Aug 9, 2021
0.0.6 Aug 9, 2021
0.0.3 Jul 18, 2021

#74 in FFI

Download history 80/week @ 2022-06-02 18/week @ 2022-06-09 45/week @ 2022-06-16 24/week @ 2022-06-23 28/week @ 2022-06-30 30/week @ 2022-07-07 25/week @ 2022-07-14 41/week @ 2022-07-21 22/week @ 2022-07-28 29/week @ 2022-08-04 36/week @ 2022-08-11 18/week @ 2022-08-18 34/week @ 2022-08-25 28/week @ 2022-09-01 55/week @ 2022-09-08 27/week @ 2022-09-15

145 downloads per month

MIT/Apache

2.5MB
58K SLoC

C 33K SLoC // 0.2% comments GNU Style Assembly 14K SLoC // 0.2% comments Shell 8K SLoC // 0.2% comments Rust 2K SLoC // 0.0% comments Automake 247 SLoC // 0.1% comments Python 199 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 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 = "1.0.1"

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 = "1.0.1", features = ["system"] }

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

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