tustvold commented on code in PR #3920:
URL: https://github.com/apache/arrow-rs/pull/3920#discussion_r1148349944


##########
arrow-buffer/src/buffer/mutable.rs:
##########
@@ -89,35 +97,46 @@ impl MutableBuffer {
     /// let data = buffer.as_slice_mut();
     /// assert_eq!(data[126], 0u8);
     /// ```
-    #[allow(deprecated)]
     pub fn from_len_zeroed(len: usize) -> Self {
-        let new_capacity = bit_util::round_upto_multiple_of_64(len);
-        let ptr = alloc::allocate_aligned_zeroed(new_capacity);
-        Self {
-            data: ptr,
-            len,
-            capacity: new_capacity,
-        }
+        let layout = Layout::from_size_align(len, ALIGNMENT).unwrap();
+        let data = match layout.size() {
+            0 => dangling_ptr(),
+            _ => {
+                // Safety: Verified size != 0
+                let raw_ptr = unsafe { std::alloc::alloc_zeroed(layout) };
+                NonNull::new(raw_ptr).unwrap_or_else(|| 
handle_alloc_error(layout))
+            }
+        };
+        Self { data, len, layout }
+    }
+
+    /// Create a [`MutableBuffer`] from the provided [`Vec`] without copying
+    #[inline]
+    pub fn from_vec<T: ArrowNativeType>(vec: Vec<T>) -> Self {
+        // Safety
+        // Vec::as_ptr guaranteed to not be null and ArrowNativeType are 
trivially transmutable
+        let data = unsafe { NonNull::new_unchecked(vec.as_ptr() as _) };
+        let len = vec.len() * mem::size_of::<T>();
+        // Safety
+        // Vec guaranteed to have a valid layout matching that of 
`Layout::array`
+        // This is based on `RawVec::current_memory`
+        let layout = unsafe { 
Layout::array::<T>(vec.capacity()).unwrap_unchecked() };
+        mem::forget(vec);
+        Self { data, len, layout }
     }
 
     /// Allocates a new [MutableBuffer] from given `Bytes`.
     pub(crate) fn from_bytes(bytes: Bytes) -> Result<Self, Bytes> {
-        let capacity = match bytes.deallocation() {
-            Deallocation::Standard(layout) if layout.align() == ALIGNMENT => {
-                layout.size()
-            }
+        let layout = match bytes.deallocation() {

Review Comment:
   Correct, we can handle any alignment now



-- 
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