Skip to content

Commit

Permalink
Update to 2018 edition idioms (#237)
Browse files Browse the repository at this point in the history
* Update to 2018 edition idioms

* Reduce extern crate usage

* fix missing import in tests

* Add missing `use $crate::unroll`

* Address review comments
  • Loading branch information
Demi-Marie authored and debris committed Oct 16, 2019
1 parent fe54a47 commit e9e3ab3
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 127 deletions.
12 changes: 6 additions & 6 deletions ethbloom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ impl Bloom {
}

pub fn contains_bloom<'a, B>(&self, bloom: B) -> bool where BloomRef<'a>: From<B> {
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)
}

Expand All @@ -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);
Expand All @@ -183,7 +183,7 @@ impl Bloom {
}

pub fn accrue_bloom<'a, B>(&mut self, bloom: B) where BloomRef<'a>: From<B> {
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 {
Expand Down Expand Up @@ -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<B> {
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 {
Expand Down
3 changes: 1 addition & 2 deletions ethereum-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
extern crate core;


mod hash;
mod uint;
Expand Down
10 changes: 4 additions & 6 deletions kvdb-rocksdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>) -> 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();
Expand Down Expand Up @@ -534,7 +534,7 @@ impl Database {
}

/// Get database iterator for flushed data.
pub fn iter(&self, col: Option<u32>) -> Option<DatabaseIterator> {
pub fn iter(&self, col: Option<u32>) -> Option<DatabaseIterator<'_>> {
match *self.db.read() {
Some(DBAndColumns { ref db, ref cfs }) => {
let overlay = &self.overlay.read()[Self::to_overlay_column(col)];
Expand All @@ -561,7 +561,7 @@ impl Database {
}
}

fn iter_from_prefix(&self, col: Option<u32>, prefix: &[u8]) -> Option<DatabaseIterator> {
fn iter_from_prefix(&self, col: Option<u32>, prefix: &[u8]) -> Option<DatabaseIterator<'_>> {
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),
Expand Down Expand Up @@ -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::*;

Expand Down
2 changes: 1 addition & 1 deletion kvdb-web/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
8 changes: 4 additions & 4 deletions parity-bytes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])?;
Expand All @@ -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])?;
}
Expand All @@ -57,15 +57,15 @@ 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())
}
}

impl<T: AsRef<[u8]>> ToPretty for T {
fn pretty(&self) -> PrettySlice {
fn pretty(&self) -> PrettySlice<'_> {
PrettySlice(self.as_ref())
}
}
Expand Down
3 changes: 0 additions & 3 deletions parity-crypto/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.


extern crate parity_crypto;

#[macro_use]
extern crate criterion;

Expand Down
12 changes: 6 additions & 6 deletions parity-crypto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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"),
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion parity-crypto/src/hmac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct SigKey<T>(KeyInner, PhantomData<T>);
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())
}
}
Expand Down
4 changes: 2 additions & 2 deletions parity-crypto/src/pbkdf2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<hmac::Hmac<sha2::Sha256>>(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::<hmac::Hmac<sha2::Sha512>>(sec.0, salt.0, iter as usize, out)
}

Expand Down
4 changes: 2 additions & 2 deletions rlp/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ fn test_nested_list_roundtrip() {
}

impl Decodable for Inner {
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
fn decode(rlp: &Rlp<'_>) -> Result<Self, DecoderError> {
Ok(Inner(rlp.val_at(0)?, rlp.val_at(1)?))
}
}
Expand All @@ -526,7 +526,7 @@ fn test_nested_list_roundtrip() {
}

