Apache DataFusion SQL Query Engine
ARROW-2378: [Rust] Rustfmt
Maximilian Roosae38ee4f1b46
diff --git a/ci/travis_script_rust.sh b/ci/travis_script_rust.shindex 89554f6e43..4405d036d6 100755--- a/ci/travis_script_rust.sh+++ b/ci/travis_script_rust.sh@@ -23,6 +23,9 @@ RUST_DIR=${TRAVIS_BUILD_DIR}/rustpushd $RUST_DIR+rustup component add rustfmt-preview+cargo fmt --all -- --write-mode=diff+cargo buildcargo testpopddiff --git a/rust/src/array.rs b/rust/src/array.rsindex 15d89c3ab3..6f377cba03 100644--- a/rust/src/array.rs+++ b/rust/src/array.rs@@ -39,18 +39,17 @@ pub enum ArrayData {UInt32(Buffer<u32>),UInt64(Buffer<u64>),Utf8(List<u8>),- Struct(Vec<Rc<Array>>)+ Struct(Vec<Rc<Array>>),}macro_rules! arraydata_from_primitive {- ($DT:ty, $AT:ident) => {+ ($DT: ty, $AT: ident) => {impl From<Vec<$DT>> for ArrayData {fn from(v: Vec<$DT>) -> Self {ArrayData::$AT(Buffer::from(v))}}-- }+ };}arraydata_from_primitive!(bool, Boolean);@@ -69,14 +68,18 @@ pub struct Array {pub len: i32,pub null_count: i32,pub validity_bitmap: Option<Bitmap>,- pub data: ArrayData+ pub data: ArrayData,}impl Array {-/// Create a new array where there are no null valuespub fn new(len: usize, data: ArrayData) -> Self {- Array { len: len as i32, data, validity_bitmap: None, null_count: 0 }+ Array {+ len: len as i32,+ data,+ validity_bitmap: None,+ null_count: 0,+ }}pub fn data(&self) -> &ArrayData {@@ -86,7 +89,6 @@ impl Array {pub fn len(&self) -> usize {self.len as usize}-}/// type-safe array operations@@ -95,54 +97,54 @@ trait ArrayOps<T> {/// will pattern match the type of the array on every invocation. We should add/// other efficient iterator and map methods so we can perform columnar operations/// instead.- fn get(&self, i: usize) -> Result<T,Error>;+ fn get(&self, i: usize) -> Result<T, Error>;/// Compare two same-typed arrays using a boolean closure e.g. eq, gt, lt, and so on- fn compare(&self, other: &Array, f: &Fn(T,T) -> bool) -> Result<Vec<bool>, Error>;+ fn compare(&self, other: &Array, f: &Fn(T, T) -> bool) -> Result<Vec<bool>, Error>;/// Perform a computation on two same-typed arrays and produce a result of the same type e.g. c = a + b- fn compute(&self, other: &Array, f: &Fn(T,T) -> T) -> Result<Vec<T>, Error>;+ fn compute(&self, other: &Array, f: &Fn(T, T) -> T) -> Result<Vec<T>, Error>;}macro_rules! array_ops {- ($DT:ty, $AT:ident) => {+ ($DT: ty, $AT: ident) => {impl ArrayOps<$DT> for Array {- fn get(&self, i: usize) -> Result<$DT,Error> {+ fn get(&self, i: usize) -> Result<$DT, Error> {match self.data() {- &ArrayData::$AT(ref buf) => Ok(unsafe {*buf.data().offset(i as isize)}),- _ => Err(Error::from("Request for $DT but array is not $DT"))+ &ArrayData::$AT(ref buf) => Ok(unsafe { *buf.data().offset(i as isize) }),+ _ => Err(Error::from("Request for $DT but array is not $DT")),}}- fn compare(&self, other: &Array, f: &Fn($DT,$DT) -> bool) -> Result<Vec<bool>, Error> {+ fn compare(&self, other: &Array, f: &Fn($DT, $DT) -> bool) -> Result<Vec<bool>, Error> {match (&self.data, &other.data) {(&ArrayData::$AT(ref l), &ArrayData::$AT(ref r)) => {let mut b: Vec<bool> = Vec::with_capacity(self.len as usize);for i in 0..self.len as isize {- let lv : $DT = unsafe { *l.data().offset(i) };- let rv : $DT = unsafe { *r.data().offset(i) };- b.push(f(lv,rv));+ let lv: $DT = unsafe { *l.data().offset(i) };+ let rv: $DT = unsafe { *r.data().offset(i) };+ b.push(f(lv, rv));}Ok(b)- },- _ => Err(Error::from("Cannot compare arrays of this type"))+ }+ _ => Err(Error::from("Cannot compare arrays of this type")),}}- fn compute(&self, other: &Array, f: &Fn($DT,$DT) -> $DT) -> Result<Vec<$DT>, Error> {+ fn compute(&self, other: &Array, f: &Fn($DT, $DT) -> $DT) -> Result<Vec<$DT>, Error> {match (&self.data, &other.data) {(&ArrayData::$AT(ref l), &ArrayData::$AT(ref r)) => {let mut b: Vec<$DT> = Vec::with_capacity(self.len as usize);for i in 0..self.len as isize {- let lv : $DT = unsafe { *l.data().offset(i) };- let rv : $DT = unsafe { *r.data().offset(i) };- b.push(f(lv,rv));+ let lv: $DT = unsafe { *l.data().offset(i) };+ let rv: $DT = unsafe { *r.data().offset(i) };+ b.push(f(lv, rv));}Ok(b)- },- _ => Err(Error::from("Cannot compare arrays of this type"))+ }+ _ => Err(Error::from("Cannot compare arrays of this type")),}}}- }+ };}array_ops!(bool, Boolean);@@ -158,14 +160,18 @@ array_ops!(i32, Int32);array_ops!(i64, Int64);macro_rules! array_from_primitive {- ($DT:ty) => {+ ($DT: ty) => {impl From<Vec<$DT>> for Array {fn from(v: Vec<$DT>) -> Self {- Array { len: v.len() as i32, null_count: 0, validity_bitmap: None, data: ArrayData::from(v) }+ Array {+ len: v.len() as i32,+ null_count: 0,+ validity_bitmap: None,+ data: ArrayData::from(v),+ }}}- }-+ };}array_from_primitive!(bool);@@ -180,23 +186,29 @@ array_from_primitive!(i32);array_from_primitive!(i64);macro_rules! array_from_optional_primitive {- ($DT:ty, $DEFAULT:expr) => {+ ($DT: ty, $DEFAULT: expr) => {impl From<Vec<Option<$DT>>> for Array {fn from(v: Vec<Option<$DT>>) -> Self {let mut null_count = 0;let mut validity_bitmap = Bitmap::new(v.len());- for i in 0 .. v.len() {+ for i in 0..v.len() {if v[i].is_none() {- null_count+=1;+ null_count += 1;validity_bitmap.clear(i);}}- let values = v.iter().map(|x| x.unwrap_or($DEFAULT)).collect::<Vec<$DT>>();- Array { len: values.len() as i32, null_count, validity_bitmap: Some(validity_bitmap), data: ArrayData::from(values) }+ let values = v.iter()+ .map(|x| x.unwrap_or($DEFAULT))+ .collect::<Vec<$DT>>();+ Array {+ len: values.len() as i32,+ null_count,+ validity_bitmap: Some(validity_bitmap),+ data: ArrayData::from(values),+ }}}- }-+ };}array_from_optional_primitive!(bool, false);@@ -223,7 +235,7 @@ impl From<Vec<String>> for Array {len: v.len() as i32,null_count: 0,validity_bitmap: None,- data: ArrayData::Utf8(List::from(v))+ data: ArrayData::Utf8(List::from(v)),}}}@@ -234,7 +246,7 @@ impl From<Vec<Rc<Array>>> for Array {len: v.len() as i32,null_count: 0,validity_bitmap: None,- data: ArrayData::Struct(v.iter().map(|a| a.clone()).collect())+ data: ArrayData::Struct(v.iter().map(|a| a.clone()).collect()),}}}@@ -249,15 +261,18 @@ mod tests {let a = Array::from(vec!["this", "is", "a", "test"]);assert_eq!(4, a.len());match a.data() {- &ArrayData::Utf8(List{ ref data, ref offsets }) => {+ &ArrayData::Utf8(List {+ ref data,+ ref offsets,+ }) => {assert_eq!(11, data.len());assert_eq!(0, *offsets.get(0));assert_eq!(4, *offsets.get(1));assert_eq!(6, *offsets.get(2));assert_eq!(7, *offsets.get(3));assert_eq!(11, *offsets.get(4));- },- _ => panic!()+ }+ _ => panic!(),}}@@ -271,8 +286,8 @@ mod tests {assert_eq!("is", str::from_utf8(d.slice(1)).unwrap());assert_eq!("a", str::from_utf8(d.slice(2)).unwrap());assert_eq!("test", str::from_utf8(d.slice(3)).unwrap());- },- _ => panic!()+ }+ _ => panic!(),}}@@ -290,7 +305,6 @@ mod tests {#[test]fn test_from_i32() {-let a = Array::from(vec![15, 14, 13, 12, 11]);assert_eq!(5, a.len());@@ -316,58 +330,53 @@ mod tests {#[test]fn test_struct() {-let _schema = Schema::new(vec![Field::new("a", DataType::Int32, false),Field::new("b", DataType::Float32, false),]);- let a = Rc::new(Array::from(vec![1,2,3,4,5]));+ let a = Rc::new(Array::from(vec![1, 2, 3, 4, 5]));let b = Rc::new(Array::from(vec![1.1, 2.2, 3.3, 4.4, 5.5]));- let _ = Rc::new(Array::from(vec![a,b]));+ let _ = Rc::new(Array::from(vec![a, b]));}#[test]fn test_array_eq() {- let a = Array::from(vec![1,2,3,4,5]);- let b = Array::from(vec![5,4,3,2,1]);- let c = a.compare(&b, &|a: i32,b: i32| a == b).unwrap();- assert_eq!(c, vec![false,false,true,false,false]);+ let a = Array::from(vec![1, 2, 3, 4, 5]);+ let b = Array::from(vec![5, 4, 3, 2, 1]);+ let c = a.compare(&b, &|a: i32, b: i32| a == b).unwrap();+ assert_eq!(c, vec![false, false, true, false, false]);}#[test]fn test_array_lt() {- let a = Array::from(vec![1,2,3,4,5]);- let b = Array::from(vec![5,4,3,2,1]);- let c = a.compare(&b, &|a: i32,b: i32| a < b).unwrap();- assert_eq!(c, vec![true,true,false,false,false]);+ let a = Array::from(vec![1, 2, 3, 4, 5]);+ let b = Array::from(vec![5, 4, 3, 2, 1]);+ let c = a.compare(&b, &|a: i32, b: i32| a < b).unwrap();+ assert_eq!(c, vec![true, true, false, false, false]);}#[test]fn test_array_gt() {- let a = Array::from(vec![1,2,3,4,5]);- let b = Array::from(vec![5,4,3,2,1]);- let c = a.compare(&b, &|a: i32,b: i32| a > b).unwrap();- assert_eq!(c, vec![false,false,false,true,true]);+ let a = Array::from(vec![1, 2, 3, 4, 5]);+ let b = Array::from(vec![5, 4, 3, 2, 1]);+ let c = a.compare(&b, &|a: i32, b: i32| a > b).unwrap();+ assert_eq!(c, vec![false, false, false, true, true]);}#[test]fn test_array_add() {- let a = Array::from(vec![1,2,3,4,5]);- let b = Array::from(vec![5,4,3,2,1]);- let c = a.compute(&b, &|a: i32,b: i32| a + b).unwrap();- assert_eq!(c, vec![6,6,6,6,6]);+ let a = Array::from(vec![1, 2, 3, 4, 5]);+ let b = Array::from(vec![5, 4, 3, 2, 1]);+ let c = a.compute(&b, &|a: i32, b: i32| a + b).unwrap();+ assert_eq!(c, vec![6, 6, 6, 6, 6]);}#[test]fn test_array_multiply() {- let a = Array::from(vec![1,2,3,4,5]);- let b = Array::from(vec![5,4,3,2,1]);- let c = a.compute(&b, &|a: i32,b: i32| a * b).unwrap();- assert_eq!(c, vec![5,8,9,8,5]);+ let a = Array::from(vec![1, 2, 3, 4, 5]);+ let b = Array::from(vec![5, 4, 3, 2, 1]);+ let c = a.compute(&b, &|a: i32, b: i32| a * b).unwrap();+ assert_eq!(c, vec![5, 8, 9, 8, 5]);}}----diff --git a/rust/src/bitmap.rs b/rust/src/bitmap.rsindex 94c513a98b..59c651397a 100644--- a/rust/src/bitmap.rs+++ b/rust/src/bitmap.rs@@ -18,20 +18,25 @@use super::buffer::Buffer;pub struct Bitmap {- bits: Buffer<u8>+ bits: Buffer<u8>,}impl Bitmap {-pub fn new(num_bits: usize) -> Self {- let num_bytes = num_bits/8 + if num_bits%8 > 0 { 1 } else { 0 };+ let num_bytes = num_bits / 8 + if num_bits % 8 > 0 { 1 } else { 0 };let r = num_bytes % 64;- let len = if r==0 { num_bytes } else { num_bytes + 64-r };+ let len = if r == 0 {+ num_bytes+ } else {+ num_bytes + 64 - r+ };let mut v = Vec::with_capacity(len);- for _ in 0 .. len {+ for _ in 0..len {v.push(255); // 1 is not null}- Bitmap { bits: Buffer::from(v) }+ Bitmap {+ bits: Buffer::from(v),+ }}pub fn len(&self) -> i32 {@@ -45,15 +50,13 @@ impl Bitmap {pub fn set(&mut self, i: usize) {let byte_offset = i / 8;- let v : u8 = {- self.bits.get(byte_offset) | (1_u8 << ((i % 8) as u8))- };+ let v: u8 = { self.bits.get(byte_offset) | (1_u8 << ((i % 8) as u8)) };self.bits.set(byte_offset, v);}pub fn clear(&mut self, i: usize) {let byte_offset = i / 8;- let v : u8 = self.bits.get(byte_offset) ^ (1_u8 << ((i % 8) as u8));+ let v: u8 = self.bits.get(byte_offset) ^ (1_u8 << ((i % 8) as u8));self.bits.set(byte_offset, v);}}@@ -64,14 +67,14 @@ mod tests {#[test]fn test_bitmap_length() {- assert_eq!(64, Bitmap::new(63*8).len());- assert_eq!(64, Bitmap::new(64*8).len());- assert_eq!(128, Bitmap::new(65*8).len());+ assert_eq!(64, Bitmap::new(63 * 8).len());+ assert_eq!(64, Bitmap::new(64 * 8).len());+ assert_eq!(128, Bitmap::new(65 * 8).len());}#[test]fn test_set_clear_bit() {- let mut b = Bitmap::new(64*8);+ let mut b = Bitmap::new(64 * 8);assert_eq!(true, b.is_set(12));b.clear(12);assert_eq!(false, b.is_set(12));@@ -80,6 +83,3 @@ mod tests {}}---diff --git a/rust/src/buffer.rs b/rust/src/buffer.rsindex 44fc0076ec..e8c884e31a 100644--- a/rust/src/buffer.rs+++ b/rust/src/buffer.rs@@ -23,11 +23,10 @@ use super::memory::*;pub struct Buffer<T> {data: *const T,- len: i32+ len: i32,}impl<T> Buffer<T> {-pub fn new(data: *const T, len: i32) -> Self {Buffer { data, len }}@@ -41,7 +40,7 @@ impl<T> Buffer<T> {}pub fn slice(&self, start: usize, end: usize) -> &[T] {- unsafe { slice::from_raw_parts(self.data.offset(start as isize), (end-start) as usize) }+ unsafe { slice::from_raw_parts(self.data.offset(start as isize), (end - start) as usize) }}pub fn get(&self, i: usize) -> &T {@@ -91,7 +90,7 @@ impl<T> Iterator for BufferIterator<T> where T: Copy {}macro_rules! array_from_primitive {- ($DT:ty) => {+ ($DT: ty) => {impl From<Vec<$DT>> for Buffer<$DT> {fn from(v: Vec<$DT>) -> Self {// allocate aligned memory buffer@@ -102,13 +101,17 @@ macro_rules! array_from_primitive {len: len as i32,data: unsafe {let dst = mem::transmute::<*const u8, *mut libc::c_void>(buffer);- libc::memcpy(dst, mem::transmute::<*const $DT, *const libc::c_void>(v.as_ptr()), len * sz);+ libc::memcpy(+ dst,+ mem::transmute::<*const $DT, *const libc::c_void>(v.as_ptr()),+ len * sz,+ );mem::transmute::<*mut libc::c_void, *const $DT>(dst)- }+ },}}}- }+ };}array_from_primitive!(bool);@@ -123,7 +126,6 @@ array_from_primitive!(i16);array_from_primitive!(i32);array_from_primitive!(i64);-#[cfg(test)]mod tests {use super::*;diff --git a/rust/src/datatypes.rs b/rust/src/datatypes.rsindex cf104f3727..a812f32228 100644--- a/rust/src/datatypes.rs+++ b/rust/src/datatypes.rs@@ -15,7 +15,7 @@// specific language governing permissions and limitations// under the License.-#[derive(Debug,Clone)]+#[derive(Debug, Clone)]pub enum DataType {Boolean,Int8,@@ -29,23 +29,22 @@ pub enum DataType {Float32,Float64,Utf8,- Struct(Vec<Field>)+ Struct(Vec<Field>),}-#[derive(Debug,Clone)]+#[derive(Debug, Clone)]pub struct Field {pub name: String,pub data_type: DataType,- pub nullable: bool+ pub nullable: bool,}impl Field {-pub fn new(name: &str, data_type: DataType, nullable: bool) -> Self {Field {name: name.to_string(),data_type: data_type,- nullable: nullable+ nullable: nullable,}}@@ -54,32 +53,33 @@ impl Field {}}-#[derive(Debug,Clone)]+#[derive(Debug, Clone)]pub struct Schema {- pub columns: Vec<Field>+ pub columns: Vec<Field>,}impl Schema {-/// create an empty schema- pub fn empty() -> Self { Schema { columns: vec![] } }+ pub fn empty() -> Self {+ Schema { columns: vec![] }+ }- pub fn new(columns: Vec<Field>) -> Self { Schema { columns: columns } }+ pub fn new(columns: Vec<Field>) -> Self {+ Schema { columns: columns }+ }/// look up a column by name and return a reference to the column along with it's indexpub fn column(&self, name: &str) -> Option<(usize, &Field)> {- self.columns.iter()+ self.columns+ .iter().enumerate()- .find(|&(_,c)| c.name == name)+ .find(|&(_, c)| c.name == name)}pub fn to_string(&self) -> String {- let s : Vec<String> = self.columns.iter()- .map(|c| c.to_string())- .collect();+ let s: Vec<String> = self.columns.iter().map(|c| c.to_string()).collect();s.join(",")}-}#[cfg(test)]@@ -91,10 +91,14 @@ mod tests {let _person = Schema::new(vec![Field::new("first_name", DataType::Utf8, false),Field::new("last_name", DataType::Utf8, false),- Field::new("address", DataType::Struct(vec![- Field::new("street", DataType::Utf8, false),- Field::new("zip", DataType::UInt16, false),- ]), false),+ Field::new(+ "address",+ DataType::Struct(vec![+ Field::new("street", DataType::Utf8, false),+ Field::new("zip", DataType::UInt16, false),+ ]),+ false,+ ),]);}}diff --git a/rust/src/error.rs b/rust/src/error.rsindex 837978049f..d1fb742efe 100644--- a/rust/src/error.rs+++ b/rust/src/error.rs@@ -17,13 +17,15 @@use std::convert::*;-#[derive(Debug,Clone)]+#[derive(Debug, Clone)]pub struct Error {- msg: String+ msg: String,}impl From<&'static str> for Error where {fn from(msg: &'static str) -> Self {- Error { msg: String::from(msg) }+ Error {+ msg: String::from(msg),+ }}-}\ No newline at end of file+}diff --git a/rust/src/lib.rs b/rust/src/lib.rsindex b2caa61902..cd1154d160 100644--- a/rust/src/lib.rs+++ b/rust/src/lib.rs@@ -25,4 +25,3 @@ pub mod datatypes;pub mod list;pub mod error;pub mod memory;-diff --git a/rust/src/list.rs b/rust/src/list.rsindex a3a4e76fd8..aee8c763aa 100644--- a/rust/src/list.rs+++ b/rust/src/list.rs@@ -17,31 +17,30 @@use std::str;-use bytes::{BytesMut, BufMut};+use bytes::{BufMut, BytesMut};use super::buffer::Buffer;pub struct List<T> {pub data: Buffer<T>,- pub offsets: Buffer<i32>+ pub offsets: Buffer<i32>,}impl<T> List<T> {-pub fn len(&self) -> i32 {- self.offsets.len()-1+ self.offsets.len() - 1}pub fn slice(&self, index: usize) -> &[T] {let start = *self.offsets.get(index) as usize;- let end = *self.offsets.get(index+1) as usize;+ let end = *self.offsets.get(index + 1) as usize;&self.data.slice(start, end)}}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 offsets: Vec<i32> = Vec::with_capacity(v.len() + 1);let mut buf = BytesMut::with_capacity(v.len() * 32);offsets.push(0_i32);v.iter().for_each(|s| {@@ -51,7 +50,10 @@ impl From<Vec<String>> for List<u8> {let bytes = buf.freeze();let buffer = Buffer::new(bytes.as_ptr(), bytes.len() as i32);- List { data: buffer, offsets: Buffer::from(offsets) }+ List {+ data: buffer,+ offsets: Buffer::from(offsets),+ }}}@@ -62,8 +64,6 @@ impl From<Vec<&'static str>> for List<u8> {}}--#[cfg(test)]mod tests {use super::*;@@ -78,4 +78,4 @@ mod tests {assert_eq!("test", str::from_utf8(list.slice(3)).unwrap());}-}\ No newline at end of file+}