Struct libffi::middle::Closure [] [src]

pub struct Closure<'a> { /* fields omitted */ }

Represents a closure callable from C.

A libffi closure captures a void* (“userdata”) and passes it to a callback when the code pointer (obtained via code_ptr) is invoked. Lifetype parameter 'a ensures that the closure does not outlive the userdata.

Construct with Closure::new and Closure::new_mut.

Examples

In this example we turn a Rust lambda into a C function. We first define function lambda_callback, which will be called by libffi when the closure is called. The callback function takes four arguments: a CIF describing its arguments, a pointer for where to store its result, a pointer to an array of pointers to its arguments, and a userdata pointer. In this ase, the Rust closure value lambda is passed as userdata to lambda_callback, which then invokes it.

use std::mem;
use std::os::raw::c_void;

use libffi::middle::*;
use libffi::low;

unsafe extern "C" fn lambda_callback<F: Fn(u64, u64) -> u64>(
    _cif: &low::ffi_cif,
    result: &mut u64,
    args: *const *const c_void,
    userdata: &F)
{
    let args: *const &u64 = mem::transmute(args);
    let arg1 = **args.offset(0);
    let arg2 = **args.offset(1);

    *result = userdata(arg1, arg2);
}

let cif = Cif::new(vec![Type::u64(), Type::u64()].into_iter(),
                   Type::u64());
let lambda = |x: u64, y: u64| x + y;
let closure = Closure::new(cif, lambda_callback, &lambda);

unsafe {
    let fun: &unsafe extern "C" fn(u64, u64) -> u64
        = mem::transmute(closure.code_ptr());

    assert_eq!(11, fun(5, 6));
    assert_eq!(12, fun(5, 7));
}

Methods

impl<'a> Closure<'a>
[src]

Creates a new closure with immutable userdata.

Arguments

  • cif — describes the calling convention and argument and result types
  • callback — the function to call when the closure is invoked
  • userdata — the pointer to pass to callback along with the arguments when the closure is called

Result

The new closure.

Creates a new closure with mutable userdata.

Arguments

  • cif — describes the calling convention and argument and result types
  • callback — the function to call when the closure is invoked
  • userdata — the pointer to pass to callback along with the arguments when the closure is called

Result

The new closure.

Obtains the callable code pointer for a closure.

Safety

The result needs to be transmuted to the correct type before it can be called. If the type is wrong then undefined behavior will result.

Trait Implementations

impl<'a> Debug for Closure<'a>
[src]

Formats the value using the given formatter.

impl<'a> Drop for Closure<'a>
[src]

A method called when the value goes out of scope. Read more