impl<T: Decodable> Decodable for Nest<T> {
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
fn decode(rlp: &Rlp<'_>) -> Result<Self, DecoderError> {
Ok(Nest(rlp.list_at(0)?))
}
}
Expand Down
2 changes: 1 addition & 1 deletion transaction-pool/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum Error<Hash: fmt::Debug + fmt::LowerHex> {
pub type Result<T, H> = result::Result<T, Error<H>>;

impl<H: fmt::Debug + fmt::LowerHex> fmt::Display for Error<H> {
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),
Expand Down
10 changes: 5 additions & 5 deletions transaction-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<T, S, L> Pool<T, S, L> 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<T>) -> error::Result<Arc<T>, T::Hash> {
pub fn import(&mut self, transaction: T, replace: &dyn ShouldReplace<T>) -> error::Result<Arc<T>, T::Hash> {
let mem_usage = transaction.mem_usage();

if self.by_hash.contains_key(transaction.hash()) {
Expand Down Expand Up @@ -288,7 +288,7 @@ impl<T, S, L> Pool<T, S, L> 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<T>, replace: &ShouldReplace<T>) -> error::Result<Option<Transaction<T>>, T::Hash> {
fn remove_worst(&mut self, transaction: &Transaction<T>, replace: &dyn ShouldReplace<T>) -> error::Result<Option<Transaction<T>>, T::Hash> {
let to_remove = match self.worst_transactions.iter().next_back() {
// No elements to remove? and the pool is still full?
None => {
Expand Down Expand Up @@ -437,7 +437,7 @@ impl<T, S, L> Pool<T, S, L> where
}

/// Returns an iterator of pending (ready) transactions.
pub fn pending<R: Ready<T>>(&self, ready: R) -> PendingIterator<T, R, S, L> {
pub fn pending<R: Ready<T>>(&self, ready: R) -> PendingIterator<'_, T, R, S, L> {
PendingIterator {
ready,
best_transactions: self.best_transactions.clone(),
Expand All @@ -446,7 +446,7 @@ impl<T, S, L> Pool<T, S, L> where
}

/// Returns pending (ready) transactions from given sender.
pub fn pending_from_sender<R: Ready<T>>(&self, ready: R, sender: &T::Sender) -> PendingIterator<T, R, S, L> {
pub fn pending_from_sender<R: Ready<T>>(&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))
Expand All @@ -465,7 +465,7 @@ impl<T, S, L> Pool<T, S, L> where
}

/// Returns unprioritized list of ready transactions.
pub fn unordered_pending<R: Ready<T>>(&self, ready: R) -> UnorderedIterator<T, R, S> {
pub fn unordered_pending<R: Ready<T>>(&self, ready: R) -> UnorderedIterator<'_, T, R, S> {
UnorderedIterator {
ready,
senders: self.transactions.iter(),
Expand Down
2 changes: 1 addition & 1 deletion transaction-pool/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ pub trait ShouldReplace<T> {
/// 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<T>, new: &ReplaceTransaction<T>) -> Choice;
fn should_replace(&self, old: &ReplaceTransaction<'_, T>, new: &ReplaceTransaction<'_, T>) -> Choice;
}
2 changes: 1 addition & 1 deletion transaction-pool/src/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Scoring<Transaction> for DummyScoring {
}

impl ShouldReplace<Transaction> for DummyScoring {
fn should_replace(&self, old: &ReplaceTransaction<Transaction>, new: &ReplaceTransaction<Transaction>) -> 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 {
Expand Down
4 changes: 2 additions & 2 deletions transaction-pool/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion transaction-pool/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<T: fmt::Debug, S: Scoring<T>> Transactions<T, S> {
self.transactions.len()
}

pub fn iter(&self) -> ::std::slice::Iter<Transaction<T>> {
pub fn iter(&self) -> ::std::slice::Iter<'_, Transaction<T>> {
self.transactions.iter()
}

Expand Down
10 changes: 3 additions & 7 deletions uint/benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
//! rustup run cargo bench
//! ```
#[macro_use]
extern crate criterion;
extern crate core;
#[macro_use]
extern crate uint;
extern crate num_bigint;
extern crate rug;

use criterion::{criterion_group, criterion_main};
use uint::{construct_uint, uint_full_mul_reg};

construct_uint! {
pub struct U256(4);
Expand Down
3 changes: 1 addition & 2 deletions uint/examples/modular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions uint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
#![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;

extern crate crunchy;
#[doc(hidden)]
pub use crunchy::unroll;

#[macro_use]
Expand Down
Loading

0 comments on commit e9e3ab3

Please sign in to comment.