viirya commented on code in PR #2643:
URL: https://github.com/apache/arrow-rs/pull/2643#discussion_r962194103
##########
arrow/src/datatypes/native.rs:
##########
@@ -114,6 +115,98 @@ pub trait ArrowPrimitiveType: 'static {
}
}
+/// Trait for ArrowNativeType to provide overflow-aware operations.
+pub trait ArrowNativeTypeOp:
+ ArrowNativeType
+ + Add<Output = Self>
+ + Sub<Output = Self>
+ + Mul<Output = Self>
+ + Div<Output = Self>
+{
+ fn checked_add_if_applied(self, rhs: Self) -> Option<Self> {
+ Some(self + rhs)
+ }
+
+ fn wrapping_add_if_applied(self, rhs: Self) -> Self {
+ self + rhs
+ }
+
+ fn checked_sub_if_applied(self, rhs: Self) -> Option<Self> {
+ Some(self - rhs)
+ }
+
+ fn wrapping_sub_if_applied(self, rhs: Self) -> Self {
+ self + rhs
+ }
+
+ fn checked_mul_if_applied(self, rhs: Self) -> Option<Self> {
+ Some(self * rhs)
+ }
+
+ fn wrapping_mul_if_applied(self, rhs: Self) -> Self {
+ self * rhs
+ }
+
+ fn checked_div_if_applied(self, rhs: Self) -> Option<Self> {
+ Some(self / rhs)
+ }
+
+ fn wrapping_div_if_applied(self, rhs: Self) -> Self {
+ self / rhs
+ }
+}
+
+macro_rules! native_type_op {
+ ($t:tt) => {
+ impl ArrowNativeTypeOp for $t {
+ fn checked_add_if_applied(self, rhs: Self) -> Option<Self> {
Review Comment:
We can just have one API name here as we cannot distinguish what native type
we are calling from the kernels. That's why I make such API name. :)
I can rename it to `add_checked` and add some comments to say for floating
point types, it is simply `add` without check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]