arrow

Module ffi

Expand description

Contains declarations to bind to the C Data Interface.

Generally, this module is divided in two main interfaces: One interface maps C ABI to native Rust types, i.e. convert c-pointers, c_char, to native rust. This is handled by FFI_ArrowSchema and FFI_ArrowArray.

The second interface maps native Rust types to the Rust-specific implementation of Arrow such as format to Datatype, Buffer, etc. This is handled by from_ffi and to_ffi.

Export to FFI

// create an array natively

let array = Int32Array::from(vec![Some(1), None, Some(3)]);
let data = array.into_data();

// Export it
let (out_array, out_schema) = to_ffi(&data)?;

// import it
let data = unsafe { from_ffi(out_array, &out_schema) }?;
let array = Int32Array::from(data);

// verify
assert_eq!(array, Int32Array::from(vec![Some(1), None, Some(3)]));

Import from FFI

/// A foreign data container that can export to C Data interface
struct ForeignArray {};

impl ForeignArray {
    /// Export from foreign array representation to C Data interface
    /// e.g. <https://github.com/apache/arrow/blob/fc1f9ebbc4c3ae77d5cfc2f9322f4373d3d19b8a/python/pyarrow/array.pxi#L1552>
    fn export_to_c(&self, array: *mut FFI_ArrowArray, schema: *mut FFI_ArrowSchema) {
        // ...
    }
}

/// Import an [`ArrayRef`] from a [`ForeignArray`]
fn import_array(foreign: &ForeignArray) -> Result<ArrayRef, ArrowError> {
    let mut schema = FFI_ArrowSchema::empty();
    let mut array = FFI_ArrowArray::empty();
    foreign.export_to_c(addr_of_mut!(array), addr_of_mut!(schema));
    Ok(make_array(unsafe { from_ffi(array, &schema) }?))
}

Structs§

Functions§