-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathffi.rs
63 lines (54 loc) · 1.82 KB
/
ffi.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use polars_error::PolarsResult;
use super::Utf8Array;
use crate::array::{FromFfi, ToFfi};
use crate::bitmap::align;
use crate::ffi;
use crate::offset::{Offset, OffsetsBuffer};
unsafe impl<O: Offset> ToFfi for Utf8Array<O> {
fn buffers(&self) -> Vec<Option<*const u8>> {
vec![
self.validity.as_ref().map(|x| x.as_ptr()),
Some(self.offsets.buffer().storage_ptr().cast::<u8>()),
Some(self.values.storage_ptr().cast::<u8>()),
]
}
fn offset(&self) -> Option<usize> {
let offset = self.offsets.buffer().offset();
if let Some(bitmap) = self.validity.as_ref() {
if bitmap.offset() == offset {
Some(offset)
} else {
None
}
} else {
Some(offset)
}
}
fn to_ffi_aligned(&self) -> Self {
let offset = self.offsets.buffer().offset();
let validity = self.validity.as_ref().map(|bitmap| {
if bitmap.offset() == offset {
bitmap.clone()
} else {
align(bitmap, offset)
}
});
Self {
dtype: self.dtype.clone(),
validity,
offsets: self.offsets.clone(),
values: self.values.clone(),
}
}
}
impl<O: Offset, A: ffi::ArrowArrayRef> FromFfi<A> for Utf8Array<O> {
unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {
let dtype = array.dtype().clone();
let validity = unsafe { array.validity() }?;
let offsets = unsafe { array.buffer::<O>(1) }?;
let values = unsafe { array.buffer::<u8>(2)? };
// assumption that data from FFI is well constructed
let offsets = unsafe { OffsetsBuffer::new_unchecked(offsets) };
Ok(Self::new_unchecked(dtype, offsets, values, validity))
}
}