geoarrow_wasm/array/
ffi.rs

1use crate::error::{GeoArrowError, Result};
2use arrow2::array::Array;
3use arrow2::datatypes::Field;
4use arrow2::ffi;
5use wasm_bindgen::prelude::*;
6
7/// Wrapper around an ArrowArray FFI schema and array struct in Wasm memory.
8#[wasm_bindgen]
9pub struct FFIArrowArray(Box<ffi::ArrowSchema>, Box<ffi::ArrowArray>);
10
11impl FFIArrowArray {
12    pub fn new(field: &Field, array: Box<dyn Array>) -> Self {
13        let field = Box::new(ffi::export_field_to_c(field));
14        let array = Box::new(ffi::export_array_to_c(array));
15        Self(field, array)
16    }
17}
18
19impl From<(&Field, Box<dyn Array>)> for FFIArrowArray {
20    fn from(value: (&Field, Box<dyn Array>)) -> Self {
21        Self::new(value.0, value.1)
22    }
23}
24
25impl TryFrom<FFIArrowArray> for Box<dyn Array> {
26    type Error = GeoArrowError;
27
28    fn try_from(ffi_array: FFIArrowArray) -> Result<Self> {
29        let field = unsafe { ffi::import_field_from_c(&ffi_array.0) }?;
30        let array = unsafe { ffi::import_array_from_c(*ffi_array.1, field.data_type) }?;
31        Ok(array)
32    }
33}
34
35#[wasm_bindgen]
36impl FFIArrowArray {
37    #[wasm_bindgen]
38    pub fn field_addr(&self) -> *const ffi::ArrowSchema {
39        self.0.as_ref() as *const _
40    }
41
42    #[wasm_bindgen]
43    pub fn array_addr(&self) -> *const ffi::ArrowArray {
44        self.1.as_ref() as *const _
45    }
46
47    #[wasm_bindgen]
48    pub fn free(self) {
49        drop(self.0)
50    }
51
52    #[wasm_bindgen]
53    pub fn drop(self) {
54        drop(self.0)
55    }
56}