pchintar commented on code in PR #9971:
URL: https://github.com/apache/arrow-rs/pull/9971#discussion_r3248909051


##########
arrow-buffer/src/buffer/immutable.rs:
##########
@@ -84,6 +86,89 @@ pub struct Buffer {
     length: usize,
 }
 
+/// An aligned byte buffer that can be filled through `Read::read_exact` and
+/// converted into [`Buffer`] without copying.
+///
+/// This is useful for readers that need Arrow buffer alignment without
+/// first zero-initializing the allocation.
+pub struct AlignedVec {
+    ptr: NonNull<u8>,
+    len: usize,
+    layout: Layout,
+}
+
+impl AlignedVec {
+    /// Allocates `len` bytes with the requested alignment.
+    pub fn new(len: usize, align: usize) -> Self {
+        let layout =
+            Layout::from_size_align(len, align).expect("failed to create 
layout for AlignedVec");
+
+        let ptr = match layout.size() {
+            0 => dangling_ptr(),
+            _ => {
+                // Safety: `layout` has non-zero size and was constructed 
above.
+                let raw_ptr = unsafe { std::alloc::alloc(layout) };
+                NonNull::new(raw_ptr).unwrap_or_else(|| 
handle_alloc_error(layout))
+            }
+        };
+
+        Self { ptr, len, layout }
+    }
+}
+
+// Allows callers such as `Read::read_exact` to view the allocated region as
+// bytes after it has been filled.
+impl Deref for AlignedVec {
+    type Target = [u8];
+
+    fn deref(&self) -> &[u8] {
+        // Safety: `ptr` points to `len` bytes owned by this AlignedVec.
+        unsafe { std::slice::from_raw_parts(self.ptr.as_ptr(), self.len) }

Review Comment:
   I agree, this still exposes the allocation as initialized `[u8]` through 
`Deref`/`DerefMut` before the reader has actually filled it, so it has the same 
issue as the earlier `Vec::with_capacity` + `set_len` approach.
   
   I also explored the aligned-allocation direction separately by allocating 
through an aligned backing type and then converting back to `Vec<u8>`, but that 
ran into layout/deallocation issues under Miri because the allocation alignment 
was no longer preserved through the `Vec<u8>` ownership path.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to