-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-7813: [Rust] Remove and fix unsafe code #6395
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
Changes from all commits
62919d4
5a15589
5681258
44bf359
27b7b29
20e62c4
4df3f2b
5a69773
fb719eb
c9996be
fc0d0d4
b73debc
92e3205
46a894b
425ba07
c606440
52cc391
6990951
cfb8496
45cccc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,9 +27,7 @@ use std::fmt::{Debug, Formatter}; | |
| use std::io::{Error as IoError, ErrorKind, Result as IoResult, Write}; | ||
| use std::mem; | ||
| use std::ops::{BitAnd, BitOr, Not}; | ||
| use std::slice::from_raw_parts; | ||
| #[cfg(feature = "simd")] | ||
| use std::slice::from_raw_parts_mut; | ||
| use std::slice::{from_raw_parts, from_raw_parts_mut}; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::array::{BufferBuilderTrait, UInt8BufferBuilder}; | ||
|
|
@@ -68,14 +66,11 @@ struct BufferData { | |
|
|
||
| impl PartialEq for BufferData { | ||
| fn eq(&self, other: &BufferData) -> bool { | ||
| if self.len != other.len { | ||
| return false; | ||
| } | ||
| if self.capacity != other.capacity { | ||
| return false; | ||
| } | ||
|
|
||
| unsafe { memory::memcmp(self.ptr, other.ptr, self.len) == 0 } | ||
| self.data() == other.data() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -96,16 +91,22 @@ impl Debug for BufferData { | |
| self.ptr, self.len, self.capacity | ||
| )?; | ||
|
|
||
| unsafe { | ||
| f.debug_list() | ||
| .entries(std::slice::from_raw_parts(self.ptr, self.len).iter()) | ||
| .finish()?; | ||
| } | ||
| f.debug_list().entries(self.data().iter()).finish()?; | ||
|
|
||
| write!(f, " }}") | ||
| } | ||
| } | ||
|
|
||
| impl BufferData { | ||
| fn data(&self) -> &[u8] { | ||
| if self.ptr.is_null() { | ||
| &[] | ||
| } else { | ||
| unsafe { std::slice::from_raw_parts(self.ptr, self.len) } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Buffer { | ||
| /// Creates a buffer from an existing memory region (must already be byte-aligned), this | ||
| /// `Buffer` will free this piece of memory when dropped. | ||
|
|
@@ -194,13 +195,13 @@ impl Buffer { | |
|
|
||
| /// Returns the byte slice stored in this buffer | ||
| pub fn data(&self) -> &[u8] { | ||
| unsafe { std::slice::from_raw_parts(self.raw_data(), self.len()) } | ||
| &self.data.data()[self.offset..] | ||
| } | ||
|
|
||
| /// Returns a slice of this buffer, starting from `offset`. | ||
| pub fn slice(&self, offset: usize) -> Self { | ||
| assert!( | ||
| self.offset + offset <= self.len(), | ||
| offset <= self.len(), | ||
| "the offset of the new Buffer cannot exceed the existing length" | ||
| ); | ||
| Self { | ||
|
|
@@ -511,12 +512,20 @@ impl MutableBuffer { | |
|
|
||
| /// Returns the data stored in this buffer as a slice. | ||
| pub fn data(&self) -> &[u8] { | ||
| unsafe { std::slice::from_raw_parts(self.raw_data(), self.len()) } | ||
| if self.data.is_null() { | ||
| &[] | ||
| } else { | ||
| unsafe { std::slice::from_raw_parts(self.raw_data(), self.len()) } | ||
| } | ||
| } | ||
|
|
||
| /// Returns the data stored in this buffer as a mutable slice. | ||
| pub fn data_mut(&mut self) -> &mut [u8] { | ||
| unsafe { std::slice::from_raw_parts_mut(self.raw_data() as *mut u8, self.len()) } | ||
| if self.data.is_null() { | ||
| &mut [] | ||
| } else { | ||
| unsafe { std::slice::from_raw_parts_mut(self.raw_data_mut(), self.len()) } | ||
| } | ||
| } | ||
|
|
||
| /// Returns a raw pointer for this buffer. | ||
|
||
|
|
@@ -527,6 +536,10 @@ impl MutableBuffer { | |
| self.data | ||
| } | ||
|
|
||
| pub fn raw_data_mut(&mut self) -> *mut u8 { | ||
| self.data | ||
| } | ||
|
|
||
| /// Freezes this buffer and return an immutable version of it. | ||
| pub fn freeze(self) -> Buffer { | ||
| let buffer_data = BufferData { | ||
|
|
@@ -541,6 +554,18 @@ impl MutableBuffer { | |
| offset: 0, | ||
| } | ||
| } | ||
|
|
||
| /// View buffer as typed slice. | ||
| pub fn typed_data_mut<T: ArrowNativeType + num::Num>(&mut self) -> &mut [T] { | ||
| assert_eq!(self.len() % mem::size_of::<T>(), 0); | ||
| assert!(memory::is_ptr_aligned::<T>(self.raw_data() as *const T)); | ||
| unsafe { | ||
| from_raw_parts_mut( | ||
| self.raw_data() as *mut T, | ||
|
||
| self.len() / mem::size_of::<T>(), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Drop for MutableBuffer { | ||
|
|
@@ -665,6 +690,7 @@ mod tests { | |
| assert_eq!(empty_slice, buf4.data()); | ||
| assert_eq!(0, buf4.len()); | ||
| assert!(buf4.is_empty()); | ||
| assert_eq!(buf2.slice(2).data(), &[10]); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.