From bf075f6312c9cd9d54b801da70872bb2dd760b66 Mon Sep 17 00:00:00 2001 From: Liang-Chi Hsieh Date: Sat, 3 Sep 2022 22:38:59 -0700 Subject: [PATCH] Hide ArrowNativeTypeOp --- arrow/src/compute/kernels/arithmetic.rs | 4 +- arrow/src/datatypes/native.rs | 96 +++++++++++++------------ 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/arrow/src/compute/kernels/arithmetic.rs b/arrow/src/compute/kernels/arithmetic.rs index 9bd6047f508..53f48570d92 100644 --- a/arrow/src/compute/kernels/arithmetic.rs +++ b/arrow/src/compute/kernels/arithmetic.rs @@ -35,8 +35,8 @@ use crate::compute::unary_dyn; use crate::compute::util::combine_option_bitmap; use crate::datatypes; use crate::datatypes::{ - ArrowNativeTypeOp, ArrowNumericType, ArrowPrimitiveType, DataType, Date32Type, - Date64Type, IntervalDayTimeType, IntervalMonthDayNanoType, IntervalUnit, + native_op::ArrowNativeTypeOp, ArrowNumericType, ArrowPrimitiveType, DataType, + Date32Type, Date64Type, IntervalDayTimeType, IntervalMonthDayNanoType, IntervalUnit, IntervalYearMonthType, }; use crate::datatypes::{ diff --git a/arrow/src/datatypes/native.rs b/arrow/src/datatypes/native.rs index f7584fe419c..ce7536fa9a9 100644 --- a/arrow/src/datatypes/native.rs +++ b/arrow/src/datatypes/native.rs @@ -17,7 +17,6 @@ use super::DataType; use half::f16; -use std::ops::{Add, Div, Mul, Sub}; mod private { pub trait Sealed {} @@ -115,59 +114,64 @@ pub trait ArrowPrimitiveType: 'static { } } -/// Trait for ArrowNativeType to provide overflow-checking and non-overflow-checking -/// variants for arithmetic operations. For floating point types, this provides some -/// default implementations. Integer types that need to deal with overflow can implement -/// this trait. -/// -/// The APIs with `wrapping` suffix are the variant of non-overflow-checking. If overflow -/// occurred, they will supposedly wrap around the boundary of the type. -/// -/// The APIs with `_check` suffix are the variant of overflow-checking which return `None` -/// if overflow occurred. -pub trait ArrowNativeTypeOp: - ArrowNativeType - + Add - + Sub - + Mul - + Div -{ - fn add_checked(self, rhs: Self) -> Option { - Some(self + rhs) - } +pub(crate) mod native_op { + use super::ArrowNativeType; + use std::ops::{Add, Div, Mul, Sub}; - fn add_wrapping(self, rhs: Self) -> Self { - self + rhs - } + /// Trait for ArrowNativeType to provide overflow-checking and non-overflow-checking + /// variants for arithmetic operations. For floating point types, this provides some + /// default implementations. Integer types that need to deal with overflow can implement + /// this trait. + /// + /// The APIs with `wrapping` suffix are the variant of non-overflow-checking. If overflow + /// occurred, they will supposedly wrap around the boundary of the type. + /// + /// The APIs with `_checked` suffix are the variant of overflow-checking which return `None` + /// if overflow occurred. + pub trait ArrowNativeTypeOp: + ArrowNativeType + + Add + + Sub + + Mul + + Div + { + fn add_checked(self, rhs: Self) -> Option { + Some(self + rhs) + } - fn sub_checked(self, rhs: Self) -> Option { - Some(self - rhs) - } + fn add_wrapping(self, rhs: Self) -> Self { + self + rhs + } - fn sub_wrapping(self, rhs: Self) -> Self { - self - rhs - } + fn sub_checked(self, rhs: Self) -> Option { + Some(self - rhs) + } - fn mul_checked(self, rhs: Self) -> Option { - Some(self * rhs) - } + fn sub_wrapping(self, rhs: Self) -> Self { + self - rhs + } - fn mul_wrapping(self, rhs: Self) -> Self { - self * rhs - } + fn mul_checked(self, rhs: Self) -> Option { + Some(self * rhs) + } - fn div_checked(self, rhs: Self) -> Option { - Some(self / rhs) - } + fn mul_wrapping(self, rhs: Self) -> Self { + self * rhs + } - fn div_wrapping(self, rhs: Self) -> Self { - self / rhs + fn div_checked(self, rhs: Self) -> Option { + Some(self / rhs) + } + + fn div_wrapping(self, rhs: Self) -> Self { + self / rhs + } } } macro_rules! native_type_op { ($t:tt) => { - impl ArrowNativeTypeOp for $t { + impl native_op::ArrowNativeTypeOp for $t { fn add_checked(self, rhs: Self) -> Option { self.checked_add(rhs) } @@ -176,7 +180,7 @@ macro_rules! native_type_op { self.wrapping_add(rhs) } - fn sub_checked(self, rhs: Self) -> Option { + fn sub_checked(self, rhs: Self) -> Option {div_wrapping self.checked_sub(rhs) } @@ -212,9 +216,9 @@ native_type_op!(u16); native_type_op!(u32); native_type_op!(u64); -impl ArrowNativeTypeOp for f16 {} -impl ArrowNativeTypeOp for f32 {} -impl ArrowNativeTypeOp for f64 {} +impl native_op::ArrowNativeTypeOp for f16 {} +impl native_op::ArrowNativeTypeOp for f32 {} +impl native_op::ArrowNativeTypeOp for f64 {} impl private::Sealed for i8 {} impl ArrowNativeType for i8 {