Skip to content

Commit

Permalink
Hide ArrowNativeTypeOp
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Sep 4, 2022
1 parent 3d98aff commit bf075f6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 48 deletions.
4 changes: 2 additions & 2 deletions arrow/src/compute/kernels/arithmetic.rs
Expand Up @@ -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::{
Expand Down
96 changes: 50 additions & 46 deletions arrow/src/datatypes/native.rs
Expand Up @@ -17,7 +17,6 @@

use super::DataType;
use half::f16;
use std::ops::{Add, Div, Mul, Sub};

mod private {
pub trait Sealed {}
Expand Down Expand Up @@ -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<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
{
fn add_checked(self, rhs: Self) -> Option<Self> {
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<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
{
fn add_checked(self, rhs: Self) -> Option<Self> {
Some(self + rhs)
}

fn sub_checked(self, rhs: Self) -> Option<Self> {
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<Self> {
Some(self - rhs)
}

fn mul_checked(self, rhs: Self) -> Option<Self> {
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<Self> {
Some(self * rhs)
}

fn div_checked(self, rhs: Self) -> Option<Self> {
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<Self> {
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> {
self.checked_add(rhs)
}
Expand All @@ -176,7 +180,7 @@ macro_rules! native_type_op {
self.wrapping_add(rhs)
}

fn sub_checked(self, rhs: Self) -> Option<Self> {
fn sub_checked(self, rhs: Self) -> Option<Self> {div_wrapping
self.checked_sub(rhs)
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bf075f6

Please sign in to comment.