From 95da017492f5c24d881de2063803b296caa475a1 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Sun, 2 Jun 2024 11:41:34 +0800 Subject: [PATCH 1/3] naive impl Signed-off-by: Ruihang Xia --- arrow-buffer/src/interval.rs | 54 +++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/arrow-buffer/src/interval.rs b/arrow-buffer/src/interval.rs index 7e8043e9a724..3c31caae4f17 100644 --- a/arrow-buffer/src/interval.rs +++ b/arrow-buffer/src/interval.rs @@ -16,7 +16,7 @@ // under the License. use crate::arith::derive_arith; -use std::ops::Neg; +use std::ops::{AddAssign, Neg, SubAssign}; /// Value of an IntervalMonthDayNano array #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] @@ -225,6 +225,34 @@ impl Neg for IntervalMonthDayNano { } } +impl AddAssign for IntervalMonthDayNano { + #[cfg(debug_assertions)] + fn add_assign(&mut self, rhs: Self) { + *self = self + .checked_add(rhs) + .expect("IntervalMonthDayNano overflow"); + } + + #[cfg(not(debug_assertions))] + fn add_assign(&mut self, rhs: Self) { + *self = self.wrapping_add(rhs); + } +} + +impl SubAssign for IntervalMonthDayNano { + #[cfg(debug_assertions)] + fn sub_assign(&mut self, rhs: Self) { + *self = self + .checked_sub(rhs) + .expect("IntervalMonthDayNano underflow"); + } + + #[cfg(not(debug_assertions))] + fn sub_assign(&mut self, rhs: Self) { + *self = self.wrapping_sub(rhs); + } +} + derive_arith!(IntervalMonthDayNano, Add, add, wrapping_add, checked_add); derive_arith!(IntervalMonthDayNano, Sub, sub, wrapping_sub, checked_sub); derive_arith!(IntervalMonthDayNano, Mul, mul, wrapping_mul, checked_mul); @@ -417,6 +445,30 @@ impl Neg for IntervalDayTime { } } +impl AddAssign for IntervalDayTime { + #[cfg(debug_assertions)] + fn add_assign(&mut self, rhs: Self) { + *self = self.checked_add(rhs).expect("IntervalDayTime overflow"); + } + + #[cfg(not(debug_assertions))] + fn add_assign(&mut self, rhs: Self) { + *self = self.wrapping_add(rhs); + } +} + +impl SubAssign for IntervalDayTime { + #[cfg(debug_assertions)] + fn sub_assign(&mut self, rhs: Self) { + *self = self.checked_sub(rhs).expect("IntervalDayTime underflow"); + } + + #[cfg(not(debug_assertions))] + fn sub_assign(&mut self, rhs: Self) { + *self = self.wrapping_sub(rhs); + } +} + derive_arith!(IntervalDayTime, Add, add, wrapping_add, checked_add); derive_arith!(IntervalDayTime, Sub, sub, wrapping_sub, checked_sub); derive_arith!(IntervalDayTime, Mul, mul, wrapping_mul, checked_mul); From 8ab09e1dc7fa79f982f2cb8dfff133b96fa8a53d Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Sun, 2 Jun 2024 12:01:04 +0800 Subject: [PATCH 2/3] extend macro rule Signed-off-by: Ruihang Xia --- arrow-buffer/Cargo.toml | 1 + arrow-buffer/src/arith.rs | 16 ++++++++++- arrow-buffer/src/interval.rs | 54 +----------------------------------- 3 files changed, 17 insertions(+), 54 deletions(-) diff --git a/arrow-buffer/Cargo.toml b/arrow-buffer/Cargo.toml index 8bc33b1874e4..2165f6fe050f 100644 --- a/arrow-buffer/Cargo.toml +++ b/arrow-buffer/Cargo.toml @@ -37,6 +37,7 @@ bench = false bytes = { version = "1.4" } num = { version = "0.4", default-features = false, features = ["std"] } half = { version = "2.1", default-features = false } +paste = { version = "1.0" } [dev-dependencies] criterion = { version = "0.5", default-features = false } diff --git a/arrow-buffer/src/arith.rs b/arrow-buffer/src/arith.rs index ca693c3607dc..fe7aa42b82e7 100644 --- a/arrow-buffer/src/arith.rs +++ b/arrow-buffer/src/arith.rs @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -/// Derives `std::ops::$op` for `$ty` calling `$wrapping` or `$checked` variants +/// Derives `std::ops::$t` for `$ty` calling `$wrapping` or `$checked` variants /// based on if debug_assertions enabled macro_rules! derive_arith { ($ty:ty, $t:ident, $op:ident, $wrapping:ident, $checked:ident) => { @@ -34,6 +34,20 @@ macro_rules! derive_arith { } } + ::paste::paste! { + impl std::ops::[< $t Assign >] for $ty { + #[cfg(debug_assertions)] + fn [< $op _assign >](&mut self, rhs: Self) { + *self = self.$checked(rhs).expect(concat!(stringify!($ty), " overflow")); + } + + #[cfg(not(debug_assertions))] + fn [< $op _assign >](&mut self, rhs: Self) { + *self = self.$wrapping(rhs); + } + } + } + impl<'a> std::ops::$t<$ty> for &'a $ty { type Output = $ty; diff --git a/arrow-buffer/src/interval.rs b/arrow-buffer/src/interval.rs index 3c31caae4f17..7e8043e9a724 100644 --- a/arrow-buffer/src/interval.rs +++ b/arrow-buffer/src/interval.rs @@ -16,7 +16,7 @@ // under the License. use crate::arith::derive_arith; -use std::ops::{AddAssign, Neg, SubAssign}; +use std::ops::Neg; /// Value of an IntervalMonthDayNano array #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] @@ -225,34 +225,6 @@ impl Neg for IntervalMonthDayNano { } } -impl AddAssign for IntervalMonthDayNano { - #[cfg(debug_assertions)] - fn add_assign(&mut self, rhs: Self) { - *self = self - .checked_add(rhs) - .expect("IntervalMonthDayNano overflow"); - } - - #[cfg(not(debug_assertions))] - fn add_assign(&mut self, rhs: Self) { - *self = self.wrapping_add(rhs); - } -} - -impl SubAssign for IntervalMonthDayNano { - #[cfg(debug_assertions)] - fn sub_assign(&mut self, rhs: Self) { - *self = self - .checked_sub(rhs) - .expect("IntervalMonthDayNano underflow"); - } - - #[cfg(not(debug_assertions))] - fn sub_assign(&mut self, rhs: Self) { - *self = self.wrapping_sub(rhs); - } -} - derive_arith!(IntervalMonthDayNano, Add, add, wrapping_add, checked_add); derive_arith!(IntervalMonthDayNano, Sub, sub, wrapping_sub, checked_sub); derive_arith!(IntervalMonthDayNano, Mul, mul, wrapping_mul, checked_mul); @@ -445,30 +417,6 @@ impl Neg for IntervalDayTime { } } -impl AddAssign for IntervalDayTime { - #[cfg(debug_assertions)] - fn add_assign(&mut self, rhs: Self) { - *self = self.checked_add(rhs).expect("IntervalDayTime overflow"); - } - - #[cfg(not(debug_assertions))] - fn add_assign(&mut self, rhs: Self) { - *self = self.wrapping_add(rhs); - } -} - -impl SubAssign for IntervalDayTime { - #[cfg(debug_assertions)] - fn sub_assign(&mut self, rhs: Self) { - *self = self.checked_sub(rhs).expect("IntervalDayTime underflow"); - } - - #[cfg(not(debug_assertions))] - fn sub_assign(&mut self, rhs: Self) { - *self = self.wrapping_sub(rhs); - } -} - derive_arith!(IntervalDayTime, Add, add, wrapping_add, checked_add); derive_arith!(IntervalDayTime, Sub, sub, wrapping_sub, checked_sub); derive_arith!(IntervalDayTime, Mul, mul, wrapping_mul, checked_mul); From 33b69f806f2f882be27c93ec5c7a6ec34f034e31 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Sun, 2 Jun 2024 15:31:57 +0800 Subject: [PATCH 3/3] remove quote! Signed-off-by: Ruihang Xia --- arrow-buffer/Cargo.toml | 1 - arrow-buffer/src/arith.rs | 22 ++++---- arrow-buffer/src/bigint/mod.rs | 50 +++++++++++++++-- arrow-buffer/src/interval.rs | 100 +++++++++++++++++++++++++++++---- 4 files changed, 146 insertions(+), 27 deletions(-) diff --git a/arrow-buffer/Cargo.toml b/arrow-buffer/Cargo.toml index 2165f6fe050f..8bc33b1874e4 100644 --- a/arrow-buffer/Cargo.toml +++ b/arrow-buffer/Cargo.toml @@ -37,7 +37,6 @@ bench = false bytes = { version = "1.4" } num = { version = "0.4", default-features = false, features = ["std"] } half = { version = "2.1", default-features = false } -paste = { version = "1.0" } [dev-dependencies] criterion = { version = "0.5", default-features = false } diff --git a/arrow-buffer/src/arith.rs b/arrow-buffer/src/arith.rs index fe7aa42b82e7..a576b2677131 100644 --- a/arrow-buffer/src/arith.rs +++ b/arrow-buffer/src/arith.rs @@ -18,7 +18,7 @@ /// Derives `std::ops::$t` for `$ty` calling `$wrapping` or `$checked` variants /// based on if debug_assertions enabled macro_rules! derive_arith { - ($ty:ty, $t:ident, $op:ident, $wrapping:ident, $checked:ident) => { + ($ty:ty, $t:ident, $t_assign:ident, $op:ident, $op_assign:ident, $wrapping:ident, $checked:ident) => { impl std::ops::$t for $ty { type Output = $ty; @@ -34,17 +34,17 @@ macro_rules! derive_arith { } } - ::paste::paste! { - impl std::ops::[< $t Assign >] for $ty { - #[cfg(debug_assertions)] - fn [< $op _assign >](&mut self, rhs: Self) { - *self = self.$checked(rhs).expect(concat!(stringify!($ty), " overflow")); - } + impl std::ops::$t_assign for $ty { + #[cfg(debug_assertions)] + fn $op_assign(&mut self, rhs: Self) { + *self = self + .$checked(rhs) + .expect(concat!(stringify!($ty), " overflow")); + } - #[cfg(not(debug_assertions))] - fn [< $op _assign >](&mut self, rhs: Self) { - *self = self.$wrapping(rhs); - } + #[cfg(not(debug_assertions))] + fn $op_assign(&mut self, rhs: Self) { + *self = self.$wrapping(rhs); } } diff --git a/arrow-buffer/src/bigint/mod.rs b/arrow-buffer/src/bigint/mod.rs index bbe65b073aa6..c3296fed3753 100644 --- a/arrow-buffer/src/bigint/mod.rs +++ b/arrow-buffer/src/bigint/mod.rs @@ -639,11 +639,51 @@ fn mulx(a: u128, b: u128) -> (u128, u128) { (low, high) } -derive_arith!(i256, Add, add, wrapping_add, checked_add); -derive_arith!(i256, Sub, sub, wrapping_sub, checked_sub); -derive_arith!(i256, Mul, mul, wrapping_mul, checked_mul); -derive_arith!(i256, Div, div, wrapping_div, checked_div); -derive_arith!(i256, Rem, rem, wrapping_rem, checked_rem); +derive_arith!( + i256, + Add, + AddAssign, + add, + add_assign, + wrapping_add, + checked_add +); +derive_arith!( + i256, + Sub, + SubAssign, + sub, + sub_assign, + wrapping_sub, + checked_sub +); +derive_arith!( + i256, + Mul, + MulAssign, + mul, + mul_assign, + wrapping_mul, + checked_mul +); +derive_arith!( + i256, + Div, + DivAssign, + div, + div_assign, + wrapping_div, + checked_div +); +derive_arith!( + i256, + Rem, + RemAssign, + rem, + rem_assign, + wrapping_rem, + checked_rem +); impl Neg for i256 { type Output = i256; diff --git a/arrow-buffer/src/interval.rs b/arrow-buffer/src/interval.rs index 7e8043e9a724..bed3b2e31ada 100644 --- a/arrow-buffer/src/interval.rs +++ b/arrow-buffer/src/interval.rs @@ -225,11 +225,51 @@ impl Neg for IntervalMonthDayNano { } } -derive_arith!(IntervalMonthDayNano, Add, add, wrapping_add, checked_add); -derive_arith!(IntervalMonthDayNano, Sub, sub, wrapping_sub, checked_sub); -derive_arith!(IntervalMonthDayNano, Mul, mul, wrapping_mul, checked_mul); -derive_arith!(IntervalMonthDayNano, Div, div, wrapping_div, checked_div); -derive_arith!(IntervalMonthDayNano, Rem, rem, wrapping_rem, checked_rem); +derive_arith!( + IntervalMonthDayNano, + Add, + AddAssign, + add, + add_assign, + wrapping_add, + checked_add +); +derive_arith!( + IntervalMonthDayNano, + Sub, + SubAssign, + sub, + sub_assign, + wrapping_sub, + checked_sub +); +derive_arith!( + IntervalMonthDayNano, + Mul, + MulAssign, + mul, + mul_assign, + wrapping_mul, + checked_mul +); +derive_arith!( + IntervalMonthDayNano, + Div, + DivAssign, + div, + div_assign, + wrapping_div, + checked_div +); +derive_arith!( + IntervalMonthDayNano, + Rem, + RemAssign, + rem, + rem_assign, + wrapping_rem, + checked_rem +); /// Value of an IntervalDayTime array #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] @@ -417,8 +457,48 @@ impl Neg for IntervalDayTime { } } -derive_arith!(IntervalDayTime, Add, add, wrapping_add, checked_add); -derive_arith!(IntervalDayTime, Sub, sub, wrapping_sub, checked_sub); -derive_arith!(IntervalDayTime, Mul, mul, wrapping_mul, checked_mul); -derive_arith!(IntervalDayTime, Div, div, wrapping_div, checked_div); -derive_arith!(IntervalDayTime, Rem, rem, wrapping_rem, checked_rem); +derive_arith!( + IntervalDayTime, + Add, + AddAssign, + add, + add_assign, + wrapping_add, + checked_add +); +derive_arith!( + IntervalDayTime, + Sub, + SubAssign, + sub, + sub_assign, + wrapping_sub, + checked_sub +); +derive_arith!( + IntervalDayTime, + Mul, + MulAssign, + mul, + mul_assign, + wrapping_mul, + checked_mul +); +derive_arith!( + IntervalDayTime, + Div, + DivAssign, + div, + div_assign, + wrapping_div, + checked_div +); +derive_arith!( + IntervalDayTime, + Rem, + RemAssign, + rem, + rem_assign, + wrapping_rem, + checked_rem +);