Path: blob/main/crates/polars-arrow/src/array/binary/ffi.rs
6939 views
use polars_error::PolarsResult;12use super::BinaryArray;3use crate::array::{FromFfi, ToFfi};4use crate::bitmap::align;5use crate::ffi;6use crate::offset::{Offset, OffsetsBuffer};78unsafe impl<O: Offset> ToFfi for BinaryArray<O> {9fn buffers(&self) -> Vec<Option<*const u8>> {10vec![11self.validity.as_ref().map(|x| x.as_ptr()),12Some(self.offsets.buffer().storage_ptr().cast::<u8>()),13Some(self.values.storage_ptr().cast::<u8>()),14]15}1617fn offset(&self) -> Option<usize> {18let offset = self.offsets.buffer().offset();19if let Some(bitmap) = self.validity.as_ref() {20if bitmap.offset() == offset {21Some(offset)22} else {23None24}25} else {26Some(offset)27}28}2930fn to_ffi_aligned(&self) -> Self {31let offset = self.offsets.buffer().offset();3233let validity = self.validity.as_ref().map(|bitmap| {34if bitmap.offset() == offset {35bitmap.clone()36} else {37align(bitmap, offset)38}39});4041Self {42dtype: self.dtype.clone(),43validity,44offsets: self.offsets.clone(),45values: self.values.clone(),46}47}48}4950impl<O: Offset, A: ffi::ArrowArrayRef> FromFfi<A> for BinaryArray<O> {51unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {52let dtype = array.dtype().clone();5354let validity = unsafe { array.validity() }?;55let offsets = unsafe { array.buffer::<O>(1) }?;56let values = unsafe { array.buffer::<u8>(2) }?;5758// assumption that data from FFI is well constructed59let offsets = unsafe { OffsetsBuffer::new_unchecked(offsets) };6061Self::try_new(dtype, offsets, values, validity)62}63}646566