From bbfada5c0f594d3d5f0c0ab9edd6f7ddc441de01 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 7 Apr 2020 13:46:51 -0400 Subject: [PATCH] Add convenience macro for BigInt --- src/bigint.rs | 22 ++++++++++++++++++++++ src/num.rs | 2 -- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/bigint.rs b/src/bigint.rs index 0e4341d..5a457d1 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -15,6 +15,17 @@ pub struct BigInt { sign: Sign, } +/// Create a [BigInt](bigint/struct.BigInt.html) type with signed or unsigned number literals +#[macro_export] +macro_rules! big_int { + ($w:literal) => { + $crate::bigint::BigInt::from($w) + }; + (-$x:literal) => { + $crate::bigint::BigInt::from(-$x) + }; +} + impl Default for BigInt { fn default() -> Self { Self { @@ -439,6 +450,17 @@ mod tests { assert_eq!(int.inner[0], 45usize) } + #[test] + fn test_macro() { + let a = big_int!(75); + let b = BigInt::from(75); + assert_eq!(a, b); + + let a = big_int!(-75); + let b = BigInt::from(-75); + assert_eq!(a, b); + } + #[test] fn test_trim_zeros() { let mut lotsoftrailing = BigInt { diff --git a/src/num.rs b/src/num.rs index 8764ad5..c080e89 100644 --- a/src/num.rs +++ b/src/num.rs @@ -160,7 +160,6 @@ macro_rules! impl_int { self == 0 } - #[cfg_attr(tarpaulin, skip)] fn max_value() -> $type { <$type>::max_value() } @@ -253,7 +252,6 @@ macro_rules! impl_unsigned { let (x, y) = (min(x, y), max(x, y)); Self::stein_gcd((y - x) >> 1, x) } - #[cfg_attr(tarpaulin, skip)] _ => unreachable!(), } }