Skip to content

Commit fe7d5c0

Browse files
committed
impl {Add,Sub,Mul,Div} for &BigRational
Similar to the previous commit, this extends behavior of letting Ratios operate on primitives (and BigInt). In the case of `BigRational`, I imagine references to them will be more prevalent than the other types, hence this commit.
1 parent 656fd4f commit fe7d5c0

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/lib.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,15 @@ macro_rules! impl_bigint_ops_primitive {
852852
}
853853
}
854854

855+
impl<'a> Add<$primitive> for &'a BigRational {
856+
type Output = BigRational;
857+
858+
#[inline]
859+
fn add(self, rhs: $primitive) -> BigRational {
860+
Ratio::new(self.numer.clone() + BigInt::from(rhs), self.denom.clone())
861+
}
862+
}
863+
855864
impl Sub<$primitive> for BigRational {
856865
type Output = BigRational;
857866

@@ -861,6 +870,15 @@ macro_rules! impl_bigint_ops_primitive {
861870
}
862871
}
863872

873+
impl<'a> Sub<$primitive> for &'a BigRational {
874+
type Output = BigRational;
875+
876+
#[inline]
877+
fn sub(self, rhs: $primitive) -> BigRational {
878+
Ratio::new(self.numer.clone() - BigInt::from(rhs), self.denom.clone())
879+
}
880+
}
881+
864882
impl Mul<$primitive> for BigRational {
865883
type Output = BigRational;
866884

@@ -870,6 +888,15 @@ macro_rules! impl_bigint_ops_primitive {
870888
}
871889
}
872890

891+
impl<'a> Mul<$primitive> for &'a BigRational {
892+
type Output = BigRational;
893+
894+
#[inline]
895+
fn mul(self, rhs: $primitive) -> BigRational {
896+
Ratio::new(self.numer.clone() * BigInt::from(rhs), self.denom.clone())
897+
}
898+
}
899+
873900
impl Div<$primitive> for BigRational {
874901
type Output = BigRational;
875902

@@ -878,6 +905,15 @@ macro_rules! impl_bigint_ops_primitive {
878905
Ratio::new(self.numer, self.denom * BigInt::from(rhs))
879906
}
880907
}
908+
909+
impl<'a> Div<$primitive> for &'a BigRational {
910+
type Output = BigRational;
911+
912+
#[inline]
913+
fn div(self, rhs: $primitive) -> BigRational {
914+
Ratio::new(self.numer.clone(), self.denom.clone() * BigInt::from(rhs))
915+
}
916+
}
881917
};
882918
}
883919

