Skip to content

Commit 13a26d6

Browse files
authored
feat: impl *Assign ops for types in arrow-buffer (#5832)
* naive impl Signed-off-by: Ruihang Xia <[email protected]> * extend macro rule Signed-off-by: Ruihang Xia <[email protected]> * remove quote! Signed-off-by: Ruihang Xia <[email protected]> --------- Signed-off-by: Ruihang Xia <[email protected]>
1 parent 065151b commit 13a26d6

File tree

3 files changed

+151
-17
lines changed

3 files changed

+151
-17
lines changed

arrow-buffer/src/arith.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
/// Derives `std::ops::$op` for `$ty` calling `$wrapping` or `$checked` variants
18+
/// Derives `std::ops::$t` for `$ty` calling `$wrapping` or `$checked` variants
1919
/// based on if debug_assertions enabled
2020
macro_rules! derive_arith {
21-
($ty:ty, $t:ident, $op:ident, $wrapping:ident, $checked:ident) => {
21+
($ty:ty, $t:ident, $t_assign:ident, $op:ident, $op_assign:ident, $wrapping:ident, $checked:ident) => {
2222
impl std::ops::$t for $ty {
2323
type Output = $ty;
2424

@@ -34,6 +34,20 @@ macro_rules! derive_arith {
3434
}
3535
}
3636

37+
impl std::ops::$t_assign for $ty {
38+
#[cfg(debug_assertions)]
39+
fn $op_assign(&mut self, rhs: Self) {
40+
*self = self
41+
.$checked(rhs)
42+
.expect(concat!(stringify!($ty), " overflow"));
43+
}
44+
45+
#[cfg(not(debug_assertions))]
46+
fn $op_assign(&mut self, rhs: Self) {
47+
*self = self.$wrapping(rhs);
48+
}
49+
}
50+
3751
impl<'a> std::ops::$t<$ty> for &'a $ty {
3852
type Output = $ty;
3953

arrow-buffer/src/bigint/mod.rs

+45-5
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,51 @@ fn mulx(a: u128, b: u128) -> (u128, u128) {
639639
(low, high)
640640
}
641641

642-
derive_arith!(i256, Add, add, wrapping_add, checked_add);
643-
derive_arith!(i256, Sub, sub, wrapping_sub, checked_sub);
644-
derive_arith!(i256, Mul, mul, wrapping_mul, checked_mul);
645-
derive_arith!(i256, Div, div, wrapping_div, checked_div);
646-
derive_arith!(i256, Rem, rem, wrapping_rem, checked_rem);
642+
derive_arith!(
643+
i256,
644+
Add,
645+
AddAssign,
646+
add,
647+
add_assign,
648+
wrapping_add,
649+
checked_add
650+
);
651+
derive_arith!(
652+
i256,
653+
Sub,
654+
SubAssign,
655+
sub,
656+
sub_assign,
657+
wrapping_sub,
658+
checked_sub
659+
);
660+
derive_arith!(
661+
i256,
662+
Mul,
663+
MulAssign,
664+
mul,
665+
mul_assign,
666+
wrapping_mul,
667+
checked_mul
668+
);
669+
derive_arith!(
670+
i256,
671+
Div,
672+
DivAssign,
673+
div,
674+
div_assign,
675+
wrapping_div,
676+
checked_div
677+
);
678+
derive_arith!(
679+
i256,
680+
Rem,
681+
RemAssign,
682+
rem,
683+
rem_assign,
684+
wrapping_rem,
685+
checked_rem
686+
);
647687

648688
impl Neg for i256 {
649689
type Output = i256;

arrow-buffer/src/interval.rs

+90-10
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,51 @@ impl Neg for IntervalMonthDayNano {
225225
}
226226
}
227227

228-
derive_arith!(IntervalMonthDayNano, Add, add, wrapping_add, checked_add);
229-
derive_arith!(IntervalMonthDayNano, Sub, sub, wrapping_sub, checked_sub);
230-
derive_arith!(IntervalMonthDayNano, Mul, mul, wrapping_mul, checked_mul);
231-
derive_arith!(IntervalMonthDayNano, Div, div, wrapping_div, checked_div);
232-
derive_arith!(IntervalMonthDayNano, Rem, rem, wrapping_rem, checked_rem);
228+
derive_arith!(
229+
IntervalMonthDayNano,
230+
Add,
231+
AddAssign,
232+
add,
233+
add_assign,
234+
wrapping_add,
235+
checked_add
236+
);
237+
derive_arith!(
238+
IntervalMonthDayNano,
239+
Sub,
240+
SubAssign,
241+
sub,
242+
sub_assign,
243+
wrapping_sub,
244+
checked_sub
245+
);
246+
derive_arith!(
247+
IntervalMonthDayNano,
248+
Mul,
249+
MulAssign,
250+
mul,
251+
mul_assign,
252+
wrapping_mul,
253+
checked_mul
254+
);
255+
derive_arith!(
256+
IntervalMonthDayNano,
257+
Div,
258+
DivAssign,
259+
div,
260+
div_assign,
261+
wrapping_div,
262+
checked_div
263+
);
264+
derive_arith!(
265+
IntervalMonthDayNano,
266+
Rem,
267+
RemAssign,
268+
rem,
269+
rem_assign,
270+
wrapping_rem,
271+
checked_rem
272+
);
233273

234274
/// Value of an IntervalDayTime array
235275
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
@@ -417,8 +457,48 @@ impl Neg for IntervalDayTime {
417457
}
418458
}
419459

420-
derive_arith!(IntervalDayTime, Add, add, wrapping_add, checked_add);
421-
derive_arith!(IntervalDayTime, Sub, sub, wrapping_sub, checked_sub);
422-
derive_arith!(IntervalDayTime, Mul, mul, wrapping_mul, checked_mul);
423-
derive_arith!(IntervalDayTime, Div, div, wrapping_div, checked_div);
424-
derive_arith!(IntervalDayTime, Rem, rem, wrapping_rem, checked_rem);
460+
derive_arith!(
461+
IntervalDayTime,
462+
Add,
463+
AddAssign,
464+
add,
465+
add_assign,
466+
wrapping_add,
467+
checked_add
468+
);
469+
derive_arith!(
470+
IntervalDayTime,
471+
Sub,
472+
SubAssign,
473+
sub,
474+
sub_assign,
475+
wrapping_sub,
476+
checked_sub
477+
);
478+
derive_arith!(
479+
IntervalDayTime,
480+
Mul,
481+
MulAssign,
482+
mul,
483+
mul_assign,
484+
wrapping_mul,
485+
checked_mul
486+
);
487+
derive_arith!(
488+
IntervalDayTime,
489+
Div,
490+
DivAssign,
491+
div,
492+
div_assign,
493+
wrapping_div,
494+
checked_div
495+
);
496+
derive_arith!(
497+
IntervalDayTime,
498+
Rem,
499+
RemAssign,
500+
rem,
501+
rem_assign,
502+
wrapping_rem,
503+
checked_rem
504+
);

0 commit comments

Comments
 (0)