@@ -2,7 +2,7 @@ use core::{
22 error:: Error ,
33 fmt:: { self , Display } ,
44 num:: TryFromIntError ,
5- ops:: { BitAnd , BitAndAssign , BitOr , BitOrAssign , BitXor , BitXorAssign , Neg , Not } ,
5+ ops:: { Add , BitAnd , BitAndAssign , BitOr , BitOrAssign , BitXor , BitXorAssign , Neg , Not , Sub } ,
66} ;
77
88use hashql_core:: value:: { Integer , Primitive } ;
@@ -642,6 +642,58 @@ impl Neg for Int {
642642 }
643643}
644644
645+ impl Add for Int {
646+ type Output = Numeric ;
647+
648+ #[ inline]
649+ #[ expect( clippy:: float_arithmetic) ]
650+ fn add ( self , rhs : Self ) -> Self :: Output {
651+ let ( value, overflow) = self . as_int ( ) . overflowing_add ( rhs. as_int ( ) ) ;
652+
653+ if overflow {
654+ Numeric :: Num ( Num :: from ( self . as_f64 ( ) + rhs. as_f64 ( ) ) )
655+ } else {
656+ Numeric :: Int ( Self :: from_value_unchecked ( value) )
657+ }
658+ }
659+ }
660+
661+ impl Add < Num > for Int {
662+ type Output = Num ;
663+
664+ #[ inline]
665+ #[ expect( clippy:: float_arithmetic) ]
666+ fn add ( self , rhs : Num ) -> Self :: Output {
667+ Num :: from ( self . as_f64 ( ) + rhs. as_f64 ( ) )
668+ }
669+ }
670+
671+ impl Sub for Int {
672+ type Output = Numeric ;
673+
674+ #[ inline]
675+ #[ expect( clippy:: float_arithmetic) ]
676+ fn sub ( self , rhs : Self ) -> Self :: Output {
677+ let ( value, overflow) = self . as_int ( ) . overflowing_sub ( rhs. as_int ( ) ) ;
678+
679+ if overflow {
680+ Numeric :: Num ( Num :: from ( self . as_f64 ( ) - rhs. as_f64 ( ) ) )
681+ } else {
682+ Numeric :: Int ( Self :: from_value_unchecked ( value) )
683+ }
684+ }
685+ }
686+
687+ impl Sub < Num > for Int {
688+ type Output = Num ;
689+
690+ #[ inline]
691+ #[ expect( clippy:: float_arithmetic) ]
692+ fn sub ( self , rhs : Num ) -> Self :: Output {
693+ Num :: from ( self . as_f64 ( ) - rhs. as_f64 ( ) )
694+ }
695+ }
696+
645697impl BitOr for Int {
646698 type Output = Self ;
647699
@@ -692,6 +744,10 @@ impl BitXorAssign for Int {
692744
693745forward_ref_unop ! ( impl Not :: not for Int ) ;
694746forward_ref_unop ! ( impl Neg :: neg for Int ) ;
747+ forward_ref_binop ! ( impl Add <Int >:: add for Int ) ;
748+ forward_ref_binop ! ( impl Add <Num >:: add for Int ) ;
749+ forward_ref_binop ! ( impl Sub <Int >:: sub for Int ) ;
750+ forward_ref_binop ! ( impl Sub <Num >:: sub for Int ) ;
695751forward_ref_binop ! ( impl BitOr <Int >:: bitor for Int ) ;
696752forward_ref_binop ! ( impl BitAnd <Int >:: bitand for Int ) ;
697753forward_ref_binop ! ( impl BitXor <Int >:: bitxor for Int ) ;
@@ -707,7 +763,7 @@ mod tests {
707763 clippy:: float_cmp
708764 ) ]
709765
710- use crate :: { body :: constant :: Int , interpret :: value :: Numeric } ;
766+ use crate :: interpret :: value :: { Int , Numeric } ;
711767
712768 #[ test]
713769 fn neg_positive ( ) {
0 commit comments