@@ -1959,6 +1995,9 @@ mod test {
19591995
fn test_isize(a: BigRational, b: isize, c: BigRational) {
19601996
assert_eq!(a + b, c);
19611997
}
1998+
fn test_isize_ref(a: &BigRational, b: isize, c: BigRational) {
1999+
assert_eq!(a + b, c);
2000+
}
19622001
fn test_usize(a: BigRational, b: usize, c: BigRational) {
19632002
assert_eq!(a + b, c);
19642003
}
@@ -1997,8 +2036,12 @@ mod test {
19972036
fn test_bigint(a: BigRational, b: BigInt, c: BigRational) {
19982037
assert_eq!(a + b, c);
19992038
}
2039+
fn test_bigint_ref(a: &BigRational, b: BigInt, c: BigRational) {
2040+
assert_eq!(a + b, c);
2041+
}
20002042

20012043
test_isize(to_big(_1), -2, to_big(_NEG1));
2044+
test_isize_ref(&to_big(_1), -2, to_big(_NEG1));
20022045
test_usize(to_big(_1), 1, to_big(_2));
20032046
test_i8(to_big(_1), -2, to_big(_NEG1));
20042047
test_u8(to_big(_1), 1, to_big(_2));
@@ -2013,6 +2056,7 @@ mod test {
20132056
#[cfg(has_i128)]
20142057
test_u128(to_big(_1), 1, to_big(_2));
20152058
test_bigint(to_big(_1), BigInt::from(1), to_big(_2));
2059+
test_bigint_ref(&to_big(_1), BigInt::from(1), to_big(_2));
20162060
}
20172061

20182062
#[test]
@@ -2206,6 +2250,9 @@ mod test {
22062250
fn test_isize(a: BigRational, b: isize, c: BigRational) {
22072251
assert_eq!(a - b, c);
22082252
}
2253+
fn test_isize_ref(a: &BigRational, b: isize, c: BigRational) {
2254+
assert_eq!(a - b, c);
2255+
}
22092256
fn test_usize(a: BigRational, b: usize, c: BigRational) {
22102257
assert_eq!(a - b, c);
22112258
}
@@ -2244,8 +2291,12 @@ mod test {
22442291
fn test_bigint(a: BigRational, b: BigInt, c: BigRational) {
22452292
assert_eq!(a - b, c);
22462293
}
2294+
fn test_bigint_ref(a: &BigRational, b: BigInt, c: BigRational) {
2295+
assert_eq!(a - b, c);
2296+
}
22472297

22482298
test_isize(to_big(_2), 1, to_big(_1));
2299+
test_isize_ref(&to_big(_2), 1, to_big(_1));
22492300
test_usize(to_big(_2), 1, to_big(_1));
22502301
test_i8(to_big(_2), 1, to_big(_1));
22512302
test_u8(to_big(_2), 1, to_big(_1));
@@ -2260,6 +2311,7 @@ mod test {
22602311
#[cfg(has_i128)]
22612312
test_u128(to_big(_2), 1, to_big(_1));
22622313
test_bigint(to_big(_2), BigInt::from(1), to_big(_1));
2314+
test_bigint_ref(&to_big(_2), BigInt::from(1), to_big(_1));
22632315
}
22642316

22652317
#[test]
@@ -2453,6 +2505,9 @@ mod test {
24532505
fn test_isize(a: BigRational, b: isize, c: BigRational) {
24542506
assert_eq!(a * b, c);
24552507
}
2508+
fn test_isize_ref(a: &BigRational, b: isize, c: BigRational) {
2509+
assert_eq!(a * b, c);
2510+
}
24562511
fn test_usize(a: BigRational, b: usize, c: BigRational) {
24572512
assert_eq!(a * b, c);
24582513
}
@@ -2491,8 +2546,12 @@ mod test {
24912546
fn test_bigint(a: BigRational, b: BigInt, c: BigRational) {
24922547
assert_eq!(a * b, c);
24932548
}
2549+
fn test_bigint_ref(a: &BigRational, b: BigInt, c: BigRational) {
2550+
assert_eq!(a * b, c);
2551+
}
24942552

24952553
test_isize(to_big(_1_2), -2, to_big(_NEG1));
2554+
test_isize_ref(&to_big(_1_2), -2, to_big(_NEG1));
24962555
test_usize(to_big(_1_2), 2, to_big(_1));
24972556
test_i8(to_big(_1_2), -2, to_big(_NEG1));
24982557
test_u8(to_big(_1_2), 2, to_big(_1));
@@ -2507,6 +2566,7 @@ mod test {
25072566
#[cfg(has_i128)]
25082567
test_u128(to_big(_1_2), 2, to_big(_1));
25092568
test_bigint(to_big(_1_2), BigInt::from(2), to_big(_1));
2569+
test_bigint_ref(&to_big(_1_2), BigInt::from(2), to_big(_1));
25102570
}
25112571

25122572
#[test]
@@ -2700,6 +2760,9 @@ mod test {
27002760
fn test_isize(a: BigRational, b: isize, c: BigRational) {
27012761
assert_eq!(a / b, c);
27022762
}
2763+
fn test_isize_ref(a: &BigRational, b: isize, c: BigRational) {
2764+
assert_eq!(a / b, c);
2765+
}
27032766
fn test_usize(a: BigRational, b: usize, c: BigRational) {
27042767
assert_eq!(a / b, c);
27052768
}
@@ -2738,8 +2801,12 @@ mod test {
27382801
fn test_bigint(a: BigRational, b: BigInt, c: BigRational) {
27392802
assert_eq!(a / b, c);
27402803
}
2804+
fn test_bigint_ref(a: &BigRational, b: BigInt, c: BigRational) {
2805+
assert_eq!(a / b, c);
2806+
}
27412807

27422808
test_isize(to_big(_2), -2isize, to_big(_NEG1));
2809+
test_isize_ref(&to_big(_2), -2isize, to_big(_NEG1));
27432810
test_usize(to_big(_2), 2usize, to_big(_1));
27442811
test_i8(to_big(_2), -2i8, to_big(_NEG1));
27452812
test_u8(to_big(_2), 2u8, to_big(_1));
@@ -2754,6 +2821,7 @@ mod test {
27542821
#[cfg(has_i128)]
27552822
test_u128(to_big(_2), 2, to_big(_1));
27562823
test_bigint(to_big(_2), BigInt::from(2), to_big(_1));
2824+
test_bigint_ref(&to_big(_2), BigInt::from(2), to_big(_1));
27572825
}
27582826

27592827
#[test]

0 commit comments

Comments
 (0)