[go: up one dir, main page]

ffi_type

Struct ffi_type 

Source
#[repr(C)]
pub struct ffi_type { pub size: usize, pub alignment: c_ushort, pub type_: c_ushort, pub elements: *mut *mut ffi_type, }
Expand description

A struct used by libffi to describe types and their memory layout.

New ffi_type variables should only be constructed for describing the layout of custom structs. For plain scalar types it is recommended to refer to the static variables defined by libffi instead of creating new ffi_types.

When creating new ffi_types, the size and alignment fields should be left at their default values, as libffi will fill out these fields during ffi_prep_cif.

§Example

use std::ptr;

#[repr(C)]
struct CustomStruct {
    num: u32,
    num2: i64,
    float_num: f32,
}

// We need to describe the types of the values in `CustomStruct`. The order
// must be the same as the order in the struct definition. Note that this
// array must be alive and at the same address for the entire lifetime of
// the resulting `ffi_type`.
let mut elements_array = unsafe {[
    // `libffi::low::types::uint32`, `sint64`, and `float` can be used
    // instead if using libffi (not -sys)
    ptr::addr_of_mut!(libffi_sys::ffi_type_uint32),
    ptr::addr_of_mut!(libffi_sys::ffi_type_sint64),
    ptr::addr_of_mut!(libffi_sys::ffi_type_float),
    // The last element in the array must be a `NULL` since `ffi_type` does
    // not store the number of elements in the struct.
    ptr::null_mut(),
]};

let mut custom_struct_description = libffi_sys::ffi_type {
    // `libffi::low::type_tag::STRUCT` can be used instead if using libffi
    type_: libffi_sys::FFI_TYPE_STRUCT,
    elements: elements_array.as_mut_ptr(),
    ..Default::default()
};

// `custom_struct_description` can now be used in a [`ffi_cif`] to send
// `CustomStruct` as an argument or receive a `CustomStruct` as response.

Fields§

§size: usize§alignment: c_ushort§type_: c_ushort§elements: *mut *mut ffi_type

Trait Implementations§

Source§

impl Clone for ffi_type

Source§

fn clone(&self) -> ffi_type

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 Default for ffi_type

Source§

fn default() -> Self

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

impl Copy for ffi_type

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.