Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/ffi/mod.rs
6939 views
1
#![allow(unsafe_op_in_unsafe_fn)]
2
//! contains FFI bindings to import and export [`Array`](crate::array::Array) via
3
//! Arrow's [C Data Interface](https://arrow.apache.org/docs/format/CDataInterface.html)
4
mod array;
5
mod bridge;
6
mod generated;
7
pub mod mmap;
8
mod schema;
9
mod stream;
10
11
pub(crate) use array::{ArrowArrayRef, InternalArrowArray, try_from};
12
pub(crate) use bridge::align_to_c_data_interface;
13
pub use generated::{ArrowArray, ArrowArrayStream, ArrowSchema};
14
use polars_error::PolarsResult;
15
pub use stream::{ArrowArrayStreamReader, export_iterator};
16
17
use self::schema::to_field;
18
use crate::array::Array;
19
use crate::datatypes::{ArrowDataType, Field};
20
21
/// Exports an [`Box<dyn Array>`] to the C data interface.
22
pub fn export_array_to_c(array: Box<dyn Array>) -> ArrowArray {
23
ArrowArray::new(bridge::align_to_c_data_interface(array))
24
}
25
26
/// Exports a [`Field`] to the C data interface.
27
pub fn export_field_to_c(field: &Field) -> ArrowSchema {
28
ArrowSchema::new(field)
29
}
30
31
/// Imports a [`Field`] from the C data interface.
32
/// # Safety
33
/// This function is intrinsically `unsafe` and relies on a [`ArrowSchema`]
34
/// being valid according to the [C data interface](https://arrow.apache.org/docs/format/CDataInterface.html) (FFI).
35
pub unsafe fn import_field_from_c(field: &ArrowSchema) -> PolarsResult<Field> {
36
to_field(field)
37
}
38
39
/// Imports an [`Array`] from the C data interface.
40
/// # Safety
41
/// This function is intrinsically `unsafe` and relies on a [`ArrowArray`]
42
/// being valid according to the [C data interface](https://arrow.apache.org/docs/format/CDataInterface.html) (FFI).
43
pub unsafe fn import_array_from_c(
44
array: ArrowArray,
45
dtype: ArrowDataType,
46
) -> PolarsResult<Box<dyn Array>> {
47
try_from(InternalArrowArray::new(array, dtype))
48
}
49
50