Struct deno_libffi::middle::Closure[][src]

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

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 deno_libffi::middle::*;
use deno_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 = args as *const &u64;
    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);

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

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

Implementations

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.

Transmutes the callable code pointer for a closure to a reference to any type. This is intended to be used to transmute it to its correct function type in order to call it.

Safety

This method allows transmuting to a reference to any sized type, and cannot check whether the code pointer actually has that type. If the type is wrong then undefined behavior will result.

Trait Implementations

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.