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

Removed unused NativeType::to_ne_bytes #1112

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/array/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,7 @@ impl<O: Offset> Utf8Array<O> {
P: AsRef<str>,
I: TrustedLen<Item = Option<P>>,
{
// soundness: I is `TrustedLen`
unsafe { Self::from_trusted_len_iter_unchecked(iterator) }
MutableUtf8Array::<O>::from_trusted_len_iter(iterator).into()
}

/// Creates a [`Utf8Array`] from an falible iterator of trusted length.
Expand All @@ -512,8 +511,7 @@ impl<O: Offset> Utf8Array<O> {
P: AsRef<str>,
I: TrustedLen<Item = std::result::Result<Option<P>, E>>,
{
// soundness: I: TrustedLen
unsafe { Self::try_from_trusted_len_iter_unchecked(iter) }
MutableUtf8Array::<O>::try_from_trusted_len_iter(iter).map(|x| x.into())
}

/// Alias for `new`
Expand Down
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
18 changes: 18 additions & 0 deletions tests/it/array/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,21 @@ fn into_mut_4() {
let array = Utf8Array::<i32>::new(DataType::Utf8, offsets, values, validity);
assert!(array.into_mut().is_right());
}

#[test]
fn rev_iter() {
let array = Utf8Array::<i32>::from(&[Some("hello"), Some(" "), None]);

assert_eq!(
array.into_iter().rev().collect::<Vec<_>>(),
vec![None, Some(" "), Some("hello")]
);
}

#[test]
fn iter_nth() {
let array = Utf8Array::<i32>::from(&[Some("hello"), Some(" "), None]);

assert_eq!(array.iter().nth(1), Some(Some(" ")));
assert_eq!(array.iter().nth(10), None);
}
26 changes: 26 additions & 0 deletions tests/it/array/utf8/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,29 @@ fn test_extend_values() {
assert_eq!(array.offsets().as_slice(), &[0, 2, 7, 12, 17]);
assert_eq!(array.validity(), None,);
}

#[test]
fn test_extend() {
let mut array = MutableUtf8Array::<i32>::new();

array.extend([Some("hi"), None, Some("there"), None].into_iter());

let array: Utf8Array<i32> = array.into();

assert_eq!(
array,
Utf8Array::<i32>::from([Some("hi"), None, Some("there"), None])
);
}

#[test]
fn as_arc() {
let mut array = MutableUtf8Array::<i32>::new();

array.extend([Some("hi"), None, Some("there"), None].into_iter());

assert_eq!(
Utf8Array::<i32>::from([Some("hi"), None, Some("there"), None]),
array.as_arc().as_ref()
);
}
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()));
}