Struct deno_libffi::middle::Builder[][src]

pub struct Builder { /* fields omitted */ }
Expand description

Provides a builder-style API for constructing CIFs and closures.

To use a builder, first construct it using Builder::new. The default calling convention is ffi_abi_FFI_DEFAULT_ABI, and the default function type is extern "C" fn() (or in C, void(*)()). Add argument types to the function type with the arg and args methods. Set the result type with res. Change the calling convention, if necessary, with abi.

Once the builder is configured, construct a Cif with into_cif or a closure with into_closure, into_closure_mut, or into_closure_once.

Examples

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: *const &u64 = mem::transmute(args);
    let arg1 = **args.offset(0);
    let arg2 = **args.offset(1);

    *result = userdata(arg1, arg2);
}

let lambda = |x: u64, y: u64| x + y;

let closure = Builder::new()
    .arg(Type::u64())
    .arg(Type::u64())
    .res(Type::u64())
    .into_closure(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));
}

Implementations

Constructs a Builder.

Adds a type to the argument type list.

Adds several types to the argument type list.

Sets the result type.

Sets the calling convention.

Builds a CIF.

Builds an immutable closure.

Arguments

  • 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.

Builds a mutable closure.

Arguments

  • 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.

Builds a one-shot closure.

Arguments

  • callback — the function to call when the closure is invoked
  • userdata — the object to pass to callback along with the arguments when the closure is called

Result

The new closure.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a 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 resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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.