Skip to content

Commit f9dd799

Browse files
authored
Update docs for zero copy conversion of ScalarBuffer (#8772)
# Which issue does this PR close? - Similar to #8771 # Rationale for this change As people write more arrow kernels, let's make it easier to understand how to go back/forth to Vec without copying # What changes are included in this PR? Add some additional information and examples about converting ScalarBuffer back/forth to Vec # Are these changes tested? Yes by CI # Are there any user-facing changes? Docs only. No functional change
1 parent e2ebb23 commit f9dd799

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

arrow-buffer/src/buffer/scalar.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,38 @@ use std::ops::Deref;
2929
/// with the following differences:
3030
///
3131
/// - slicing and cloning is O(1).
32-
/// - it supports external allocated memory
32+
/// - support for external allocated memory (e.g. via FFI).
3333
///
34+
/// See [`Buffer`] for more low-level memory management details.
35+
///
36+
/// # Example: Convert to/from Vec (without copies)
37+
///
38+
/// (See [`Buffer::from_vec`] and [`Buffer::into_vec`] for a lower level API)
3439
/// ```
3540
/// # use arrow_buffer::ScalarBuffer;
3641
/// // Zero-copy conversion from Vec
3742
/// let buffer = ScalarBuffer::from(vec![1, 2, 3]);
3843
/// assert_eq!(&buffer, &[1, 2, 3]);
44+
/// // convert the buffer back to Vec without copy assuming:
45+
/// // 1. the inner buffer is not sliced
46+
/// // 2. the inner buffer uses standard allocation
47+
/// // 3. there are no other references to the inner buffer
48+
/// let vec: Vec<i32> = buffer.into();
49+
/// assert_eq!(&vec, &[1, 2, 3]);
50+
/// ```
3951
///
52+
/// # Example: Zero copy slicing
53+
/// ```
54+
/// # use arrow_buffer::ScalarBuffer;
55+
/// let buffer = ScalarBuffer::from(vec![1, 2, 3]);
56+
/// assert_eq!(&buffer, &[1, 2, 3]);
4057
/// // Zero-copy slicing
4158
/// let sliced = buffer.slice(1, 2);
4259
/// assert_eq!(&sliced, &[2, 3]);
60+
/// // Original buffer is unchanged
61+
/// assert_eq!(&buffer, &[1, 2, 3]);
62+
/// // converting the sliced buffer back to Vec incurs a copy
63+
/// let vec: Vec<i32> = sliced.into();
4364
/// ```
4465
#[derive(Clone, Default)]
4566
pub struct ScalarBuffer<T: ArrowNativeType> {

0 commit comments

Comments
 (0)