[ 
https://issues.apache.org/jira/browse/ARROW-2440?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16432848#comment-16432848
 ] 

ASF GitHub Bot commented on ARROW-2440:
---------------------------------------

pitrou closed pull request #1876: ARROW-2440: [Rust] Implement ListBuilder<T>
URL: https://github.com/apache/arrow/pull/1876
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 6ab3daabb..e0c14db87 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -28,4 +28,5 @@ pub mod builder;
 pub mod datatypes;
 pub mod error;
 pub mod list;
+pub mod list_builder;
 pub mod memory;
diff --git a/rust/src/list.rs b/rust/src/list.rs
index a7b11a628..fad0ed372 100644
--- a/rust/src/list.rs
+++ b/rust/src/list.rs
@@ -17,9 +17,8 @@
 
 use std::str;
 
-use bytes::{BufMut, BytesMut};
-
 use super::buffer::Buffer;
+use super::list_builder::ListBuilder;
 
 pub struct List<T> {
     pub data: Buffer<T>,
@@ -40,19 +39,11 @@ impl<T> List<T> {
 
 impl From<Vec<String>> for List<u8> {
     fn from(v: Vec<String>) -> Self {
-        let mut offsets: Vec<i32> = Vec::with_capacity(v.len() + 1);
-        let mut buf = BytesMut::with_capacity(v.len() * 32);
-        offsets.push(0_i32);
+        let mut b: ListBuilder<u8> = ListBuilder::with_capacity(v.len());
         v.iter().for_each(|s| {
-            let slice = s.as_bytes();
-            buf.reserve(slice.len());
-            buf.put(slice);
-            offsets.push(buf.len() as i32);
+            b.push(s.as_bytes());
         });
-        List {
-            data: Buffer::from(buf.freeze()),
-            offsets: Buffer::from(offsets),
-        }
+        b.finish()
     }
 }
 
diff --git a/rust/src/list_builder.rs b/rust/src/list_builder.rs
new file mode 100644
index 000000000..0122e680e
--- /dev/null
+++ b/rust/src/list_builder.rs
@@ -0,0 +1,66 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::builder::*;
+use super::list::List;
+
+pub struct ListBuilder<T> {
+    data: Builder<T>,
+    offsets: Builder<i32>,
+}
+
+impl<T> ListBuilder<T> {
+    pub fn new() -> Self {
+        ListBuilder::with_capacity(64)
+    }
+
+    pub fn with_capacity(n: usize) -> Self {
+        let data = Builder::with_capacity(n);
+        let mut offsets = Builder::with_capacity(n);
+        offsets.push(0_i32);
+        ListBuilder { data, offsets }
+    }
+
+    pub fn push(&mut self, slice: &[T]) {
+        self.data.push_slice(slice);
+        self.offsets.push(self.data.len() as i32);
+    }
+
+    pub fn finish(&mut self) -> List<T> {
+        List {
+            data: self.data.finish(),
+            offsets: self.offsets.finish(),
+        }
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_list_u8() {
+        let mut b: ListBuilder<u8> = ListBuilder::new();
+        b.push("Hello, ".as_bytes());
+        b.push("World!".as_bytes());
+        let buffer = b.finish();
+
+        assert_eq!(2, buffer.len());
+        assert_eq!("Hello, ".as_bytes(), buffer.slice(0));
+        assert_eq!("World!".as_bytes(), buffer.slice(1));
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [Rust] Implement ListBuilder<T>
> -------------------------------
>
>                 Key: ARROW-2440
>                 URL: https://issues.apache.org/jira/browse/ARROW-2440
>             Project: Apache Arrow
>          Issue Type: New Feature
>          Components: Rust
>            Reporter: Andy Grove
>            Assignee: Andy Grove
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.10.0
>
>
> Implement ListBuilder<T>



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to