Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MutableBuffer::typed_data - shared ref access to the typed slice #2652

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion arrow/src/buffer/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Buffer {
unsafe { self.data.ptr().as_ptr().add(self.offset) }
}

/// View buffer as typed slice.
/// View buffer as a slice of a specific type.
///
/// # Panics
///
Expand Down
17 changes: 16 additions & 1 deletion arrow/src/buffer/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl MutableBuffer {
Buffer::from_bytes(bytes)
}

/// View this buffer as a slice of a specific type.
/// View this buffer as a mutable slice of a specific type.
///
/// # Panics
///
Expand All @@ -304,6 +304,21 @@ impl MutableBuffer {
offsets
}

/// View buffer as a immutable slice of a specific type.
///
/// # Panics
///
/// This function panics if the underlying buffer is not aligned
/// correctly for type `T`.
pub fn typed_data<T: ArrowNativeType>(&self) -> &[T] {
// SAFETY
// ArrowNativeType is trivially transmutable, is sealed to prevent potentially incorrect
// implementation outside this crate, and this method checks alignment
let (prefix, offsets, suffix) = unsafe { self.as_slice().align_to::<T>() };
assert!(prefix.is_empty() && suffix.is_empty());
offsets
}

/// Extends this buffer from a slice of items that can be represented in bytes, increasing its capacity if needed.
/// # Example
/// ```
Expand Down