Path: blob/main/crates/polars-parquet/src/arrow/read/deserialize/utils/array_chunks.rs
8431 views
use arrow::types::AlignedBytes;12/// A slice of chunks that fit an [`AlignedBytes`] type.3///4/// This is essentially the equivalent of [`ChunksExact`][std::slice::ChunksExact], but with a size5/// and type known at compile-time. This makes the compiler able to reason much more about the6/// code. Especially, since the chunk-sizes for this type are almost always powers of 2 and7/// bitshifts or special instructions would be much better to use.8#[derive(Debug, Clone, Copy)]9pub(crate) struct ArrayChunks<'a, B: AlignedBytes> {10pub(crate) bytes: &'a [B::Unaligned],11}1213impl<'a, B: AlignedBytes> ArrayChunks<'a, B> {14/// Create a new [`ArrayChunks`]15///16/// This returns null if the `bytes` slice's length is not a multiple of the size of `P::Bytes`.17pub(crate) fn new(bytes: &'a [u8]) -> Option<Self> {18if !bytes.len().is_multiple_of(B::SIZE) {19return None;20}2122let bytes = bytemuck::cast_slice(bytes);2324Some(Self { bytes })25}2627pub(crate) unsafe fn get_unchecked(&self, at: usize) -> B {28B::from_unaligned(*unsafe { self.bytes.get_unchecked(at) })29}3031pub fn truncate(&self, length: usize) -> ArrayChunks<'a, B> {32let length = length.min(self.bytes.len());3334Self {35bytes: unsafe { self.bytes.get_unchecked(..length) },36}37}3839pub fn slice(&self, start: usize, length: usize) -> ArrayChunks<'a, B> {40assert!(start <= self.bytes.len());41assert!(start + length <= self.bytes.len());42unsafe { self.slice_unchecked(start, length) }43}4445pub unsafe fn slice_unchecked(&self, start: usize, length: usize) -> ArrayChunks<'a, B> {46debug_assert!(start <= self.bytes.len());47debug_assert!(start + length <= self.bytes.len());48Self {49bytes: unsafe { self.bytes.get_unchecked(start..start + length) },50}51}5253pub fn as_ptr(&self) -> *const B::Unaligned {54self.bytes.as_ptr()55}5657pub fn is_empty(&self) -> bool {58self.len() == 059}60}6162impl<'a, B: AlignedBytes> Iterator for ArrayChunks<'a, B> {63type Item = &'a B::Unaligned;6465#[inline(always)]66fn next(&mut self) -> Option<Self::Item> {67let item = self.bytes.first()?;68self.bytes = &self.bytes[1..];69Some(item)70}7172#[inline(always)]73fn size_hint(&self) -> (usize, Option<usize>) {74(self.bytes.len(), Some(self.bytes.len()))75}76}7778impl<B: AlignedBytes> ExactSizeIterator for ArrayChunks<'_, B> {}798081