From 728017171468c9e3cf1c88974a13b18a1964fe8f Mon Sep 17 00:00:00 2001 From: "Demi M. Obenour" Date: Wed, 9 Oct 2019 11:02:04 -0400 Subject: [PATCH 1/5] Update to 2018 edition idioms --- ethbloom/src/lib.rs | 12 ++++++------ ethereum-types/src/lib.rs | 3 +-- kvdb-rocksdb/src/lib.rs | 10 ++++------ kvdb-web/src/error.rs | 2 +- parity-bytes/src/lib.rs | 8 ++++---- parity-crypto/benches/bench.rs | 3 --- parity-crypto/src/error.rs | 12 ++++++------ parity-crypto/src/hmac/mod.rs | 2 +- parity-crypto/src/pbkdf2/mod.rs | 4 ++-- rlp/tests/tests.rs | 4 ++-- transaction-pool/src/error.rs | 2 +- transaction-pool/src/pool.rs | 10 +++++----- transaction-pool/src/replace.rs | 2 +- transaction-pool/src/tests/helpers.rs | 2 +- transaction-pool/src/tests/mod.rs | 4 ++-- transaction-pool/src/transactions.rs | 2 +- uint/benches/bigint.rs | 6 +++--- uint/examples/modular.rs | 3 +-- uint/src/lib.rs | 2 +- uint/tests/uint_tests.rs | 10 ++++++++-- 20 files changed, 51 insertions(+), 52 deletions(-) diff --git a/ethbloom/src/lib.rs b/ethbloom/src/lib.rs index ec6514c18..682a0d247 100644 --- a/ethbloom/src/lib.rs +++ b/ethbloom/src/lib.rs @@ -140,13 +140,13 @@ impl Bloom { } pub fn contains_bloom<'a, B>(&self, bloom: B) -> bool where BloomRef<'a>: From { - let bloom_ref: BloomRef = bloom.into(); + let bloom_ref: BloomRef<'_> = bloom.into(); // workaround for https://github.com/rust-lang/rust/issues/43644 self.contains_bloom_ref(bloom_ref) } - fn contains_bloom_ref(&self, bloom: BloomRef) -> bool { - let self_ref: BloomRef = self.into(); + fn contains_bloom_ref(&self, bloom: BloomRef<'_>) -> bool { + let self_ref: BloomRef<'_> = self.into(); self_ref.contains_bloom(bloom) } @@ -158,7 +158,7 @@ impl Bloom { let mask = bloom_bits - 1; let bloom_bytes = (log2(bloom_bits) + 7) / 8; - let hash: Hash = input.into(); + let hash: Hash<'_> = input.into(); // must be a power of 2 assert_eq!(m & (m - 1), 0); @@ -183,7 +183,7 @@ impl Bloom { } pub fn accrue_bloom<'a, B>(&mut self, bloom: B) where BloomRef<'a>: From { - let bloom_ref: BloomRef = bloom.into(); + let bloom_ref: BloomRef<'_> = bloom.into(); assert_eq!(self.0.len(), BLOOM_SIZE); assert_eq!(bloom_ref.0.len(), BLOOM_SIZE); for i in 0..BLOOM_SIZE { @@ -213,7 +213,7 @@ impl<'a> BloomRef<'a> { #[allow(clippy::trivially_copy_pass_by_ref)] pub fn contains_bloom<'b, B>(&self, bloom: B) -> bool where BloomRef<'b>: From { - let bloom_ref: BloomRef = bloom.into(); + let bloom_ref: BloomRef<'_> = bloom.into(); assert_eq!(self.0.len(), BLOOM_SIZE); assert_eq!(bloom_ref.0.len(), BLOOM_SIZE); for i in 0..BLOOM_SIZE { diff --git a/ethereum-types/src/lib.rs b/ethereum-types/src/lib.rs index a6a828f1b..b491fd37b 100644 --- a/ethereum-types/src/lib.rs +++ b/ethereum-types/src/lib.rs @@ -1,7 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature = "std")] -extern crate core; + mod hash; mod uint; diff --git a/kvdb-rocksdb/src/lib.rs b/kvdb-rocksdb/src/lib.rs index 34388d47c..7f137953c 100644 --- a/kvdb-rocksdb/src/lib.rs +++ b/kvdb-rocksdb/src/lib.rs @@ -409,7 +409,7 @@ impl Database { } /// Commit buffered changes to database. Must be called under `flush_lock` - fn write_flushing_with_lock(&self, _lock: &mut MutexGuard) -> io::Result<()> { + fn write_flushing_with_lock(&self, _lock: &mut MutexGuard<'_, bool>) -> io::Result<()> { match *self.db.read() { Some(DBAndColumns { ref db, ref cfs }) => { let batch = WriteBatch::new(); @@ -534,7 +534,7 @@ impl Database { } /// Get database iterator for flushed data. - pub fn iter(&self, col: Option) -> Option { + pub fn iter(&self, col: Option) -> Option> { match *self.db.read() { Some(DBAndColumns { ref db, ref cfs }) => { let overlay = &self.overlay.read()[Self::to_overlay_column(col)]; @@ -561,7 +561,7 @@ impl Database { } } - fn iter_from_prefix(&self, col: Option, prefix: &[u8]) -> Option { + fn iter_from_prefix(&self, col: Option, prefix: &[u8]) -> Option> { match *self.db.read() { Some(DBAndColumns { ref db, ref cfs }) => { let iter = col.map_or_else(|| db.iterator_opt(IteratorMode::From(prefix, Direction::Forward), &self.read_opts), @@ -703,10 +703,8 @@ impl Drop for Database { #[cfg(test)] mod tests { - extern crate tempdir; - use std::str::FromStr; - use self::tempdir::TempDir; + use tempdir::TempDir; use ethereum_types::H256; use super::*; diff --git a/kvdb-web/src/error.rs b/kvdb-web/src/error.rs index 64361830d..f45295dc9 100644 --- a/kvdb-web/src/error.rs +++ b/kvdb-web/src/error.rs @@ -45,7 +45,7 @@ impl std::error::Error for Error { } impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { Error::WindowNotAvailable => write!(f, "Accessing a Window has failed"), Error::NotSupported(ref err) => write!( diff --git a/parity-bytes/src/lib.rs b/parity-bytes/src/lib.rs index 475864537..41bfd6134 100644 --- a/parity-bytes/src/lib.rs +++ b/parity-bytes/src/lib.rs @@ -32,7 +32,7 @@ use core::{cmp::min, fmt, ops}; pub struct PrettySlice<'a>(&'a [u8]); impl<'a> fmt::Debug for PrettySlice<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for i in 0..self.0.len() { if i > 0 { write!(f, "·{:02x}", self.0[i])?; @@ -45,7 +45,7 @@ impl<'a> fmt::Debug for PrettySlice<'a> { } impl<'a> fmt::Display for PrettySlice<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for i in 0..self.0.len() { write!(f, "{:02x}", self.0[i])?; } @@ -57,7 +57,7 @@ impl<'a> fmt::Display for PrettySlice<'a> { /// defaults cannot otherwise be avoided. pub trait ToPretty { /// Convert a type into a derivative form in order to make `format!` print it prettily. - fn pretty(&self) -> PrettySlice; + fn pretty(&self) -> PrettySlice<'_>; /// Express the object as a hex string. fn to_hex(&self) -> String { format!("{}", self.pretty()) @@ -65,7 +65,7 @@ pub trait ToPretty { } impl> ToPretty for T { - fn pretty(&self) -> PrettySlice { + fn pretty(&self) -> PrettySlice<'_> { PrettySlice(self.as_ref()) } } diff --git a/parity-crypto/benches/bench.rs b/parity-crypto/benches/bench.rs index f9f68dfd1..bdc9fd16d 100644 --- a/parity-crypto/benches/bench.rs +++ b/parity-crypto/benches/bench.rs @@ -14,9 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . - -extern crate parity_crypto; - #[macro_use] extern crate criterion; diff --git a/parity-crypto/src/error.rs b/parity-crypto/src/error.rs index 888bebb21..110c62cc5 100644 --- a/parity-crypto/src/error.rs +++ b/parity-crypto/src/error.rs @@ -43,7 +43,7 @@ enum PrivSymmErr { } impl StdError for Error { - fn source(&self) -> Option<&(StdError + 'static)> { + fn source(&self) -> Option<&(dyn StdError + 'static)> { match self { Error::Scrypt(scrypt_err) => Some(scrypt_err), Error::Symm(symm_err) => Some(symm_err), @@ -52,7 +52,7 @@ impl StdError for Error { } impl StdError for ScryptError { - fn source(&self) -> Option<&(StdError + 'static)> { + fn source(&self) -> Option<&(dyn StdError + 'static)> { match self { ScryptError::ScryptParam(err) => Some(err), ScryptError::ScryptLength(err) => Some(err), @@ -62,7 +62,7 @@ impl StdError for ScryptError { } impl StdError for SymmError { - fn source(&self) -> Option<&(StdError + 'static)> { + fn source(&self) -> Option<&(dyn StdError + 'static)> { match &self.0 { PrivSymmErr::BlockMode(err) => Some(err), PrivSymmErr::InvalidKeyLength(err) => Some(err), @@ -72,7 +72,7 @@ impl StdError for SymmError { } impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> { match self { Error::Scrypt(err)=> write!(f, "scrypt error: {}", err), Error::Symm(err) => write!(f, "symm error: {}", err), @@ -81,7 +81,7 @@ impl fmt::Display for Error { } impl fmt::Display for ScryptError { - fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> { match self { ScryptError::InvalidN => write!(f, "invalid n argument"), ScryptError::InvalidP => write!(f, "invalid p argument"), @@ -92,7 +92,7 @@ impl fmt::Display for ScryptError { } impl fmt::Display for SymmError { - fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> { match self { SymmError(PrivSymmErr::BlockMode(err)) => write!(f, "block cipher error: {}", err), SymmError(PrivSymmErr::KeyStream(err)) => write!(f, "ctr key stream ended: {}", err), diff --git a/parity-crypto/src/hmac/mod.rs b/parity-crypto/src/hmac/mod.rs index 43721fb0a..571df7b59 100644 --- a/parity-crypto/src/hmac/mod.rs +++ b/parity-crypto/src/hmac/mod.rs @@ -52,7 +52,7 @@ pub struct SigKey(KeyInner, PhantomData); struct DisposableBox(Box<[u8]>); impl std::fmt::Debug for DisposableBox { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", &self.0.as_ref()) } } diff --git a/parity-crypto/src/pbkdf2/mod.rs b/parity-crypto/src/pbkdf2/mod.rs index 83445e7e7..099e98893 100644 --- a/parity-crypto/src/pbkdf2/mod.rs +++ b/parity-crypto/src/pbkdf2/mod.rs @@ -17,11 +17,11 @@ pub struct Salt<'a>(pub &'a [u8]); pub struct Secret<'a>(pub &'a [u8]); -pub fn sha256(iter: u32, salt: Salt, sec: Secret, out: &mut [u8; 32]) { +pub fn sha256(iter: u32, salt: Salt<'_>, sec: Secret<'_>, out: &mut [u8; 32]) { pbkdf2::pbkdf2::>(sec.0, salt.0, iter as usize, out) } -pub fn sha512(iter: u32, salt: Salt, sec: Secret, out: &mut [u8; 64]) { +pub fn sha512(iter: u32, salt: Salt<'_>, sec: Secret<'_>, out: &mut [u8; 64]) { pbkdf2::pbkdf2::>(sec.0, salt.0, iter as usize, out) } diff --git a/rlp/tests/tests.rs b/rlp/tests/tests.rs index 07dd3fcc5..5d0310553 100644 --- a/rlp/tests/tests.rs +++ b/rlp/tests/tests.rs @@ -509,7 +509,7 @@ fn test_nested_list_roundtrip() { } impl Decodable for Inner { - fn decode(rlp: &Rlp) -> Result { + fn decode(rlp: &Rlp<'_>) -> Result { Ok(Inner(rlp.val_at(0)?, rlp.val_at(1)?)) } } @@ -526,7 +526,7 @@ fn test_nested_list_roundtrip() { } impl Decodable for Nest { - fn decode(rlp: &Rlp) -> Result { + fn decode(rlp: &Rlp<'_>) -> Result { Ok(Nest(rlp.list_at(0)?)) } } diff --git a/transaction-pool/src/error.rs b/transaction-pool/src/error.rs index 851f1f6e7..74ce76652 100644 --- a/transaction-pool/src/error.rs +++ b/transaction-pool/src/error.rs @@ -31,7 +31,7 @@ pub enum Error { pub type Result = result::Result>; impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Error::AlreadyImported(h) => write!(f, "[{:?}] already imported", h), diff --git a/transaction-pool/src/pool.rs b/transaction-pool/src/pool.rs index efc12b23d..539e7dded 100644 --- a/transaction-pool/src/pool.rs +++ b/transaction-pool/src/pool.rs @@ -138,7 +138,7 @@ impl Pool where /// new transaction via the supplied `ShouldReplace` implementation and may be evicted. /// /// The `Listener` will be informed on any drops or rejections. - pub fn import(&mut self, transaction: T, replace: &ShouldReplace) -> error::Result, T::Hash> { + pub fn import(&mut self, transaction: T, replace: &dyn ShouldReplace) -> error::Result, T::Hash> { let mem_usage = transaction.mem_usage(); if self.by_hash.contains_key(transaction.hash()) { @@ -288,7 +288,7 @@ impl Pool where /// /// Returns `None` in case we couldn't decide if the transaction should replace the worst transaction or not. /// In such case we will accept the transaction even though it is going to exceed the limit. - fn remove_worst(&mut self, transaction: &Transaction, replace: &ShouldReplace) -> error::Result>, T::Hash> { + fn remove_worst(&mut self, transaction: &Transaction, replace: &dyn ShouldReplace) -> error::Result>, T::Hash> { let to_remove = match self.worst_transactions.iter().next_back() { // No elements to remove? and the pool is still full? None => { @@ -437,7 +437,7 @@ impl Pool where } /// Returns an iterator of pending (ready) transactions. - pub fn pending>(&self, ready: R) -> PendingIterator { + pub fn pending>(&self, ready: R) -> PendingIterator<'_, T, R, S, L> { PendingIterator { ready, best_transactions: self.best_transactions.clone(), @@ -446,7 +446,7 @@ impl Pool where } /// Returns pending (ready) transactions from given sender. - pub fn pending_from_sender>(&self, ready: R, sender: &T::Sender) -> PendingIterator { + pub fn pending_from_sender>(&self, ready: R, sender: &T::Sender) -> PendingIterator<'_, T, R, S, L> { let best_transactions = self.transactions.get(sender) .and_then(|transactions| transactions.worst_and_best()) .map(|(_, best)| ScoreWithRef::new(best.0, best.1)) @@ -465,7 +465,7 @@ impl Pool where } /// Returns unprioritized list of ready transactions. - pub fn unordered_pending>(&self, ready: R) -> UnorderedIterator { + pub fn unordered_pending>(&self, ready: R) -> UnorderedIterator<'_, T, R, S> { UnorderedIterator { ready, senders: self.transactions.iter(), diff --git a/transaction-pool/src/replace.rs b/transaction-pool/src/replace.rs index 7a8896995..bc44e8d90 100644 --- a/transaction-pool/src/replace.rs +++ b/transaction-pool/src/replace.rs @@ -51,5 +51,5 @@ pub trait ShouldReplace { /// Decides if `new` should push out `old` transaction from the pool. /// /// NOTE returning `InsertNew` here can lead to some transactions being accepted above pool limits. - fn should_replace(&self, old: &ReplaceTransaction, new: &ReplaceTransaction) -> Choice; + fn should_replace(&self, old: &ReplaceTransaction<'_, T>, new: &ReplaceTransaction<'_, T>) -> Choice; } diff --git a/transaction-pool/src/tests/helpers.rs b/transaction-pool/src/tests/helpers.rs index f14314c2e..73d11f9e5 100644 --- a/transaction-pool/src/tests/helpers.rs +++ b/transaction-pool/src/tests/helpers.rs @@ -74,7 +74,7 @@ impl Scoring for DummyScoring { } impl ShouldReplace for DummyScoring { - fn should_replace(&self, old: &ReplaceTransaction, new: &ReplaceTransaction) -> scoring::Choice { + fn should_replace(&self, old: &ReplaceTransaction<'_, Transaction>, new: &ReplaceTransaction<'_, Transaction>) -> scoring::Choice { if self.always_insert { scoring::Choice::InsertNew } else if new.gas_price > old.gas_price { diff --git a/transaction-pool/src/tests/mod.rs b/transaction-pool/src/tests/mod.rs index 3d1ca4af4..7cc7c5553 100644 --- a/transaction-pool/src/tests/mod.rs +++ b/transaction-pool/src/tests/mod.rs @@ -272,9 +272,9 @@ fn should_skip_staled_pending_transactions() { let b = TransactionBuilder::default(); let mut txq = TestPool::default(); - let tx0 = import(&mut txq, b.tx().nonce(0).gas_price(5).new()).unwrap(); + let _tx0 = import(&mut txq, b.tx().nonce(0).gas_price(5).new()).unwrap(); let tx2 = import(&mut txq, b.tx().nonce(2).gas_price(5).new()).unwrap(); - let tx1 = import(&mut txq, b.tx().nonce(1).gas_price(5).new()).unwrap(); + let _tx1 = import(&mut txq, b.tx().nonce(1).gas_price(5).new()).unwrap(); // tx0 and tx1 are Stale, tx2 is Ready let mut pending = txq.pending(NonceReady::new(2)); diff --git a/transaction-pool/src/transactions.rs b/transaction-pool/src/transactions.rs index eb6151131..8256bf33a 100644 --- a/transaction-pool/src/transactions.rs +++ b/transaction-pool/src/transactions.rs @@ -70,7 +70,7 @@ impl> Transactions { self.transactions.len() } - pub fn iter(&self) -> ::std::slice::Iter> { + pub fn iter(&self) -> ::std::slice::Iter<'_, Transaction> { self.transactions.iter() } diff --git a/uint/benches/bigint.rs b/uint/benches/bigint.rs index e1d45adc9..2051a7e46 100644 --- a/uint/benches/bigint.rs +++ b/uint/benches/bigint.rs @@ -14,11 +14,11 @@ #[macro_use] extern crate criterion; -extern crate core; + #[macro_use] extern crate uint; -extern crate num_bigint; -extern crate rug; + + construct_uint! { pub struct U256(4); diff --git a/uint/examples/modular.rs b/uint/examples/modular.rs index 6cd9f7409..1364bd766 100644 --- a/uint/examples/modular.rs +++ b/uint/examples/modular.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[cfg(feature="std")] -extern crate core; + #[macro_use] extern crate uint; diff --git a/uint/src/lib.rs b/uint/src/lib.rs index 352ccf70d..1abe6f22d 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -25,7 +25,7 @@ pub extern crate rustc_hex; #[doc(hidden)] pub extern crate quickcheck; -extern crate crunchy; + pub use crunchy::unroll; #[macro_use] diff --git a/uint/tests/uint_tests.rs b/uint/tests/uint_tests.rs index de77b828e..5102fcf6a 100644 --- a/uint/tests/uint_tests.rs +++ b/uint/tests/uint_tests.rs @@ -1,4 +1,10 @@ -extern crate core; +// Copyright 2015-2019 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. #[macro_use] extern crate uint; @@ -8,7 +14,7 @@ extern crate uint; extern crate quickcheck; #[cfg_attr(all(test, feature = "quickcheck"), macro_use(unroll))] -extern crate crunchy; + use core::u64::MAX; use core::str::FromStr; From c1ab020a6bccbe3f7943a1e62618586581fbc0c8 Mon Sep 17 00:00:00 2001 From: "Demi M. Obenour" Date: Wed, 9 Oct 2019 14:17:00 -0400 Subject: [PATCH 2/5] Reduce extern crate usage --- uint/src/lib.rs | 9 ++- uint/src/uint.rs | 118 +++++++++++++++++++-------------------- uint/tests/uint_tests.rs | 23 ++++---- 3 files changed, 74 insertions(+), 76 deletions(-) diff --git a/uint/src/lib.rs b/uint/src/lib.rs index 1abe6f22d..01d728773 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -11,20 +11,19 @@ #![cfg_attr(not(feature = "std"), no_std)] #[doc(hidden)] -pub extern crate byteorder; +pub use byteorder; // Re-export libcore using an alias so that the macros can work without // requiring `extern crate core` downstream. #[doc(hidden)] -pub extern crate core as core_; +pub use core as core_; #[doc(hidden)] -pub extern crate rustc_hex; +pub use rustc_hex; #[cfg(feature="quickcheck")] #[doc(hidden)] -pub extern crate quickcheck; - +pub use quickcheck; pub use crunchy::unroll; diff --git a/uint/src/uint.rs b/uint/src/uint.rs index cc58ee220..e5608d462 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -81,7 +81,7 @@ macro_rules! uint_overflowing_binop { let ret_ptr = &mut ret as *mut [u64; $n_words] as *mut u64; let mut carry = 0u64; - unroll! { + $crate::unroll! { for i in 0..$n_words { use $crate::core_::ptr; @@ -119,10 +119,10 @@ macro_rules! uint_overflowing_binop { #[doc(hidden)] macro_rules! uint_full_mul_reg { ($name:ident, 8, $self_expr:expr, $other:expr) => { - uint_full_mul_reg!($name, 8, $self_expr, $other, |a, b| a != 0 || b != 0); + $crate::uint_full_mul_reg!($name, 8, $self_expr, $other, |a, b| a != 0 || b != 0); }; ($name:ident, $n_words:tt, $self_expr:expr, $other:expr) => { - uint_full_mul_reg!($name, $n_words, $self_expr, $other, |_, _| true); + $crate::uint_full_mul_reg!($name, $n_words, $self_expr, $other, |_, _| true); }; ($name:ident, $n_words:tt, $self_expr:expr, $other:expr, $check:expr) => ({{ #![allow(unused_assignments)] @@ -131,12 +131,12 @@ macro_rules! uint_full_mul_reg { let $name(ref you) = $other; let mut ret = [0u64; $n_words * 2]; - unroll! { + $crate::unroll! { for i in 0..$n_words { let mut carry = 0u64; let b = you[i]; - unroll! { + $crate::unroll! { for j in 0..$n_words { if $check(me[j], carry) { let a = me[j]; @@ -173,7 +173,7 @@ macro_rules! uint_full_mul_reg { #[doc(hidden)] macro_rules! uint_overflowing_mul { ($name:ident, $n_words: tt, $self_expr: expr, $other: expr) => ({ - let ret: [u64; $n_words * 2] = uint_full_mul_reg!($name, $n_words, $self_expr, $other); + let ret: [u64; $n_words * 2] = $crate::uint_full_mul_reg!($name, $n_words, $self_expr, $other); // The safety of this is enforced by the compiler let ret: [[u64; $n_words]; 2] = unsafe { $crate::core_::mem::transmute(ret) }; @@ -181,7 +181,7 @@ macro_rules! uint_overflowing_mul { // The compiler WILL NOT inline this if you remove this annotation. #[inline(always)] fn any_nonzero(arr: &[u64; $n_words]) -> bool { - unroll! { + $crate::unroll! { for i in 0..$n_words { if arr[i] != 0 { return true; @@ -234,7 +234,7 @@ macro_rules! impl_mul_from { fn mul(self, other: $other) -> $name { let bignum: $name = other.into(); let (result, overflow) = self.overflowing_mul(bignum); - panic_on_overflow!(overflow); + $crate::panic_on_overflow!(overflow); result } } @@ -245,7 +245,7 @@ macro_rules! impl_mul_from { fn mul(self, other: &'a $other) -> $name { let bignum: $name = (*other).into(); let (result, overflow) = self.overflowing_mul(bignum); - panic_on_overflow!(overflow); + $crate::panic_on_overflow!(overflow); result } } @@ -256,7 +256,7 @@ macro_rules! impl_mul_from { fn mul(self, other: &'a $other) -> $name { let bignum: $name = (*other).into(); let (result, overflow) = self.overflowing_mul(bignum); - panic_on_overflow!(overflow); + $crate::panic_on_overflow!(overflow); result } } @@ -267,7 +267,7 @@ macro_rules! impl_mul_from { fn mul(self, other: $other) -> $name { let bignum: $name = other.into(); let (result, overflow) = self.overflowing_mul(bignum); - panic_on_overflow!(overflow); + $crate::panic_on_overflow!(overflow); result } } @@ -290,7 +290,7 @@ macro_rules! impl_mul_for_primitive { fn mul(self, other: $other) -> $name { let (result, carry) = self.overflowing_mul_u64(other as u64); - panic_on_overflow!(carry > 0); + $crate::panic_on_overflow!(carry > 0); result } } @@ -300,7 +300,7 @@ macro_rules! impl_mul_for_primitive { fn mul(self, other: &'a $other) -> $name { let (result, carry) = self.overflowing_mul_u64(*other as u64); - panic_on_overflow!(carry > 0); + $crate::panic_on_overflow!(carry > 0); result } } @@ -310,7 +310,7 @@ macro_rules! impl_mul_for_primitive { fn mul(self, other: &'a $other) -> $name { let (result, carry) = self.overflowing_mul_u64(*other as u64); - panic_on_overflow!(carry > 0); + $crate::panic_on_overflow!(carry > 0); result } } @@ -320,7 +320,7 @@ macro_rules! impl_mul_for_primitive { fn mul(self, other: $other) -> $name { let (result, carry) = self.overflowing_mul_u64(other as u64); - panic_on_overflow!(carry > 0); + $crate::panic_on_overflow!(carry > 0); result } } @@ -337,11 +337,11 @@ macro_rules! impl_mul_for_primitive { #[macro_export] macro_rules! construct_uint { ( $(#[$attr:meta])* $visibility:vis struct $name:ident (1); ) => { - construct_uint!{ @construct $(#[$attr])* $visibility struct $name (1); } + $crate::construct_uint!{ @construct $(#[$attr])* $visibility struct $name (1); } }; ( $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_words:tt ); ) => { - construct_uint! { @construct $(#[$attr])* $visibility struct $name ($n_words); } + $crate::construct_uint! { @construct $(#[$attr])* $visibility struct $name ($n_words); } impl $crate::core_::convert::From for $name { fn from(value: u128) -> $name { @@ -863,22 +863,22 @@ macro_rules! construct_uint { while n > u_one { if is_even(&n) { - x = overflowing!(x.overflowing_mul(x), overflow); + x = $crate::overflowing!(x.overflowing_mul(x), overflow); n = n >> 1usize; } else { - y = overflowing!(x.overflowing_mul(y), overflow); - x = overflowing!(x.overflowing_mul(x), overflow); + y = $crate::overflowing!(x.overflowing_mul(y), overflow); + x = $crate::overflowing!(x.overflowing_mul(x), overflow); n = (n - u_one) >> 1usize; } } - let res = overflowing!(x.overflowing_mul(y), overflow); + let res = $crate::overflowing!(x.overflowing_mul(y), overflow); (res, overflow) } /// Add with overflow. #[inline(always)] pub fn overflowing_add(self, other: $name) -> ($name, bool) { - uint_overflowing_binop!( + $crate::uint_overflowing_binop!( $name, $n_words, self, @@ -906,7 +906,7 @@ macro_rules! construct_uint { /// Subtraction which underflows and returns a flag if it does. #[inline(always)] pub fn overflowing_sub(self, other: $name) -> ($name, bool) { - uint_overflowing_binop!( + $crate::uint_overflowing_binop!( $name, $n_words, self, @@ -934,7 +934,7 @@ macro_rules! construct_uint { /// Multiply with overflow, returning a flag if it does. #[inline(always)] pub fn overflowing_mul(self, other: $name) -> ($name, bool) { - uint_overflowing_mul!($name, $n_words, self, other) + $crate::uint_overflowing_mul!($name, $n_words, self, other) } /// Multiplication which saturates at the maximum value.. @@ -1156,10 +1156,10 @@ macro_rules! construct_uint { } } - impl_map_from!($name, u8, u64); - impl_map_from!($name, u16, u64); - impl_map_from!($name, u32, u64); - impl_map_from!($name, usize, u64); + $crate::impl_map_from!($name, u8, u64); + $crate::impl_map_from!($name, u16, u64); + $crate::impl_map_from!($name, u32, u64); + $crate::impl_map_from!($name, usize, u64); impl $crate::core_::convert::From for $name { fn from(value: i64) -> $name { @@ -1170,10 +1170,10 @@ macro_rules! construct_uint { } } - impl_map_from!($name, i8, i64); - impl_map_from!($name, i16, i64); - impl_map_from!($name, i32, i64); - impl_map_from!($name, isize, i64); + $crate::impl_map_from!($name, i8, i64); + $crate::impl_map_from!($name, i16, i64); + $crate::impl_map_from!($name, i32, i64); + $crate::impl_map_from!($name, isize, i64); // Converts from big endian representation. impl<'a> $crate::core_::convert::From<&'a [u8]> for $name { @@ -1182,23 +1182,23 @@ macro_rules! construct_uint { } } - impl_try_from_for_primitive!($name, u8); - impl_try_from_for_primitive!($name, u16); - impl_try_from_for_primitive!($name, u32); - impl_try_from_for_primitive!($name, usize); - impl_try_from_for_primitive!($name, u64); - impl_try_from_for_primitive!($name, i8); - impl_try_from_for_primitive!($name, i16); - impl_try_from_for_primitive!($name, i32); - impl_try_from_for_primitive!($name, isize); - impl_try_from_for_primitive!($name, i64); + $crate::impl_try_from_for_primitive!($name, u8); + $crate::impl_try_from_for_primitive!($name, u16); + $crate::impl_try_from_for_primitive!($name, u32); + $crate::impl_try_from_for_primitive!($name, usize); + $crate::impl_try_from_for_primitive!($name, u64); + $crate::impl_try_from_for_primitive!($name, i8); + $crate::impl_try_from_for_primitive!($name, i16); + $crate::impl_try_from_for_primitive!($name, i32); + $crate::impl_try_from_for_primitive!($name, isize); + $crate::impl_try_from_for_primitive!($name, i64); impl $crate::core_::ops::Add for $name where T: Into<$name> { type Output = $name; fn add(self, other: T) -> $name { let (result, overflow) = self.overflowing_add(other.into()); - panic_on_overflow!(overflow); + $crate::panic_on_overflow!(overflow); result } } @@ -1214,7 +1214,7 @@ macro_rules! construct_uint { impl $crate::core_::ops::AddAssign<$name> for $name { fn add_assign(&mut self, other: $name) { let (result, overflow) = self.overflowing_add(other); - panic_on_overflow!(overflow); + $crate::panic_on_overflow!(overflow); *self = result } } @@ -1225,7 +1225,7 @@ macro_rules! construct_uint { #[inline] fn sub(self, other: T) -> $name { let (result, overflow) = self.overflowing_sub(other.into()); - panic_on_overflow!(overflow); + $crate::panic_on_overflow!(overflow); result } } @@ -1241,23 +1241,23 @@ macro_rules! construct_uint { impl $crate::core_::ops::SubAssign<$name> for $name { fn sub_assign(&mut self, other: $name) { let (result, overflow) = self.overflowing_sub(other); - panic_on_overflow!(overflow); + $crate::panic_on_overflow!(overflow); *self = result } } // all other impls - impl_mul_from!($name, $name); - impl_mul_for_primitive!($name, u8); - impl_mul_for_primitive!($name, u16); - impl_mul_for_primitive!($name, u32); - impl_mul_for_primitive!($name, u64); - impl_mul_for_primitive!($name, usize); - impl_mul_for_primitive!($name, i8); - impl_mul_for_primitive!($name, i16); - impl_mul_for_primitive!($name, i32); - impl_mul_for_primitive!($name, i64); - impl_mul_for_primitive!($name, isize); + $crate::impl_mul_from!($name, $name); + $crate::impl_mul_for_primitive!($name, u8); + $crate::impl_mul_for_primitive!($name, u16); + $crate::impl_mul_for_primitive!($name, u32); + $crate::impl_mul_for_primitive!($name, u64); + $crate::impl_mul_for_primitive!($name, usize); + $crate::impl_mul_for_primitive!($name, i8); + $crate::impl_mul_for_primitive!($name, i16); + $crate::impl_mul_for_primitive!($name, i32); + $crate::impl_mul_for_primitive!($name, i64); + $crate::impl_mul_for_primitive!($name, isize); impl $crate::core_::ops::Div for $name where T: Into<$name> { type Output = $name; @@ -1526,10 +1526,10 @@ macro_rules! construct_uint { } } - impl_std_for_uint!($name, $n_words); + $crate::impl_std_for_uint!($name, $n_words); // `$n_words * 8` because macro expects bytes and // uints use 64 bit (8 byte) words - impl_quickcheck_arbitrary_for_uint!($name, ($n_words * 8)); + $crate::impl_quickcheck_arbitrary_for_uint!($name, ($n_words * 8)); } } diff --git a/uint/tests/uint_tests.rs b/uint/tests/uint_tests.rs index 5102fcf6a..9154e5636 100644 --- a/uint/tests/uint_tests.rs +++ b/uint/tests/uint_tests.rs @@ -6,20 +6,19 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[macro_use] -extern crate uint; - -#[cfg(feature = "quickcheck")] -#[macro_use] -extern crate quickcheck; - -#[cfg_attr(all(test, feature = "quickcheck"), macro_use(unroll))] - +// Copyright 2015-2017 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. use core::u64::MAX; use core::str::FromStr; use core::convert::TryInto; -use uint::{FromDecStrErr}; +use uint::{FromDecStrErr, construct_uint, overflowing}; +use crunchy::unroll; construct_uint! { pub struct U256(4); @@ -1110,8 +1109,8 @@ pub mod laws { macro_rules! uint_laws { ($mod_name:ident, $uint_ty:ident) => { mod $mod_name { - use quickcheck::TestResult; - use super::{$uint_ty}; + use quickcheck::{TestResult, quickcheck}; + use super::$uint_ty; quickcheck! { fn associative_add(x: $uint_ty, y: $uint_ty, z: $uint_ty) -> TestResult { From 66d564941e7092c0bd8759e0d37d3a9022f83b1e Mon Sep 17 00:00:00 2001 From: "Demi M. Obenour" Date: Wed, 9 Oct 2019 16:43:04 -0400 Subject: [PATCH 3/5] fix missing import in tests --- uint/tests/uint_tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/uint/tests/uint_tests.rs b/uint/tests/uint_tests.rs index 9154e5636..6513a6d2a 100644 --- a/uint/tests/uint_tests.rs +++ b/uint/tests/uint_tests.rs @@ -1106,6 +1106,7 @@ fn trailing_zeros() { #[cfg(feature="quickcheck")] pub mod laws { + use super::construct_uint; macro_rules! uint_laws { ($mod_name:ident, $uint_ty:ident) => { mod $mod_name { From add238f0f72cf4e58cc0efbc97953fb923c18e04 Mon Sep 17 00:00:00 2001 From: "Demi M. Obenour" Date: Wed, 9 Oct 2019 17:08:08 -0400 Subject: [PATCH 4/5] Add missing `use $crate::unroll` --- uint/src/lib.rs | 1 + uint/src/uint.rs | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/uint/src/lib.rs b/uint/src/lib.rs index 01d728773..5f803a271 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -25,6 +25,7 @@ pub use rustc_hex; #[doc(hidden)] pub use quickcheck; +#[doc(hidden)] pub use crunchy::unroll; #[macro_use] diff --git a/uint/src/uint.rs b/uint/src/uint.rs index e5608d462..5006bbd38 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -81,7 +81,10 @@ macro_rules! uint_overflowing_binop { let ret_ptr = &mut ret as *mut [u64; $n_words] as *mut u64; let mut carry = 0u64; - $crate::unroll! { + // `unroll!` is recursive, but doesn’t use `$crate::unroll`, so we need to ensure that it + // is in scope unqualified. + use $crate::unroll; + unroll! { for i in 0..$n_words { use $crate::core_::ptr; @@ -131,12 +134,13 @@ macro_rules! uint_full_mul_reg { let $name(ref you) = $other; let mut ret = [0u64; $n_words * 2]; - $crate::unroll! { + use $crate::unroll; + unroll! { for i in 0..$n_words { let mut carry = 0u64; let b = you[i]; - $crate::unroll! { + unroll! { for j in 0..$n_words { if $check(me[j], carry) { let a = me[j]; @@ -181,7 +185,8 @@ macro_rules! uint_overflowing_mul { // The compiler WILL NOT inline this if you remove this annotation. #[inline(always)] fn any_nonzero(arr: &[u64; $n_words]) -> bool { - $crate::unroll! { + use $crate::unroll; + unroll! { for i in 0..$n_words { if arr[i] != 0 { return true; From d694635d141cd59fe528fb72823041857e9df946 Mon Sep 17 00:00:00 2001 From: "Demi M. Obenour" Date: Tue, 15 Oct 2019 10:40:57 -0400 Subject: [PATCH 5/5] Address review comments --- uint/benches/bigint.rs | 8 ++------ uint/tests/uint_tests.rs | 8 -------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/uint/benches/bigint.rs b/uint/benches/bigint.rs index 2051a7e46..61f24e93d 100644 --- a/uint/benches/bigint.rs +++ b/uint/benches/bigint.rs @@ -12,13 +12,9 @@ //! rustup run cargo bench //! ``` -#[macro_use] -extern crate criterion; - -#[macro_use] -extern crate uint; - +use criterion::{criterion_group, criterion_main}; +use uint::{construct_uint, uint_full_mul_reg}; construct_uint! { pub struct U256(4); diff --git a/uint/tests/uint_tests.rs b/uint/tests/uint_tests.rs index 6513a6d2a..7f36b6a65 100644 --- a/uint/tests/uint_tests.rs +++ b/uint/tests/uint_tests.rs @@ -6,14 +6,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// Copyright 2015-2017 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use core::u64::MAX; use core::str::FromStr; use core::convert::TryInto;