Builder

Struct Builder 

Source
pub struct Builder { /* private fields */ }
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§

Source§

impl Builder

Source

pub fn new() -> Self

Constructs a Builder.

Source

pub fn arg(self, type_: Type) -> Self

Adds a type to the argument type list.

Source

pub fn args<I>(self, types: I) -> Self
where I: IntoIterator<Item = Type>,

Adds several types to the argument type list.

Source

pub fn res(self, type_: Type) -> Self

Sets the result type.

Source

pub fn abi(self, abi: FfiAbi) -> Self

Sets the calling convention.

Source

pub fn into_cif(self) -> Cif

Builds a CIF.

Source

pub fn into_closure<U, R>( self, callback: Callback<U, R>, userdata: &U, ) -> Closure<'_>

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.

Source

pub fn into_closure_mut<U, R>( self, callback: CallbackMut<U, R>, userdata: &mut U, ) -> 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.

Source

pub fn into_closure_once<U: Any, R>( self, callback: CallbackOnce<U, R>, userdata: U, ) -> ClosureOnce

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§

Source§

impl Clone for Builder

Source§

fn clone(&self) -> Builder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Builder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Builder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.