Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/array/primitive/ffi.rs
6939 views
1
use polars_error::PolarsResult;
2
3
use super::PrimitiveArray;
4
use crate::array::{FromFfi, ToFfi};
5
use crate::bitmap::align;
6
use crate::ffi;
7
use crate::types::NativeType;
8
9
unsafe impl<T: NativeType> ToFfi for PrimitiveArray<T> {
10
fn buffers(&self) -> Vec<Option<*const u8>> {
11
vec![
12
self.validity.as_ref().map(|x| x.as_ptr()),
13
Some(self.values.storage_ptr().cast::<u8>()),
14
]
15
}
16
17
fn offset(&self) -> Option<usize> {
18
let offset = self.values.offset();
19
if let Some(bitmap) = self.validity.as_ref() {
20
if bitmap.offset() == offset {
21
Some(offset)
22
} else {
23
None
24
}
25
} else {
26
Some(offset)
27
}
28
}
29
30
fn to_ffi_aligned(&self) -> Self {
31
let offset = self.values.offset();
32
33
let validity = self.validity.as_ref().map(|bitmap| {
34
if bitmap.offset() == offset {
35
bitmap.clone()
36
} else {
37
align(bitmap, offset)
38
}
39
});
40
41
Self {
42
dtype: self.dtype.clone(),
43
validity,
44
values: self.values.clone(),
45
}
46
}
47
}
48
49
impl<T: NativeType, A: ffi::ArrowArrayRef> FromFfi<A> for PrimitiveArray<T> {
50
unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {
51
let dtype = array.dtype().clone();
52
let validity = unsafe { array.validity() }?;
53
let values = unsafe { array.buffer::<T>(1) }?;
54
55
Self::try_new(dtype, values, validity)
56
}
57
}
58
59