diff --git a/src/types/native.rs b/src/types/native.rs index fbc8ac408b3..4d4f34eb602 100644 --- a/src/types/native.rs +++ b/src/types/native.rs @@ -33,9 +33,6 @@ pub trait NativeType: /// To bytes in little endian fn to_le_bytes(&self) -> Self::Bytes; - /// To bytes in native endian - fn to_ne_bytes(&self) -> Self::Bytes; - /// To bytes in big endian fn to_be_bytes(&self) -> Self::Bytes; @@ -59,11 +56,6 @@ macro_rules! native_type { Self::to_be_bytes(*self) } - #[inline] - fn to_ne_bytes(&self) -> Self::Bytes { - Self::to_ne_bytes(*self) - } - #[inline] fn from_be_bytes(bytes: Self::Bytes) -> Self { Self::from_be_bytes(bytes) @@ -88,25 +80,25 @@ native_type!(i128, PrimitiveType::Int128); #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash, Zeroable, Pod)] #[allow(non_camel_case_types)] #[repr(C)] -pub struct days_ms([i32; 2]); +pub struct days_ms(pub i32, pub i32); impl days_ms { /// A new [`days_ms`]. #[inline] pub fn new(days: i32, milliseconds: i32) -> Self { - Self([days, milliseconds]) + Self(days, milliseconds) } /// The number of days #[inline] pub fn days(&self) -> i32 { - self.0[0] + self.0 } /// The number of milliseconds #[inline] pub fn milliseconds(&self) -> i32 { - self.0[1] + self.1 } } @@ -115,24 +107,8 @@ impl NativeType for days_ms { type Bytes = [u8; 8]; #[inline] fn to_le_bytes(&self) -> Self::Bytes { - let days = self.0[0].to_le_bytes(); - let ms = self.0[1].to_le_bytes(); - let mut result = [0; 8]; - result[0] = days[0]; - result[1] = days[1]; - result[2] = days[2]; - result[3] = days[3]; - result[4] = ms[0]; - result[5] = ms[1]; - result[6] = ms[2]; - result[7] = ms[3]; - result - } - - #[inline] - fn to_ne_bytes(&self) -> Self::Bytes { - let days = self.0[0].to_ne_bytes(); - let ms = self.0[1].to_ne_bytes(); + let days = self.0.to_le_bytes(); + let ms = self.1.to_le_bytes(); let mut result = [0; 8]; result[0] = days[0]; result[1] = days[1]; @@ -147,8 +123,8 @@ impl NativeType for days_ms { #[inline] fn to_be_bytes(&self) -> Self::Bytes { - let days = self.0[0].to_be_bytes(); - let ms = self.0[1].to_be_bytes(); + let days = self.0.to_be_bytes(); + let ms = self.1.to_be_bytes(); let mut result = [0; 8]; result[0] = days[0]; result[1] = days[1]; @@ -173,7 +149,7 @@ impl NativeType for days_ms { ms[1] = bytes[5]; ms[2] = bytes[6]; ms[3] = bytes[7]; - Self([i32::from_be_bytes(days), i32::from_be_bytes(ms)]) + Self(i32::from_be_bytes(days), i32::from_be_bytes(ms)) } } @@ -181,7 +157,7 @@ impl NativeType for days_ms { #[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash, Zeroable, Pod)] #[allow(non_camel_case_types)] #[repr(C)] -pub struct months_days_ns(i32, i32, i64); +pub struct months_days_ns(pub i32, pub i32, pub i64); impl months_days_ns { /// A new [`months_days_ns`]. @@ -232,26 +208,6 @@ impl NativeType for months_days_ns { result } - #[inline] - fn to_ne_bytes(&self) -> Self::Bytes { - let months = self.months().to_ne_bytes(); - let days = self.days().to_ne_bytes(); - let ns = self.ns().to_ne_bytes(); - let mut result = [0; 16]; - result[0] = months[0]; - result[1] = months[1]; - result[2] = months[2]; - result[3] = months[3]; - result[4] = days[0]; - result[5] = days[1]; - result[6] = days[2]; - result[7] = days[3]; - (0..8).for_each(|i| { - result[8 + i] = ns[i]; - }); - result - } - #[inline] fn to_be_bytes(&self) -> Self::Bytes { let months = self.months().to_be_bytes(); @@ -420,11 +376,6 @@ impl NativeType for f16 { self.0.to_le_bytes() } - #[inline] - fn to_ne_bytes(&self) -> Self::Bytes { - self.0.to_ne_bytes() - } - #[inline] fn to_be_bytes(&self) -> Self::Bytes { self.0.to_be_bytes() diff --git a/tests/it/array/primitive/mod.rs b/tests/it/array/primitive/mod.rs index 058d503f479..5a618cba256 100644 --- a/tests/it/array/primitive/mod.rs +++ b/tests/it/array/primitive/mod.rs @@ -72,7 +72,7 @@ fn from() { } #[test] -fn months_days_ns() { +fn months_days_ns_from_slice() { let data = &[ months_days_ns::new(1, 1, 2), months_days_ns::new(1, 1, 3), diff --git a/tests/it/types.rs b/tests/it/types.rs index 7dfa07cc167..ee337e901fb 100644 --- a/tests/it/types.rs +++ b/tests/it/types.rs @@ -1,4 +1,4 @@ -use arrow2::types::{BitChunkIter, BitChunkOnes}; +use arrow2::types::{days_ms, months_days_ns, BitChunkIter, BitChunkOnes, NativeType}; #[test] fn test_basic1() { @@ -18,3 +18,23 @@ fn test_ones() { assert_eq!(iter.next(), Some(0)); assert_eq!(iter.next(), Some(12)); } + +#[test] +fn months_days_ns_roundtrip() { + let a = months_days_ns(1, 2, 3); + let bytes = a.to_le_bytes(); + assert_eq!(bytes, [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0]); + + let a = months_days_ns(1, 1, 1); + assert_eq!(a, months_days_ns::from_be_bytes(a.to_be_bytes())); +} + +#[test] +fn days_ms_roundtrip() { + let a = days_ms(1, 2); + let bytes = a.to_le_bytes(); + assert_eq!(bytes, [1, 0, 0, 0, 2, 0, 0, 0]); + + let a = days_ms(1, 2); + assert_eq!(a, days_ms::from_be_bytes(a.to_be_bytes())); +}