Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Removed to_ne_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Jun 27, 2022
1 parent 873832e commit c2d01f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 61 deletions.
69 changes: 10 additions & 59 deletions src/types/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand All @@ -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
}
}

Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -173,15 +149,15 @@ 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))
}
}

/// The in-memory representation of the MonthDayNano variant of the "Interval" logical type.
#[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`].
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/it/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
22 changes: 21 additions & 1 deletion tests/it/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use arrow2::types::{BitChunkIter, BitChunkOnes};
use arrow2::types::{days_ms, months_days_ns, BitChunkIter, BitChunkOnes, NativeType};

#[test]
fn test_basic1() {
Expand All @@ -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()));
}

0 comments on commit c2d01f5

Please sign in to comment.