This is an automated email from the ASF dual-hosted git repository.
tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new 43d847442 MutableBuffer::typed_data - shared ref access to the typed
slice (#2652)
43d847442 is described below
commit 43d8474429227a267141ea49d9e2ee5df1bd9239
Author: Michael Edwards <[email protected]>
AuthorDate: Mon Sep 5 17:46:56 2022 +0200
MutableBuffer::typed_data - shared ref access to the typed slice (#2652)
---
arrow/src/buffer/immutable.rs | 2 +-
arrow/src/buffer/mutable.rs | 17 ++++++++++++++++-
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/arrow/src/buffer/immutable.rs b/arrow/src/buffer/immutable.rs
index 28042a381..6d4d624ef 100644
--- a/arrow/src/buffer/immutable.rs
+++ b/arrow/src/buffer/immutable.rs
@@ -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
///
diff --git a/arrow/src/buffer/mutable.rs b/arrow/src/buffer/mutable.rs
index 96c837922..d1e633993 100644
--- a/arrow/src/buffer/mutable.rs
+++ b/arrow/src/buffer/mutable.rs
@@ -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
///
@@ -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
/// ```