Apache DataFusion SQL Query Engine
ARROW-2440: [Rust] Implement ListBuilder<T>
Andy Grove663ca6ddf832
diff --git a/rust/src/lib.rs b/rust/src/lib.rsindex 6ab3daabbb..e0c14db875 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.rsindex a7b11a6286..fad0ed372a 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.rsnew file mode 100644index 0000000000..0122e680e1--- /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));+ }+}