diff --git a/vortex-buffer/src/io_buf.rs b/vortex-buffer/src/io_buf.rs index 37c2dae8f8..53ebc1bd28 100644 --- a/vortex-buffer/src/io_buf.rs +++ b/vortex-buffer/src/io_buf.rs @@ -1,6 +1,11 @@ +//! Provides types that can be used by I/O frameworks to work with byte buffer-shaped data. + use crate::Buffer; -#[allow(clippy::missing_safety_doc)] +/// Trait for types that can provide a readonly byte buffer interface to I/O frameworks. +/// +/// # Safety +/// The type must support contiguous raw memory access via pointer, such as `Vec` or `[u8]`. pub unsafe trait IoBuf: Unpin + 'static { /// Returns a raw pointer to the vector’s buffer. fn read_ptr(&self) -> *const u8; @@ -8,10 +13,12 @@ pub unsafe trait IoBuf: Unpin + 'static { /// Number of initialized bytes. fn bytes_init(&self) -> usize; + /// Access the buffer as a byte slice fn as_slice(&self) -> &[u8] { unsafe { std::slice::from_raw_parts(self.read_ptr(), self.bytes_init()) } } + /// Access the buffer as a byte slice with begin and end indices #[inline] fn slice(self, begin: usize, end: usize) -> Slice where @@ -33,6 +40,7 @@ pub struct Slice { } impl Slice { + /// Unwrap the slice into its underlying type. pub fn into_inner(self) -> T { self.buf } diff --git a/vortex-buffer/src/lib.rs b/vortex-buffer/src/lib.rs index 1d7122f3db..abb74ae2cb 100644 --- a/vortex-buffer/src/lib.rs +++ b/vortex-buffer/src/lib.rs @@ -1,3 +1,15 @@ +#![deny(missing_docs)] + +//! A byte buffer implementation for Vortex. +//! +//! Vortex arrays hold data in a set of buffers. +//! +//! # Alignment +//! +//! We do not currently enforce any alignment guarantees on the buffer. This can be problematic +//! so it is recommended to convert all buffers into Arrow before usage. + + use std::cmp::Ordering; use std::ops::{Deref, Range}; @@ -8,10 +20,16 @@ mod flexbuffers; pub mod io_buf; mod string; +/// Buffer is an owned, cheaply cloneable byte arrays. +/// +/// Buffers form the building blocks of all in-memory storage in Vortex. #[derive(Debug, Clone)] pub enum Buffer { // TODO(ngates): we could add Aligned(Arc) from aligned-vec package + /// A Buffer that wraps an Apache Arrow buffer Arrow(ArrowBuffer), + + /// A Buffer that wraps an owned [`Bytes`]. Bytes(bytes::Bytes), } @@ -19,6 +37,7 @@ unsafe impl Send for Buffer {} unsafe impl Sync for Buffer {} impl Buffer { + /// Length of the buffer in bytes pub fn len(&self) -> usize { match self { Self::Arrow(b) => b.len(), @@ -26,6 +45,7 @@ impl Buffer { } } + /// Predicate for empty buffers pub fn is_empty(&self) -> bool { match self { Self::Arrow(b) => b.is_empty(), @@ -33,6 +53,7 @@ impl Buffer { } } + /// Return a new view on the buffer, but limited to the given index range. pub fn slice(&self, range: Range) -> Self { match self { Self::Arrow(b) => { @@ -42,6 +63,7 @@ impl Buffer { } } + /// Access the buffer as an immutable byte slice. pub fn as_slice(&self) -> &[u8] { match self { Self::Arrow(b) => b.as_ref(), @@ -49,6 +71,13 @@ impl Buffer { } } + /// Convert the buffer into a `Vec` of the given native type `T`. + /// + /// # Ownership + /// The caller takes ownership of the underlying memory. + /// + /// # Errors + /// This method will fail if the underlying buffer is an owned [`bytes::Bytes`]. pub fn into_vec(self) -> Result, Self> { match self { Self::Arrow(buffer) => buffer.into_vec::().map_err(Buffer::Arrow), diff --git a/vortex-buffer/src/string.rs b/vortex-buffer/src/string.rs index c2dfe0de13..95c0592f6d 100644 --- a/vortex-buffer/src/string.rs +++ b/vortex-buffer/src/string.rs @@ -16,6 +16,7 @@ impl BufferString { Self(buffer) } + /// Return a view of the contents of BufferString as an immutable `&str`. pub fn as_str(&self) -> &str { // SAFETY: We have already validated that the buffer is valid UTF-8 unsafe { std::str::from_utf8_unchecked(self.0.as_ref()) }