| 1 | use superrust-analyzer cargo dashu-base 0.4.1 ring/::{DivEuclidrust-analyzer cargo dashu-base 0.4.1 ring/DivEuclid#, DivRemrust-analyzer cargo dashu-base 0.4.1 ring/DivRem#, DivRemAssignrust-analyzer cargo dashu-base 0.4.1 ring/DivRemAssign#, DivRemEuclidrust-analyzer cargo dashu-base 0.4.1 ring/DivRemEuclid#, RemEuclidrust-analyzer cargo dashu-base 0.4.1 ring/RemEuclid#}; |
| 2 | |
| 3 | macro_rules! impl_div_rem_ops_primrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl_div_rem_ops_prim! { |
| 4 | ($($T:ty)*) => {$( |
| 5 | impl DivRem for $T { |
| 6 | type OutputDiv = $T; |
| 7 | type OutputRem = $T; |
| 8 | #[inline] |
| 9 | fn div_rem(self, rhs: $T) -> ($T, $T) { |
| 10 | (self / rhs, self % rhs) |
| 11 | } |
| 12 | } |
| 13 | impl DivRemAssign for $T { |
| 14 | type OutputRem = $T; |
| 15 | #[inline] |
| 16 | fn div_rem_assign(&mut self, rhs: $T) -> $T { |
| 17 | let r = *self % rhs; |
| 18 | *self /= rhs; |
| 19 | r |
| 20 | } |
| 21 | } |
| 22 | impl DivEuclid for $T { |
| 23 | type Output = $T; |
| 24 | #[inline] |
| 25 | fn div_euclid(self, rhs: $T) -> $T { |
| 26 | <$T>::div_euclid(self, rhs) |
| 27 | } |
| 28 | } |
| 29 | impl RemEuclid for $T { |
| 30 | type Output = $T; |
| 31 | #[inline] |
| 32 | fn rem_euclid(self, rhs: $T) -> $T { |
| 33 | <$T>::rem_euclid(self, rhs) |
| 34 | } |
| 35 | } |
| 36 | impl DivRemEuclid for $T { |
| 37 | type OutputDiv = $T; |
| 38 | type OutputRem = $T; |
| 39 | #[inline] |
| 40 | fn div_rem_euclid(self, rhs: $T) -> ($T, $T) { |
| 41 | let (q, r) = (self / rhs, self % rhs); |
| 42 | |
| 43 | // depending on compiler to simplify the case for unsinged integers |
| 44 | #[allow(unused_comparisons)] |
| 45 | if r >= 0 { |
| 46 | (q, r) |
| 47 | } else if rhs >= 0{ |
| 48 | (q - 1, r + rhs) |
| 49 | } else { |
| 50 | (q + 1, r - rhs) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | )*} |
| 55 | } |
| 56 | impl_div_rem_ops_primrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl_div_rem_ops_prim!!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize); |
| 57 | |
| 58 | #[cfg(test)] |
| 59 | mod testsrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/tests/ { |
| 60 | use superrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/::*; |
| 61 | |
| 62 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 63 | fn test_simplerust-analyzer cargo dashu-base 0.4.1 ring/div_rem/tests/test_simple().() { |
| 64 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(7u32.div_remrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[u32][`DivRem<Self>`]div_rem().(4), (1, 3)); |
| 65 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(7u32.div_rem_euclidrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[u32][`DivRemEuclid<Self>`]div_rem_euclid().(4), (1, 3)); |
| 66 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(7i32.div_remrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[i32][`DivRem<Self>`]div_rem().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().4), (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1, 3)); |
| 67 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(7i32.div_rem_euclidrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[i32][`DivRemEuclid<Self>`]div_rem_euclid().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().4), (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1, 3)); |
| 68 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().7i32).div_remrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[i32][`DivRem<Self>`]div_rem().(4), (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().3)); |
| 69 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().7i32).div_rem_euclidrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[i32][`DivRemEuclid<Self>`]div_rem_euclid().(4), (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().2, 1)); |
| 70 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().7i32).div_remrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[i32][`DivRem<Self>`]div_rem().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().4), (1, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().3)); |
| 71 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().7i32).div_rem_euclidrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[i32][`DivRemEuclid<Self>`]div_rem_euclid().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().4), (2, 1)); |
| 72 | |
| 73 | let mut n = 7u32; |
| 74 | let r = n.div_rem_assignrust-analyzer cargo dashu-base 0.4.1 ring/div_rem/impl#[u32][`DivRemAssign<Self>`]div_rem_assign().(4); |
| 75 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(n == 1 && r == 3); |
| 76 | } |
| 77 | } |