[−][src]Struct libffi::middle::Builder
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 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 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)); }
Methods
impl Builder
[src]
pub fn new() -> Self
[src]
Constructs a Builder
.
pub fn arg(self, type_: Type) -> Self
[src]
Adds a type to the argument type list.
pub fn args<I>(self, types: I) -> Self where
I: IntoIterator<Item = Type>,
[src]
I: IntoIterator<Item = Type>,
Adds several types to the argument type list.
pub fn res(self, type_: Type) -> Self
[src]
Sets the result type.
pub fn abi(self, abi: FfiAbi) -> Self
[src]
Sets the calling convention.
pub fn into_cif(self) -> Cif
[src]
Builds a CIF.
pub fn into_closure<U, R>(
self,
callback: Callback<U, R>,
userdata: &U
) -> Closure
[src]
self,
callback: Callback<U, R>,
userdata: &U
) -> Closure
Builds an immutable closure.
Arguments
callback
— the function to call when the closure is invokeduserdata
— the pointer to pass tocallback
along with the arguments when the closure is called
Result
The new closure.
pub fn into_closure_mut<U, R>(
self,
callback: CallbackMut<U, R>,
userdata: &mut U
) -> Closure
[src]
self,
callback: CallbackMut<U, R>,
userdata: &mut U
) -> Closure
Builds a mutable closure.
Arguments
callback
— the function to call when the closure is invokeduserdata
— the pointer to pass tocallback
along with the arguments when the closure is called
Result
The new closure.
pub fn into_closure_once<U: Any, R>(
self,
callback: CallbackOnce<U, R>,
userdata: U
) -> ClosureOnce
[src]
self,
callback: CallbackOnce<U, R>,
userdata: U
) -> ClosureOnce
Trait Implementations
Auto Trait Implementations
impl !Send for Builder
impl !Sync for Builder
impl Unpin for Builder
impl UnwindSafe for Builder
impl RefUnwindSafe for Builder
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,