Dandandan commented on a change in pull request #9076:
URL: https://github.com/apache/arrow/pull/9076#discussion_r550909002
##########
File path: rust/arrow/src/buffer.rs
##########
@@ -852,17 +870,34 @@ impl MutableBuffer {
}
}
- /// Extends the buffer from a byte slice, incrementing its capacity if
needed.
+ /// Extends the buffer from a slice, increasing its capacity if needed.
+ #[inline]
+ pub fn extend_from_slice<T: ToByteSlice>(&mut self, items: &[T]) {
+ let additional = items.len() * std::mem::size_of::<T>();
+ let new_len = self.len + additional;
+ if new_len > self.capacity {
+ self.reserve(additional);
+ }
+ unsafe {
+ let dst = self.data.as_ptr().add(self.len) as *mut T;
+ let src = items.as_ptr() as *const T;
+ std::ptr::copy_nonoverlapping(src, dst, items.len())
+ }
+ self.len = new_len;
+ }
+
+ /// Extends the buffer with a new item, increasing its capacity if needed.
#[inline]
- pub fn extend_from_slice(&mut self, bytes: &[u8]) {
- let additional = bytes.len();
- if self.len + additional > self.capacity() {
+ pub fn push<T: ToByteSlice>(&mut self, item: &T) {
+ let additional = std::mem::size_of::<T>();
+ let new_len = self.len + additional;
+ if new_len > self.capacity {
Review comment:
I am wondering if we in a followup can make `len` / `capacity` similar
as `Vec` to be in number of items instead of number of bytes?
This makes some implementations easier, e.g. looking at Vec
https://doc.rust-lang.org/src/alloc/vec.rs.html#1206
##########
File path: rust/arrow/src/buffer.rs
##########
@@ -852,17 +870,34 @@ impl MutableBuffer {
}
}
- /// Extends the buffer from a byte slice, incrementing its capacity if
needed.
+ /// Extends the buffer from a slice, increasing its capacity if needed.
+ #[inline]
+ pub fn extend_from_slice<T: ToByteSlice>(&mut self, items: &[T]) {
+ let additional = items.len() * std::mem::size_of::<T>();
+ let new_len = self.len + additional;
+ if new_len > self.capacity {
+ self.reserve(additional);
+ }
+ unsafe {
+ let dst = self.data.as_ptr().add(self.len) as *mut T;
+ let src = items.as_ptr() as *const T;
+ std::ptr::copy_nonoverlapping(src, dst, items.len())
+ }
+ self.len = new_len;
+ }
+
+ /// Extends the buffer with a new item, increasing its capacity if needed.
#[inline]
- pub fn extend_from_slice(&mut self, bytes: &[u8]) {
- let additional = bytes.len();
- if self.len + additional > self.capacity() {
+ pub fn push<T: ToByteSlice>(&mut self, item: &T) {
Review comment:
Could this be `T` too?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]