Module arrow::ffi

source · []
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 ArrowArray.

// create an array natively
let array = Int32Array::from(vec![Some(1), None, Some(3)]);

// export it
let (array_ptr, schema_ptr) = array.to_raw()?;

// consumed and used by something else...

// import it
let array = unsafe { make_array_from_raw(array_ptr, schema_ptr)? };

// perform some operation
let array = array.as_any().downcast_ref::<Int32Array>().ok_or(
    ArrowError::ParseError("Expects an int32".to_string()),
)?;
let array = arithmetic::add(&array, &array)?;

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

// Simulate if raw pointers are provided by consumer
let array = make_array(Int32Array::from(vec![Some(1), None, Some(3)]).into_data());

let out_array = Box::new(FFI_ArrowArray::empty());
let out_schema = Box::new(FFI_ArrowSchema::empty());
let out_array_ptr = Box::into_raw(out_array);
let out_schema_ptr = Box::into_raw(out_schema);

// export array into raw pointers from consumer
unsafe { export_array_into_raw(array, out_array_ptr, out_schema_ptr)?; };

// import it
let array = unsafe { make_array_from_raw(out_array_ptr, out_schema_ptr)? };

// perform some operation
let array = array.as_any().downcast_ref::<Int32Array>().ok_or(
    ArrowError::ParseError("Expects an int32".to_string()),
)?;
let array = arithmetic::add(&array, &array)?;

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

// (drop/release)
unsafe {
    Box::from_raw(out_array_ptr);
    Box::from_raw(out_schema_ptr);
    Arc::from_raw(array_ptr);
    Arc::from_raw(schema_ptr);
}

Ok(())
}

Structs

Struct used to move an Array from and to the C Data Interface. Its main responsibility is to expose functionality that requires both FFI_ArrowArray and FFI_ArrowSchema.

ABI-compatible struct for ArrowArray from C Data Interface See https://arrow.apache.org/docs/format/CDataInterface.html#structure-definitions This was created by bindgen

ABI-compatible struct for ArrowSchema from C Data Interface See https://arrow.apache.org/docs/format/CDataInterface.html#structure-definitions This was created by bindgen

Traits