Skip to content
Merged
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion arrow-buffer/src/buffer/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,35 @@ use std::ops::Deref;
/// with the following differences:
///
/// - slicing and cloning is O(1).
/// - it supports external allocated memory
/// - support for external allocated memory (e.g. via FFI).
///
/// See [`Buffer`] for more low-level memory management details.
///
/// # Example: Convert to/from Vec (without copies)
/// ```
/// # use arrow_buffer::ScalarBuffer;
/// // Zero-copy conversion from Vec
/// let buffer = ScalarBuffer::from(vec![1, 2, 3]);
/// assert_eq!(&buffer, &[1, 2, 3]);
/// // convert the buffer back to Vec without copy assuming:
/// // 1. the inner buffer is not sliced
/// // 2. the inner buffer uses standard allocation
Comment on lines +44 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there also a requirement for the reference count of the buffer to be 1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @mbrobbel 's comment applies here too #8771 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent call -- added in dac99aa. Thank you 🙏

/// let vec: Vec<i32> = buffer.into();
/// assert_eq!(&vec, &[1, 2, 3]);
/// ```
///
/// # Example: Zero copy slicing
/// ```
/// # use arrow_buffer::ScalarBuffer;
/// let buffer = ScalarBuffer::from(vec![1, 2, 3]);
/// assert_eq!(&buffer, &[1, 2, 3]);
/// // Zero-copy slicing
/// let sliced = buffer.slice(1, 2);
/// assert_eq!(&sliced, &[2, 3]);
/// // Original buffer is unchanged
/// assert_eq!(&buffer, &[1, 2, 3]);
/// // converting the sliced buffer back to Vec incurs a copy
/// let vec: Vec<i32> = sliced.into();
/// ```
#[derive(Clone, Default)]
pub struct ScalarBuffer<T: ArrowNativeType> {
Expand Down
Loading