Skip to content

Commit 656fd4f

Browse files
committed
impl {Add,Sub,Mul,Div}<&BigRational> for primitives
Since `BigRational`s/`BigInt`s don't implement `Copy`, seems like it'd be nice to be able to pass a reference on `100 / &some_big_rational`.
1 parent 7483ae8 commit 656fd4f

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

src/lib.rs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,16 @@ macro_rules! impl_primitive_ops_ratio {
731731
}
732732
}
733733

734+
#[cfg(feature = "bigint")]
735+
impl<'a> Add<&'a BigRational> for $primitive {
736+
type Output = BigRational;
737+
738+
#[inline]
739+
fn add(self, rhs: &'a BigRational) -> BigRational {
740+
Ratio::new(rhs.numer.clone() + BigInt::from(self), rhs.denom.clone())
741+
}
742+
}
743+
734744
impl<T> Sub<Ratio<T>> for $primitive
735745
where
736746
T: Clone + Integer + ::core::convert::From<$primitive>,
@@ -743,6 +753,16 @@ macro_rules! impl_primitive_ops_ratio {
743753
}
744754
}
745755

756+
#[cfg(feature = "bigint")]
757+
impl<'a> Sub<&'a BigRational> for $primitive {
758+
type Output = BigRational;
759+
760+
#[inline]
761+
fn sub(self, rhs: &'a BigRational) -> BigRational {
762+
Ratio::new(BigInt::from(self) - rhs.numer.clone(), rhs.denom.clone())
763+
}
764+
}
765+
746766
impl<T> Mul<Ratio<T>> for $primitive
747767
where
748768
T: Clone + Integer + ::core::convert::From<$primitive>,
@@ -755,6 +775,16 @@ macro_rules! impl_primitive_ops_ratio {
755775
}
756776
}
757777

778+
#[cfg(feature = "bigint")]
779+
impl<'a> Mul<&'a BigRational> for $primitive {
780+
type Output = BigRational;
781+
782+
#[inline]
783+
fn mul(self, rhs: &'a BigRational) -> BigRational {
784+
Ratio::new(rhs.numer.clone() * BigInt::from(self), rhs.denom.clone())
785+
}
786+
}
787+
758788
impl<T> Div<Ratio<T>> for $primitive
759789
where
760790
T: Clone + Integer + ::core::convert::From<$primitive>,
@@ -766,6 +796,16 @@ macro_rules! impl_primitive_ops_ratio {
766796
Ratio::new(rhs.numer, rhs.denom * T::from(self))
767797
}
768798
}
799+
800+
#[cfg(feature = "bigint")]
801+
impl<'a> Div<&'a BigRational> for $primitive {
802+
type Output = BigRational;
803+
804+
#[inline]
805+
fn div(self, rhs: &'a BigRational) -> BigRational {
806+
Ratio::new(rhs.numer.clone(), rhs.denom.clone() * BigInt::from(self))
807+
}
808+
}
769809
};
770810
}
771811

@@ -1686,12 +1726,12 @@ mod test {
16861726
}
16871727

16881728
mod arith {
1689-
#[cfg(feature = "bigint")]
1690-
use bigint::BigInt;
16911729
#[cfg(feature = "bigint")]
16921730
use super::super::BigRational;
16931731
use super::super::{Ratio, Rational, Rational32, Rational64};
16941732
use super::{_0, _1, _1_2, _2, _3_2, _NEG1_2, to_big};
1733+
#[cfg(feature = "bigint")]
1734+
use bigint::BigInt;
16951735
use traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub};
16961736

