Path: blob/main/crates/polars-arrow/src/array/primitive/ffi.rs
6939 views
use polars_error::PolarsResult;12use super::PrimitiveArray;3use crate::array::{FromFfi, ToFfi};4use crate::bitmap::align;5use crate::ffi;6use crate::types::NativeType;78unsafe impl<T: NativeType> ToFfi for PrimitiveArray<T> {9fn buffers(&self) -> Vec<Option<*const u8>> {10vec![11self.validity.as_ref().map(|x| x.as_ptr()),12Some(self.values.storage_ptr().cast::<u8>()),13]14}1516fn offset(&self) -> Option<usize> {17let offset = self.values.offset();18if let Some(bitmap) = self.validity.as_ref() {19if bitmap.offset() == offset {20Some(offset)21} else {22None23}24} else {25Some(offset)26}27}2829fn to_ffi_aligned(&self) -> Self {30let offset = self.values.offset();3132let validity = self.validity.as_ref().map(|bitmap| {33if bitmap.offset() == offset {34bitmap.clone()35} else {36align(bitmap, offset)37}38});3940Self {41dtype: self.dtype.clone(),42validity,43values: self.values.clone(),44}45}46}4748impl<T: NativeType, A: ffi::ArrowArrayRef> FromFfi<A> for PrimitiveArray<T> {49unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {50let dtype = array.dtype().clone();51let validity = unsafe { array.validity() }?;52let values = unsafe { array.buffer::<T>(1) }?;5354Self::try_new(dtype, values, validity)55}56}575859