30 releases

0.7.0 May 12, 2019
0.6.4 May 30, 2018
0.6.3 Mar 5, 2018
0.6.2 Nov 13, 2017
0.4.0 Jun 21, 2016

#18 in FFI

Download history 186/week @ 2019-04-29 93/week @ 2019-05-06 184/week @ 2019-05-13 202/week @ 2019-05-20 173/week @ 2019-05-27 152/week @ 2019-06-03 201/week @ 2019-06-10 169/week @ 2019-06-17 259/week @ 2019-06-24 217/week @ 2019-07-01 162/week @ 2019-07-08 70/week @ 2019-07-15 101/week @ 2019-07-22 74/week @ 2019-07-29 114/week @ 2019-08-05

530 downloads per month
Used in 1 crate

MIT/Apache

87KB
1K SLoC

libffi-rs: Rust bindings for libffi

Build 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, autoconf, and texinfo first. It’s on crates.io, so you can add

[dependencies]
libffi = "0.7.0"

to your Cargo.toml and

extern crate libffi;

to your crate root.

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

~3.5MB
~80K SLoC