From c7e1a4d6f4e3012543c5888e669ccb09d786f18f Mon Sep 17 00:00:00 2001 From: Jorge Leitao Date: Thu, 12 Aug 2021 07:36:02 +0100 Subject: [PATCH] Simplified code. (#277) --- src/io/ipc/read/read_basic.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/io/ipc/read/read_basic.rs b/src/io/ipc/read/read_basic.rs index 421bf0d3656..fd84edee754 100644 --- a/src/io/ipc/read/read_basic.rs +++ b/src/io/ipc/read/read_basic.rs @@ -10,18 +10,19 @@ use crate::{bitmap::Bitmap, buffer::MutableBuffer, types::NativeType}; use super::super::compression; use super::super::gen; -fn read_big_endian( +fn read_swapped( reader: &mut R, - bytes: usize, + length: usize, buffer: &mut MutableBuffer, is_little_endian: bool, ) -> Result<()> { // slow case where we must reverse bits - let mut slice = vec![0u8; bytes]; + let mut slice = vec![0u8; length * std::mem::size_of::()]; reader.read_exact(&mut slice)?; + let chunks = slice.chunks_exact(std::mem::size_of::()); if !is_little_endian { - let chunks = slice.chunks_exact(std::mem::size_of::()); + // machine is little endian, file is big endian buffer .as_mut_slice() .iter_mut() @@ -35,6 +36,9 @@ fn read_big_endian( Result::Ok(()) }) .unwrap(); + } else { + // machine is big endian, file is little endian + todo!("reading little endian files from big endian machines not yet implemented.") } Ok(()) } @@ -42,10 +46,10 @@ fn read_big_endian( fn read_uncompressed_buffer( reader: &mut R, buffer_length: usize, - bytes: usize, length: usize, is_little_endian: bool, ) -> Result> { + let bytes = length * std::mem::size_of::(); if bytes > buffer_length { return Err(ArrowError::Ipc( format!("The slots of the array times the physical size must \ @@ -68,11 +72,14 @@ fn read_uncompressed_buffer( // fast case where we can just copy the contents as is unsafe { // transmute T to bytes. - let slice = std::slice::from_raw_parts_mut(buffer.as_mut_ptr() as *mut u8, bytes); + let slice = std::slice::from_raw_parts_mut( + buffer.as_mut_ptr() as *mut u8, + length * std::mem::size_of::(), + ); reader.read_exact(slice)?; } } else { - read_big_endian(reader, bytes, &mut buffer, is_little_endian)?; + read_swapped(reader, length, &mut buffer, is_little_endian)?; } Ok(buffer) } @@ -144,18 +151,13 @@ pub fn read_buffer( let buffer_length = buf.length() as usize; - let bytes = length * std::mem::size_of::(); - if let Some(compression) = compression { Ok( read_compressed_buffer(reader, buffer_length, length, is_little_endian, compression)? .into(), ) } else { - Ok( - read_uncompressed_buffer(reader, buffer_length, bytes, length, is_little_endian)? - .into(), - ) + Ok(read_uncompressed_buffer(reader, buffer_length, length, is_little_endian)?.into()) } }