use std::ffi::c_void;
use std::os::raw::c_char;
#[repr(C)]
#[derive(Debug)]
pub struct ArrowSchema {
pub format: *const c_char,
pub name: *const c_char,
pub metadata: *const c_char,
pub flags: i64,
pub n_children: i64,
pub children: *mut *mut ArrowSchema,
pub dictionary: *mut ArrowSchema,
pub release: Option<unsafe extern "C" fn(*mut ArrowSchema)>,
pub private_data: *mut c_void,
}
#[repr(C)]
#[derive(Debug)]
pub struct ArrowArray {
pub length: i64,
pub null_count: i64,
pub offset: i64,
pub n_buffers: i64,
pub n_children: i64,
pub buffers: *mut *const c_void,
pub children: *mut *mut ArrowArray,
pub dictionary: *mut ArrowArray,
pub release: Option<unsafe extern "C" fn(*mut ArrowArray)>,
pub private_data: *mut c_void,
}
impl ArrowSchema {
pub fn empty() -> Self {
Self {
format: std::ptr::null(),
name: std::ptr::null(),
metadata: std::ptr::null(),
flags: 0,
n_children: 0,
children: std::ptr::null_mut(),
dictionary: std::ptr::null_mut(),
release: None,
private_data: std::ptr::null_mut(),
}
}
pub fn is_released(&self) -> bool {
self.release.is_none()
}
}
impl ArrowArray {
pub fn empty() -> Self {
Self {
length: 0,
null_count: 0,
offset: 0,
n_buffers: 0,
n_children: 0,
buffers: std::ptr::null_mut(),
children: std::ptr::null_mut(),
dictionary: std::ptr::null_mut(),
release: None,
private_data: std::ptr::null_mut(),
}
}
pub fn is_released(&self) -> bool {
self.release.is_none()
}
}
impl Drop for ArrowSchema {
fn drop(&mut self) {
if let Some(release) = self.release {
unsafe { release(self) };
}
}
}
impl Drop for ArrowArray {
fn drop(&mut self) {
if let Some(release) = self.release {
unsafe { release(self) };
}
}
}
unsafe impl Send for ArrowSchema {}
unsafe impl Send for ArrowArray {}
const _: () = {
assert!(
std::mem::size_of::<ArrowSchema>() == std::mem::size_of::<arrow::ffi::FFI_ArrowSchema>()
);
assert!(
std::mem::align_of::<ArrowSchema>() == std::mem::align_of::<arrow::ffi::FFI_ArrowSchema>()
);
assert!(std::mem::size_of::<ArrowArray>() == std::mem::size_of::<arrow::ffi::FFI_ArrowArray>());
assert!(
std::mem::align_of::<ArrowArray>() == std::mem::align_of::<arrow::ffi::FFI_ArrowArray>()
);
};
pub(crate) fn export_array(array: arrow::ffi::FFI_ArrowArray) -> ArrowArray {
unsafe { std::mem::transmute(array) }
}
pub(crate) fn export_schema(schema: arrow::ffi::FFI_ArrowSchema) -> ArrowSchema {
unsafe { std::mem::transmute(schema) }
}
pub(crate) fn import_array(array: ArrowArray) -> arrow::ffi::FFI_ArrowArray {
unsafe { std::mem::transmute(array) }
}
pub(crate) fn import_schema_ref(schema: &ArrowSchema) -> &arrow::ffi::FFI_ArrowSchema {
unsafe { &*(schema as *const ArrowSchema as *const arrow::ffi::FFI_ArrowSchema) }
}