Skip to content

Commit 94e8196

Browse files
paddyhoranandygrove
authored andcommitted
ARROW-3088: [Rust] Use internal Result<T> type instead of Result<T, ArrowError>
Updates internal code to use existing arrow `Result` type. Also, includes the following minor updates that do not deserve their own PR: - Removed `println!` statements that had been commented out but left in the code - Updated to use short-hand syntax where using identifiers that match the argument names when creating objects (i.e. `{field}` vs `{field: field}`) - Updated some `assert!` macros to more specific assert versions (`assert_eq!` and `assert_ne!`) to improve debug output Author: Paddy Horan <paddyhoran@hotmail.com> Closes apache#2448 from paddyhoran/ARROW-3088 and squashes the following commits: 0d923bb <Paddy Horan> Fixed lint issues. 21d8974 <Paddy Horan> Provide better debug info with more specific asserts 23f033a <Paddy Horan> Removed commented out `println!` calls 2caeb65 <Paddy Horan> Uses less verbose syntax for object creation 6d171f9 <Paddy Horan> Replaces standard `Result` with internal arrow `Result`
1 parent a43e670 commit 94e8196

6 files changed

Lines changed: 35 additions & 40 deletions

File tree

rust/src/array.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ struct RawPtrBox<T> {
106106

107107
impl<T> RawPtrBox<T> {
108108
fn new(inner: *const T) -> Self {
109-
Self { inner: inner }
109+
Self { inner }
110110
}
111111

112112
fn get(&self) -> *const T {
@@ -240,11 +240,11 @@ macro_rules! def_primitive_array {
240240
/// Constructs a `PrimitiveArray` from an array data reference.
241241
impl<T: ArrowPrimitiveType> From<ArrayDataRef> for PrimitiveArray<T> {
242242
fn from(data: ArrayDataRef) -> Self {
243-
assert!(data.buffers().len() == 1);
243+
assert_eq!(data.buffers().len(), 1);
244244
let raw_values = data.buffers()[0].raw_data();
245245
assert!(memory::is_aligned::<u8>(raw_values, mem::align_of::<T>()));
246246
Self {
247-
data: data,
247+
data,
248248
raw_values: RawPtrBox::new(raw_values as *const T),
249249
}
250250
}
@@ -321,8 +321,8 @@ impl ListArray {
321321
/// Constructs a `ListArray` from an array data reference.
322322
impl From<ArrayDataRef> for ListArray {
323323
fn from(data: ArrayDataRef) -> Self {
324-
assert!(data.buffers().len() == 1);
325-
assert!(data.child_data().len() == 1);
324+
assert_eq!(data.buffers().len(), 1);
325+
assert_eq!(data.child_data().len(), 1);
326326
let values = make_array(data.child_data()[0].clone());
327327
let raw_value_offsets = data.buffers()[0].raw_data();
328328
assert!(memory::is_aligned(
@@ -331,12 +331,15 @@ impl From<ArrayDataRef> for ListArray {
331331
));
332332
let value_offsets = raw_value_offsets as *const i32;
333333
unsafe {
334-
assert!(*value_offsets.offset(0) == 0);
335-
assert!(*value_offsets.offset(data.len() as isize) == values.data().len() as i32);
334+
assert_eq!(*value_offsets.offset(0), 0);
335+
assert_eq!(
336+
*value_offsets.offset(data.len() as isize),
337+
values.data().len() as i32
338+
);
336339
}
337340
Self {
338341
data: data.clone(),
339-
values: values,
342+
values,
340343
value_offsets: RawPtrBox::new(value_offsets),
341344
}
342345
}
@@ -410,7 +413,7 @@ impl BinaryArray {
410413

411414
impl From<ArrayDataRef> for BinaryArray {
412415
fn from(data: ArrayDataRef) -> Self {
413-
assert!(data.buffers().len() == 2);
416+
assert_eq!(data.buffers().len(), 2);
414417
let raw_value_offsets = data.buffers()[0].raw_data();
415418
assert!(memory::is_aligned(
416419
raw_value_offsets,
@@ -478,10 +481,7 @@ impl From<ArrayDataRef> for StructArray {
478481
for cd in data.child_data() {
479482
boxed_fields.push(make_array(cd.clone()));
480483
}
481-
Self {
482-
data: data,
483-
boxed_fields: boxed_fields,
484-
}
484+
Self { data, boxed_fields }
485485
}
486486
}
487487

rust/src/array_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub struct ArrayDataBuilder {
156156
impl ArrayDataBuilder {
157157
pub fn new(data_type: DataType) -> Self {
158158
Self {
159-
data_type: data_type,
159+
data_type,
160160
len: 0,
161161
null_count: UNKNOWN_NULL_COUNT,
162162
null_bit_buffer: None,

rust/src/buffer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Buffer {
6060
/// Creates a buffer from an existing memory region (must already be byte-aligned)
6161
pub fn from_raw_parts(ptr: *const u8, len: usize) -> Self {
6262
assert!(memory::is_aligned(ptr, 64));
63-
let buf_data = BufferData { ptr: ptr, len: len };
63+
let buf_data = BufferData { ptr, len };
6464
Buffer {
6565
data: Arc::new(buf_data),
6666
offset: 0,
@@ -147,17 +147,17 @@ mod tests {
147147

148148
// slice with same offset should still preserve equality
149149
let buf3 = buf1.slice(2);
150-
assert!(buf1 != buf3);
150+
assert_ne!(buf1, buf3);
151151
let buf4 = buf2.slice(2);
152152
assert_eq!(buf3, buf4);
153153

154154
// unequal because of different elements
155155
buf2 = Buffer::from(&[0, 0, 2, 3, 4]);
156-
assert!(buf1 != buf2);
156+
assert_ne!(buf1, buf2);
157157

158158
// unequal because of different length
159159
buf2 = Buffer::from(&[0, 1, 2, 3]);
160-
assert!(buf1 != buf2);
160+
assert_ne!(buf1, buf2);
161161
}
162162

163163
#[test]

rust/src/datatypes.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::fmt;
1919
use std::mem::size_of;
2020
use std::slice::from_raw_parts;
2121

22-
use error::ArrowError;
22+
use error::{ArrowError, Result};
2323
use serde_json::Value;
2424

2525
/// Arrow data type
@@ -92,8 +92,7 @@ where
9292

9393
impl DataType {
9494
/// Parse a data type from a JSON representation
95-
fn from(json: &Value) -> Result<DataType, ArrowError> {
96-
//println!("DataType::from({:?})", json);
95+
fn from(json: &Value) -> Result<DataType> {
9796
match *json {
9897
Value::Object(ref map) => match map.get("name") {
9998
Some(s) if s == "bool" => Ok(DataType::Boolean),
@@ -148,7 +147,7 @@ impl DataType {
148147
let fields = fields_array
149148
.iter()
150149
.map(|f| Field::from(f))
151-
.collect::<Result<Vec<Field>, ArrowError>>();
150+
.collect::<Result<Vec<Field>>>();
152151
Ok(DataType::Struct(fields?))
153152
}
154153
_ => Err(ArrowError::ParseError("empty type".to_string())),
@@ -193,8 +192,8 @@ impl Field {
193192
pub fn new(name: &str, data_type: DataType, nullable: bool) -> Self {
194193
Field {
195194
name: name.to_string(),
196-
data_type: data_type,
197-
nullable: nullable,
195+
data_type,
196+
nullable,
198197
}
199198
}
200199

@@ -211,8 +210,7 @@ impl Field {
211210
}
212211

213212
/// Parse a field definition from a JSON representation
214-
pub fn from(json: &Value) -> Result<Self, ArrowError> {
215-
//println!("Field::from({:?}", json);
213+
pub fn from(json: &Value) -> Result<Self> {
216214
match *json {
217215
Value::Object(ref map) => {
218216
let name = match map.get("name") {
@@ -284,7 +282,7 @@ impl Schema {
284282
}
285283

286284
pub fn new(columns: Vec<Field>) -> Self {
287-
Schema { columns: columns }
285+
Schema { columns }
288286
}
289287

290288
pub fn columns(&self) -> &Vec<Field> {

rust/src/memory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use libc;
1919
use std::mem;
2020

21-
use super::error::ArrowError;
21+
use super::error::{ArrowError, Result};
2222

2323
const ALIGNMENT: usize = 64;
2424

@@ -30,7 +30,7 @@ extern "C" {
3030
}
3131

3232
#[cfg(windows)]
33-
pub fn allocate_aligned(size: i64) -> Result<*mut u8, ArrowError> {
33+
pub fn allocate_aligned(size: i64) -> Result<*mut u8> {
3434
let page = unsafe { _aligned_malloc(size as libc::size_t, ALIGNMENT as libc::size_t) };
3535
match page {
3636
0 => Err(ArrowError::MemoryError(
@@ -41,7 +41,7 @@ pub fn allocate_aligned(size: i64) -> Result<*mut u8, ArrowError> {
4141
}
4242

4343
#[cfg(not(windows))]
44-
pub fn allocate_aligned(size: i64) -> Result<*mut u8, ArrowError> {
44+
pub fn allocate_aligned(size: i64) -> Result<*mut u8> {
4545
unsafe {
4646
let mut page: *mut libc::c_void = mem::uninitialized();
4747
let result = libc::posix_memalign(&mut page, ALIGNMENT, size as usize);

rust/src/memory_pool.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,20 @@ use libc;
1919
use std::cmp;
2020
use std::mem;
2121

22-
use super::error::ArrowError;
22+
use super::error::Result;
2323
use super::memory::{allocate_aligned, free_aligned};
2424

2525
/// Memory pool for allocating memory. It's also responsible for tracking memory usage.
2626
pub trait MemoryPool {
2727
/// Allocate memory.
2828
/// The implementation should ensures that allocated memory is aligned.
29-
fn allocate(&self, size: usize) -> Result<*mut u8, ArrowError>;
29+
fn allocate(&self, size: usize) -> Result<*mut u8>;
3030

3131
/// Reallocate memory.
3232
/// If the implementation doesn't support reallocating aligned memory, it allocates new memory
3333
/// and copied old memory to it.
34-
fn reallocate(
35-
&self,
36-
old_size: usize,
37-
new_size: usize,
38-
pointer: *const u8,
39-
) -> Result<*const u8, ArrowError>;
34+
fn reallocate(&self, old_size: usize, new_size: usize, pointer: *const u8)
35+
-> Result<*const u8>;
4036

4137
/// Free memory.
4238
fn free(&self, ptr: *const u8);
@@ -47,7 +43,7 @@ pub trait MemoryPool {
4743
struct LibcMemoryPool;
4844

4945
impl MemoryPool for LibcMemoryPool {
50-
fn allocate(&self, size: usize) -> Result<*mut u8, ArrowError> {
46+
fn allocate(&self, size: usize) -> Result<*mut u8> {
5147
allocate_aligned(size as i64)
5248
}
5349

@@ -56,7 +52,7 @@ impl MemoryPool for LibcMemoryPool {
5652
old_size: usize,
5753
new_size: usize,
5854
pointer: *const u8,
59-
) -> Result<*const u8, ArrowError> {
55+
) -> Result<*const u8> {
6056
unsafe {
6157
let old_src = mem::transmute::<*const u8, *mut libc::c_void>(pointer);
6258
let result = self.allocate(new_size)?;
@@ -75,6 +71,7 @@ impl MemoryPool for LibcMemoryPool {
7571
#[cfg(test)]
7672
mod tests {
7773
use super::*;
74+
7875
const ALIGNMENT: usize = 64;
7976

8077
#[test]

0 commit comments

Comments
 (0)