From 918ba5ba9402e0e58379a01420c2262d0c79aab6 Mon Sep 17 00:00:00 2001 From: koushiro Date: Wed, 21 Oct 2020 15:40:37 +0800 Subject: [PATCH] Update the crate to edition 2018 * update to the edition 2018 * update dependencies to the latest * upgrade the MSRV to 1.36 (required by unicode-normalization 0.1) * make clippy happy Signed-off-by: koushiro --- .travis.yml | 6 +++--- Cargo.toml | 9 +++++---- README.md | 3 +-- benches/bench.rs | 13 +++++++------ src/language/mod.rs | 2 +- src/lib.rs | 32 +++++++------------------------- src/pbkdf2.rs | 4 ++-- 7 files changed, 26 insertions(+), 43 deletions(-) diff --git a/.travis.yml b/.travis.yml index c71dfb2..b786b39 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ matrix: - rust: beta - rust: nightly env: BENCHES=true - - rust: 1.22.0 + - rust: 1.36.0 before_install: - sudo apt-get -qq update @@ -19,5 +19,5 @@ script: - cargo build --verbose --features rand,all-languages - cargo test --verbose --features rand,all-languages # benchmarks - - if ${BENCHES}; then cargo bench --verbose --features rand; fi - - if ${BENCHES}; then cargo bench --verbose --features rand,japanese; fi + - if [[ $TRAVIS_RUST_VERSION == "nightly" && ${BENCHES} ]]; then cargo bench --verbose --features rand; fi + - if [[ $TRAVIS_RUST_VERSION == "nightly" && ${BENCHES} ]]; then cargo bench --verbose --features rand,japanese; fi diff --git a/Cargo.toml b/Cargo.toml index f18be45..fae6fb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "bip39" version = "1.0.0-rc1" authors = ["Steven Roose "] +edition = "2018" license = "CC0-1.0" homepage = "https://github.com/rust-bitcoin/rust-bip39/" repository = "https://github.com/rust-bitcoin/rust-bip39/" @@ -37,9 +38,9 @@ all-languages = [ ] [dependencies] -bitcoin_hashes = "0.7.6" -unicode-normalization = "=0.1.9" -rand = { version = "0.6.0", optional = true } +bitcoin_hashes = "0.9.3" +unicode-normalization = "0.1.9" +rand = { version = "0.7.0", optional = true } [dev-dependencies] -rand = { version = "0.6.0", optional = false } +rand = "0.7.0" diff --git a/README.md b/README.md index b6d8cc1..cbde4e7 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,4 @@ A Rust implementation of BIP-39 mnemonic codes. ## MSRV -This crate supports Rust v1.22 and up. - +This crate supports Rust v1.36 and up. diff --git a/benches/bench.rs b/benches/bench.rs index e7e1157..7a5af41 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -32,8 +32,8 @@ const LANG: Language = Language::Spanish; #[bench] fn validate(b: &mut Bencher) { - let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes(); - let mnemonic = Mnemonic::from_entropy_in(LANG, &entropy).unwrap(); + let entropy = b"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"; + let mnemonic = Mnemonic::from_entropy_in(LANG, &entropy[..]).unwrap(); assert_eq!(mnemonic.word_count(), 24); let phrase = mnemonic.as_str(); @@ -44,13 +44,14 @@ fn validate(b: &mut Bencher) { #[bench] fn from_entropy(b: &mut Bencher) { - let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes(); + let entropy = b"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"; b.iter(|| { - let _ = Mnemonic::from_entropy_in(LANG, &entropy).unwrap(); + let _ = Mnemonic::from_entropy_in(LANG, &entropy[..]).unwrap(); }); } +#[cfg(feature = "rand")] #[bench] fn new_mnemonic(b: &mut Bencher) { b.iter(|| { @@ -60,8 +61,8 @@ fn new_mnemonic(b: &mut Bencher) { #[bench] fn to_seed(b: &mut Bencher) { - let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes(); - let m = Mnemonic::from_entropy_in(LANG, &entropy).unwrap(); + let entropy = b"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"; + let m = Mnemonic::from_entropy_in(LANG, &entropy[..]).unwrap(); b.iter(|| { let _ = m.to_seed(""); diff --git a/src/language/mod.rs b/src/language/mod.rs index b12bb0a..619b344 100644 --- a/src/language/mod.rs +++ b/src/language/mod.rs @@ -198,7 +198,7 @@ mod tests { let mut digest = sha256::Hash::engine(); for (_idx, word) in lang.word_list().iter().enumerate() { assert!(::unicode_normalization::is_nfkd(&word)); - write!(&mut digest, "{}\n", word).unwrap(); + writeln!(&mut digest, "{}", word).unwrap(); } assert_eq!(&sha256::Hash::from_engine(digest).to_string(), sum, "word list for language {} failed checksum check", lang, diff --git a/src/lib.rs b/src/lib.rs index ce87962..dfd9e42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,11 +26,6 @@ #![deny(unused_imports)] #![deny(missing_docs)] -extern crate bitcoin_hashes; -extern crate unicode_normalization; -#[cfg(feature = "rand")] -extern crate rand; - use std::{error, fmt, str}; use std::borrow::Cow; @@ -40,7 +35,7 @@ use unicode_normalization::UnicodeNormalization; mod language; mod pbkdf2; -pub use language::Language; +pub use self::language::Language; /// The ideagrapic space that should be used for Japanese lists. #[cfg(feature = "japanese")] @@ -48,7 +43,7 @@ pub use language::Language; const IDEOGRAPHIC_SPACE: char = ' '; /// A BIP39 error. -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub enum Error { /// Mnemonic has a word count that is not a multiple of 6. BadWordCount(usize), @@ -80,21 +75,8 @@ impl fmt::Display for Error { } } } -impl fmt::Debug for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(self, f) - } -} -impl error::Error for Error { - fn cause(&self) -> Option<&error::Error> { - None - } - - fn description(&self) -> &str { - "description() is deprecated; use Display" - } -} +impl error::Error for Error {} /// A mnemonic code. /// @@ -232,7 +214,7 @@ impl Mnemonic { pub fn language_of(s: &str) -> Result { // First we try wordlists that have guaranteed unique words. let first_word = s.split_whitespace().next().unwrap(); - if first_word.len() == 0 { + if first_word.is_empty() { return Err(Error::BadWordCount(0)); } for language in Language::all().iter().filter(|l| l.unique_words()) { @@ -566,19 +548,19 @@ mod tests { fn test_invalid_entropy() { //between 128 and 256 bits, but not divisible by 32 assert_eq!( - Mnemonic::from_entropy(&vec![b'x'; 17]), + Mnemonic::from_entropy(&[b'x'; 17]), Err(Error::BadEntropyBitCount(136)) ); //less than 128 bits assert_eq!( - Mnemonic::from_entropy(&vec![b'x'; 4]), + Mnemonic::from_entropy(&[b'x'; 4]), Err(Error::BadEntropyBitCount(32)) ); //greater than 256 bits assert_eq!( - Mnemonic::from_entropy(&vec![b'x'; 36]), + Mnemonic::from_entropy(&[b'x'; 36]), Err(Error::BadEntropyBitCount(288)) ); } diff --git a/src/pbkdf2.rs b/src/pbkdf2.rs index 66abe5a..43a5f9a 100644 --- a/src/pbkdf2.rs +++ b/src/pbkdf2.rs @@ -7,8 +7,8 @@ fn u32_to_array_be(val: u32) -> [u8; 4] { debug_assert_eq!(::std::mem::size_of::(), 4); // size_of isn't a constfn in 1.22 let mut res = [0; 4]; - for i in 0..4 { - res[i] = ((val >> (4 - i - 1)*8) & 0xff) as u8; + for (i, item) in res.iter_mut().enumerate() { + *item = ((val >> ((4 - i - 1)*8)) & 0xff) as u8; } res }