Struct libffi::middle::Cif

source ·
pub struct Cif { /* private fields */ }
Expand description

Describes the calling convention and types for calling a function.

This is the middle layer’s wrapping of the low and raw layers’ low::ffi_cif. An initialized CIF contains references to an array of argument types and a result type, each of which may be allocated on the heap. Cif manages the memory of those referenced objects.

Construct with Cif::new.

Examples

extern "C" fn add(x: f64, y: &f64) -> f64 {
    return x + y;
}

use libffi::middle::*;

let args = vec![Type::f64(), Type::pointer()];
let cif = Cif::new(args.into_iter(), Type::f64());

let n = unsafe { cif.call(CodePtr(add as *mut _), &[arg(&5f64), arg(&&6f64)]) };
assert_eq!(11f64, n);

Implementations§

source§

impl Cif

source

pub fn new<I>(args: I, result: Type) -> Selfwhere I: IntoIterator<Item = Type>, I::IntoIter: ExactSizeIterator<Item = Type>,

Creates a new CIF for the given argument and result types.

Takes ownership of the argument and result Types, because the resulting Cif retains references to them. Defaults to the platform’s default calling convention; this can be adjusted using Cif::set_abi.

source

pub unsafe fn call<R>(&self, fun: CodePtr, args: &[Arg]) -> R

Calls a function with the given arguments.

In particular, this method invokes function fun passing it arguments args, and returns the result.

Safety

There is no checking that the calling convention and types in the Cif match the actual calling convention and types of fun, nor that they match the types of args.

source

pub fn set_abi(&mut self, abi: FfiAbi)

Sets the CIF to use the given calling convention.

source

pub fn as_raw_ptr(&self) -> *mut ffi_cif

Gets a raw pointer to the underlying low::ffi_cif.

This can be used for passing a middle::Cif to functions from the low and raw modules.

Trait Implementations§

source§

impl Clone for Cif

source§

fn clone(&self) -> Self

Returns a copy 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 Cif

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Cif

§

impl !Send for Cif

§

impl !Sync for Cif

§

impl Unpin for Cif

§

impl UnwindSafe for Cif

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.