multiversx_sc/types/managed/basic/
big_float_operators.rs1use super::BigFloat;
2use crate::{
3 api::{BigFloatApiImpl, ManagedTypeApi},
4 types::managed::managed_type_trait::ManagedType,
5};
6use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
7
8macro_rules! binary_operator {
9 ($trait:ident, $method:ident, $api_func:ident) => {
10 impl<M: ManagedTypeApi> $trait for BigFloat<M> {
11 type Output = BigFloat<M>;
12
13 fn $method(self, other: BigFloat<M>) -> BigFloat<M> {
14 M::managed_type_impl().$api_func(
15 self.handle.clone(),
16 self.handle.clone(),
17 other.handle.clone(),
18 );
19 self
20 }
21 }
22
23 impl<'a, 'b, M: ManagedTypeApi> $trait<&'b BigFloat<M>> for &'a BigFloat<M> {
24 type Output = BigFloat<M>;
25
26 fn $method(self, other: &BigFloat<M>) -> BigFloat<M> {
27 unsafe {
28 let result = BigFloat::new_uninit();
29 M::managed_type_impl().$api_func(
30 result.get_handle(),
31 self.handle.clone(),
32 other.handle.clone(),
33 );
34 result
35 }
36 }
37 }
38 };
39}
40
41binary_operator! {Add, add, bf_add}
42binary_operator! {Sub, sub, bf_sub}
43binary_operator! {Mul, mul, bf_mul}
44binary_operator! {Div, div, bf_div}
45
46macro_rules! binary_assign_operator {
47 ($trait:ident, $method:ident, $api_func:ident) => {
48 impl<M: ManagedTypeApi> $trait<BigFloat<M>> for BigFloat<M> {
49 #[inline]
50 fn $method(&mut self, other: Self) {
51 let api = M::managed_type_impl();
52 api.$api_func(
53 self.handle.clone(),
54 self.handle.clone(),
55 other.handle.clone(),
56 );
57 }
58 }
59
60 impl<M: ManagedTypeApi> $trait<&BigFloat<M>> for BigFloat<M> {
61 #[inline]
62 fn $method(&mut self, other: &BigFloat<M>) {
63 let api = M::managed_type_impl();
64 api.$api_func(
65 self.handle.clone(),
66 self.handle.clone(),
67 other.handle.clone(),
68 );
69 }
70 }
71 };
72}
73
74binary_assign_operator! {AddAssign, add_assign, bf_add}
75binary_assign_operator! {SubAssign, sub_assign, bf_sub}
76binary_assign_operator! {MulAssign, mul_assign, bf_mul}
77binary_assign_operator! {DivAssign, div_assign, bf_div}
78
79impl<M: ManagedTypeApi> Neg for BigFloat<M> {
80 type Output = BigFloat<M>;
81
82 fn neg(self) -> Self::Output {
83 unsafe {
84 let result = BigFloat::new_uninit();
85 M::managed_type_impl().bf_neg(result.get_handle(), self.handle.clone());
86 result
87 }
88 }
89}