16971737
const _1I32: Rational32 = Ratio { numer: 1, denom: 1 };
@@ -1849,6 +1889,9 @@ mod test {
18491889
fn test_isize(a: isize, b: BigRational, c: BigRational) {
18501890
assert_eq!(a + b, c);
18511891
}
1892+
fn test_isize_ref(a: isize, b: &BigRational, c: BigRational) {
1893+
assert_eq!(a + b, c);
1894+
}
18521895
fn test_usize(a: usize, b: BigRational, c: BigRational) {
18531896
assert_eq!(a + b, c);
18541897
}
@@ -1887,8 +1930,12 @@ mod test {
18871930
fn test_bigint(a: BigInt, b: BigRational, c: BigRational) {
18881931
assert_eq!(a + b, c);
18891932
}
1933+
fn test_bigint_ref(a: BigInt, b: &BigRational, c: BigRational) {
1934+
assert_eq!(a + b, c);
1935+
}
18901936

18911937
test_isize(-2, to_big(_1), to_big(_NEG1));
1938+
test_isize_ref(-2, &to_big(_1), to_big(_NEG1));
18921939
test_usize(1, to_big(_1), to_big(_2));
18931940
test_i8(-2, to_big(_1), to_big(_NEG1));
18941941
test_u8(1, to_big(_1), to_big(_2));
@@ -1903,6 +1950,7 @@ mod test {
19031950
#[cfg(has_i128)]
19041951
test_u128(1, to_big(_1), to_big(_2));
19051952
test_bigint(BigInt::from(1), to_big(_1), to_big(_2));
1953+
test_bigint_ref(BigInt::from(1), &to_big(_1), to_big(_2));
19061954
}
19071955

19081956
#[cfg(feature = "bigint")]
@@ -2088,6 +2136,9 @@ mod test {
20882136
fn test_isize(a: isize, b: BigRational, c: BigRational) {
20892137
assert_eq!(a - b, c);
20902138
}
2139+
fn test_isize_ref(a: isize, b: &BigRational, c: BigRational) {
2140+
assert_eq!(a - b, c);
2141+
}
20912142
fn test_usize(a: usize, b: BigRational, c: BigRational) {
20922143
assert_eq!(a - b, c);
20932144
}
@@ -2126,8 +2177,12 @@ mod test {
21262177
fn test_bigint(a: BigInt, b: BigRational, c: BigRational) {
21272178
assert_eq!(a - b, c);
21282179
}
2180+
fn test_bigint_ref(a: BigInt, b: &BigRational, c: BigRational) {
2181+
assert_eq!(a - b, c);
2182+
}
21292183

21302184
test_isize(-1, to_big(_1), to_big(_NEG2));
2185+
test_isize_ref(-1, &to_big(_1), to_big(_NEG2));
21312186
test_usize(2, to_big(_1), to_big(_1));
21322187
test_i8(-1, to_big(_1), to_big(_NEG2));
21332188
test_u8(2, to_big(_1), to_big(_1));
@@ -2142,6 +2197,7 @@ mod test {
21422197
#[cfg(has_i128)]
21432198
test_u128(2, to_big(_1), to_big(_1));
21442199
test_bigint(BigInt::from(2), to_big(_1), to_big(_1));
2200+
test_bigint_ref(BigInt::from(2), &to_big(_1), to_big(_1));
21452201
}
21462202

21472203
#[cfg(feature = "bigint")]
@@ -2327,6 +2383,9 @@ mod test {
23272383
fn test_isize(a: isize, b: BigRational, c: BigRational) {
23282384
assert_eq!(a * b, c);
23292385
}
2386+
fn test_isize_ref(a: isize, b: &BigRational, c: BigRational) {
2387+
assert_eq!(a * b, c);
2388+
}
23302389
fn test_usize(a: usize, b: BigRational, c: BigRational) {
23312390
assert_eq!(a * b, c);
23322391
}
@@ -2365,8 +2424,12 @@ mod test {
23652424
fn test_bigint(a: BigInt, b: BigRational, c: BigRational) {
23662425
assert_eq!(a * b, c);
23672426
}
2427+
fn test_bigint_ref(a: BigInt, b: &BigRational, c: BigRational) {
2428+
assert_eq!(a * b, c);
2429+
}
23682430

23692431
test_isize(-2, to_big(_1_2), to_big(_NEG1));
2432+
test_isize_ref(-2, &to_big(_1_2), to_big(_NEG1));
23702433
test_usize(2, to_big(_1_2), to_big(_1));
23712434
test_i8(-2, to_big(_1_2), to_big(_NEG1));
23722435
test_u8(2, to_big(_1_2), to_big(_1));
@@ -2381,6 +2444,7 @@ mod test {
23812444
#[cfg(has_i128)]
23822445
test_u128(2, to_big(_1_2), to_big(_1));
23832446
test_bigint(BigInt::from(2), to_big(_1_2), to_big(_1));
2447+
test_bigint_ref(BigInt::from(2), &to_big(_1_2), to_big(_1));
23842448
}
23852449

23862450
#[cfg(feature = "bigint")]
@@ -2566,6 +2630,9 @@ mod test {
25662630
fn test_isize(a: isize, b: BigRational, c: BigRational) {
25672631
assert_eq!(a / b, c);
25682632
}
2633+
fn test_isize_ref(a: isize, b: &BigRational, c: BigRational) {
2634+
assert_eq!(a / b, c);
2635+
}
25692636
fn test_usize(a: usize, b: BigRational, c: BigRational) {
25702637
assert_eq!(a / b, c);
25712638
}
@@ -2604,8 +2671,12 @@ mod test {
26042671
fn test_bigint(a: BigInt, b: BigRational, c: BigRational) {
26052672
assert_eq!(a / b, c);
26062673
}
2674+
fn test_bigint_ref(a: BigInt, b: &BigRational, c: BigRational) {
2675+
assert_eq!(a / b, c);
2676+
}
26072677

26082678
test_isize(-2, to_big(_2), to_big(_NEG1));
2679+
test_isize_ref(-2, &to_big(_2), to_big(_NEG1));
26092680
test_usize(2, to_big(_2), to_big(_1));
26102681
test_i8(-2, to_big(_2), to_big(_NEG1));
26112682
test_u8(2, to_big(_2), to_big(_1));
@@ -2620,6 +2691,7 @@ mod test {
26202691
#[cfg(has_i128)]
26212692
test_u128(2, to_big(_2), to_big(_1));
26222693
test_bigint(BigInt::from(2), to_big(_2), to_big(_1));
2694+
test_bigint_ref(BigInt::from(2), &to_big(_2), to_big(_1));
26232695
}
26242696

26252697
#[cfg(feature = "bigint")]

0 commit comments

Comments
 (0)