Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ pub use crate::types::ArrowPrimitiveType;
///
/// # Example: From a Vec
///
/// *Note*: Converting a `Vec` to a `PrimitiveArray` does not copy the data.
/// The new `PrimitiveArray` uses the same underlying allocation from the `Vec`.
///
/// ```
/// # use arrow_array::{Array, PrimitiveArray, types::Int32Type};
/// let arr: PrimitiveArray<Int32Type> = vec![1, 2, 3, 4].into();
Expand All @@ -501,6 +504,33 @@ pub use crate::types::ArrowPrimitiveType;
/// assert_eq!(arr.values(), &[1, 2, 3, 4])
/// ```
///
/// # Example: To a `Vec<T>`
///
/// *Note*: In some cases, converting `PrimitiveArray` to a `Vec` is zero-copy
/// and does not copy the data (see [`Buffer::into_vec`] for conditions). In
/// such cases, the `Vec` will use the same underlying memory allocation from
/// the `PrimitiveArray`.
///
/// The Rust compiler generates highly optimized code for operations on
/// Vec, so using a Vec can often be faster than using a PrimitiveArray directly.
///
/// ```
/// # use arrow_array::{Array, PrimitiveArray, types::Int32Type};
/// let arr = PrimitiveArray::<Int32Type>::from(vec![1, 2, 3, 4]);
/// let starting_ptr = arr.values().as_ptr();
/// // split into its parts
/// let (datatype, buffer, nulls) = arr.into_parts();
/// // Convert the buffer to a Vec<i32> (zero copy)
/// // (not this requires that there are no other references)
/// let mut vec: Vec<i32> = buffer.into();
/// vec[2] = 300;
/// // put the parts back together
/// let arr = PrimitiveArray::<Int32Type>::try_new(vec.into(), nulls).unwrap();
/// assert_eq!(arr.values(), &[1, 2, 300, 4]);
/// // The same allocation was used
/// assert_eq!(starting_ptr, arr.values().as_ptr());
/// ```
///
/// # Example: From an optional Vec
///
/// ```
Expand Down
4 changes: 2 additions & 2 deletions arrow-buffer/src/buffer/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ impl Buffer {
/// # Errors
///
/// Returns `Err(self)` if
/// 1. this buffer does not have the same [`Layout`] as the destination Vec
/// 2. contains a non-zero offset
/// 1. The buffer does not have the same [`Layout`] as the destination Vec
/// 2. The buffer contains a non-zero offset
/// 3. The buffer is shared
pub fn into_vec<T: ArrowNativeType>(self) -> Result<Vec<T>, Self> {
let layout = match self.data.deallocation() {
Expand Down
Loading