1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Rust bindings for [libffi](https://sourceware.org/libffi/).
//!
//! # Purpose
//!
//! Libffi provides two main facilities:
//!
//! - Assembling *calls* to functions dynamically.
//! - Creating *closures* that can be called as ordinary C functions.
//!
//! The former is useful mostly for implementing FFIs for untyped
//! languages; this library provides some support in the
//! [`middle`](middle/index.html) and [`low`](low/index.html) layers,
//! but I’m not sure how useful it is. The latter can be used to
//! interface between higher-order languages and C by making closures
//! from the higher-order language callable as C function pointers. In
//! Rust, this means that we can now, for example, pass a lambda as a
//! callback to a C function.
//!
//! Most users are likely interested in the [`high`](high/index.html)
//! layer, which provides the easiest interface to the closure facility.
//!
//! # Organization
//!
//! This library is organized in four layers, each of which attempts to
//! provide more safety and a simpler interface than the next layer
//! down. From top to bottom:
//!
//! - The [`high`](high/index.html) layer provides safe(?) and
//! automatic marshalling of Rust closures into C function pointers.
//! - The [`middle`](middle/index.html) layer provides memory-managed
//! abstractions for assembling calls and closures, but is unsafe
//! because it doesn’t check argument types.
//! - The [`low`](low/index.html) layer makes no attempts at safety,
//! but provides a more idiomatically “Rusty” API than the underlying
//! C library.
//! - The [`raw`](raw/index.html) layer is a direct mapping of the
//! C libffi library into Rust, generated by [Rust
//! Bindgen](https://github.com/crabtw/rust-bindgen).
//!
//! It should be possible to use any layer without dipping into lower
//! layers (and it will be considered a bug to the extent that it
//! isn’t).
//!
//! # Example
//!
//! 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));
//! ```
extern crate libc;
/// Unwrapped definitions imported from the C library (via bindgen).