From 1fb03ca18aa2ef6a9cc7bb706cd0fc604fcf270c Mon Sep 17 00:00:00 2001 From: Sam Cappleman-Lynes Date: Fri, 30 Jun 2017 00:39:37 +0100 Subject: [PATCH] Make new code work on rustc-1.8.0 - Don't apply attributes to statements (1.12.0) - Don't use checked_abs (1.13.0) --- bigint/src/bigint.rs | 17 +++++++++++------ bigint/src/biguint.rs | 2 ++ bigint/src/lib.rs | 10 ++++++++++ bigint/src/macros.rs | 40 ++++------------------------------------ 4 files changed, 27 insertions(+), 42 deletions(-) diff --git a/bigint/src/bigint.rs b/bigint/src/bigint.rs index dfb69d4..ece48b2 100644 --- a/bigint/src/bigint.rs +++ b/bigint/src/bigint.rs @@ -28,6 +28,9 @@ use biguint; use biguint::to_str_radix_reversed; use biguint::BigUint; +use UsizePromotion; +use IsizePromotion; + #[cfg(test)] #[path = "tests/bigint.rs"] mod bigint_tests; @@ -301,17 +304,19 @@ impl Signed for BigInt { // A convenience method for getting the absolute value of an i32 in a u32. fn i32_abs_as_u32(a: i32) -> u32 { - match a.checked_abs() { - Some(x) => x as u32, - None => a as u32 + if a == i32::min_value() { + a as u32 + } else { + a.abs() as u32 } } // A convenience method for getting the absolute value of an i64 in a u64. fn i64_abs_as_u64(a: i64) -> u64 { - match a.checked_abs() { - Some(x) => x as u64, - None => a as u64 + if a == i64::min_value() { + a as u64 + } else { + a.abs() as u64 } } diff --git a/bigint/src/biguint.rs b/bigint/src/biguint.rs index 7af2e71..38f7f29 100644 --- a/bigint/src/biguint.rs +++ b/bigint/src/biguint.rs @@ -27,6 +27,8 @@ use self::algorithms::{__add2, add2, sub2, sub2rev}; use self::algorithms::{biguint_shl, biguint_shr}; use self::algorithms::{cmp_slice, fls, ilog2}; +use UsizePromotion; + use ParseBigIntError; #[cfg(test)] diff --git a/bigint/src/lib.rs b/bigint/src/lib.rs index 586eeec..02b9992 100644 --- a/bigint/src/lib.rs +++ b/bigint/src/lib.rs @@ -88,6 +88,16 @@ use std::error::Error; use std::num::ParseIntError; use std::fmt; +#[cfg(target_pointer_width = "32")] +type UsizePromotion = u32; +#[cfg(target_pointer_width = "64")] +type UsizePromotion = u64; + +#[cfg(target_pointer_width = "32")] +type IsizePromotion = i32; +#[cfg(target_pointer_width = "64")] +type IsizePromotion = i64; + #[derive(Debug, PartialEq)] pub enum ParseBigIntError { ParseInt(ParseIntError), diff --git a/bigint/src/macros.rs b/bigint/src/macros.rs index a8143ad..7705f55 100644 --- a/bigint/src/macros.rs +++ b/bigint/src/macros.rs @@ -210,35 +210,17 @@ macro_rules! promote_scalars { } } -macro_rules! promote_unsigned_scalars_to_u32 { +macro_rules! promote_unsigned_scalars { (impl $imp:ident for $res:ty, $method:ident) => { - #[cfg(target_pointer_width = "32")] - promote_scalars!(impl $imp for $res, $method, u8, u16, usize); - #[cfg(target_pointer_width = "64")] promote_scalars!(impl $imp for $res, $method, u8, u16); + promote_scalars!(impl $imp for $res, $method, usize); } } -macro_rules! promote_unsigned_scalars_to_u64 { +macro_rules! promote_signed_scalars { (impl $imp:ident for $res:ty, $method:ident) => { - #[cfg(target_pointer_width = "64")] - promote_scalars!(impl $imp for $res, $method, usize); - } -} - -macro_rules! promote_signed_scalars_to_i32 { - (impl $imp:ident for $res:ty, $method:ident) => { - #[cfg(target_pointer_width = "32")] - promote_scalars!(impl $imp for $res, $method, i8, i16, isize); - #[cfg(target_pointer_width = "64")] promote_scalars!(impl $imp for $res, $method, i8, i16); - } -} - -macro_rules! promote_signed_scalars_to_i64 { - (impl $imp:ident for $res:ty, $method:ident) => { - #[cfg(target_pointer_width = "64")] - promote_scalars!(impl $imp for $res, $method, isize); + promote_scalars!(impl $imp for $res, $method, isize); } } @@ -284,20 +266,6 @@ macro_rules! forward_all_scalar_binop_to_val_val_commutative { } } -macro_rules! promote_unsigned_scalars { - (impl $imp:ident for $res:ty, $method:ident) => { - promote_unsigned_scalars_to_u32!(impl $imp for $res, $method); - promote_unsigned_scalars_to_u64!(impl $imp for $res, $method); - } -} - -macro_rules! promote_signed_scalars { - (impl $imp:ident for $res:ty, $method:ident) => { - promote_signed_scalars_to_i32!(impl $imp for $res, $method); - promote_signed_scalars_to_i64!(impl $imp for $res, $method); - } -} - macro_rules! promote_all_scalars { (impl $imp:ident for $res:ty, $method:ident) => { promote_unsigned_scalars!(impl $imp for $res, $method);