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