Skip to content

Commit

Permalink
Merge pull request #482 from rust-bitcoin/2020-09-modern
Browse files Browse the repository at this point in the history
Small modernization after MSRV bump
  • Loading branch information
stevenroose committed Oct 9, 2020
2 parents 7c47c9a + eda47c3 commit 5355b36
Show file tree
Hide file tree
Showing 27 changed files with 99 additions and 185 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Expand Up @@ -10,9 +10,6 @@ description = "General purpose library for using and interoperating with Bitcoin
keywords = [ "crypto", "bitcoin" ]
readme = "README.md"

[lib]
name = "bitcoin"
path = "src/lib.rs"

[features]
fuzztarget = ["secp256k1/fuzztarget", "bitcoin_hashes/fuzztarget"]
Expand All @@ -32,7 +29,6 @@ bitcoinconsensus = { version = "0.19.0-1", optional = true }
serde = { version = "1", optional = true }

[dev-dependencies]
hex = "=0.3.2"
serde_derive = "<1.0.99"
serde_json = "<1.0.45"
serde_test = "1"
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/deser_net_msg.rs
Expand Up @@ -31,9 +31,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/deserialize_address.rs
Expand Up @@ -36,9 +36,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/deserialize_amount.rs
Expand Up @@ -53,9 +53,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/deserialize_block.rs
Expand Up @@ -31,9 +31,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/deserialize_psbt.rs
Expand Up @@ -40,9 +40,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/deserialize_script.rs
Expand Up @@ -70,9 +70,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/deserialize_transaction.rs
Expand Up @@ -52,9 +52,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/outpoint_string.rs
Expand Up @@ -67,9 +67,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/uint128_fuzz.rs
Expand Up @@ -88,9 +88,9 @@ mod tests {
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
Expand Down
9 changes: 2 additions & 7 deletions src/blockdata/script.rs
Expand Up @@ -120,12 +120,7 @@ impl fmt::Display for Error {
}
}

#[allow(deprecated)]
impl error::Error for Error {
fn description(&self) -> &str {
"description() is deprecated; use Display"
}
}
impl error::Error for Error {}

#[cfg(feature="bitcoinconsensus")]
#[doc(hidden)]
Expand Down Expand Up @@ -422,7 +417,7 @@ impl Script {
}

/// Write the assembly decoding of the script to the formatter.
pub fn fmt_asm(&self, f: &mut fmt::Write) -> fmt::Result {
pub fn fmt_asm(&self, f: &mut dyn fmt::Write) -> fmt::Result {
let mut index = 0;
while index < self.0.len() {
let opcode = opcodes::All::from(self.0[index]);
Expand Down
13 changes: 4 additions & 9 deletions src/blockdata/transaction.rs
Expand Up @@ -24,7 +24,7 @@
//!

use std::default::Default;
use std::{fmt, io};
use std::{error, fmt, io};

use hashes::{self, Hash, sha256d};
use hashes::hex::FromHex;
Expand Down Expand Up @@ -128,13 +128,8 @@ impl fmt::Display for ParseOutPointError {
}
}

#[allow(deprecated)]
impl ::std::error::Error for ParseOutPointError {
fn description(&self) -> &str {
"description() is deprecated; use Display"
}

fn cause(&self) -> Option<&::std::error::Error> {
impl error::Error for ParseOutPointError {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
ParseOutPointError::Txid(ref e) => Some(e),
ParseOutPointError::Vout(ref e) => Some(e),
Expand All @@ -147,7 +142,7 @@ impl ::std::error::Error for ParseOutPointError {
/// It does not permit leading zeroes or non-digit characters.
fn parse_vout(s: &str) -> Result<u32, ParseOutPointError> {
if s.len() > 1 {
let first = s.chars().nth(0).unwrap();
let first = s.chars().next().unwrap();
if first == '0' || first == '+' {
return Err(ParseOutPointError::VoutNotCanonical);
}
Expand Down
21 changes: 8 additions & 13 deletions src/consensus/encode.rs
Expand Up @@ -109,9 +109,8 @@ impl fmt::Display for Error {
}
}

#[allow(deprecated)]
impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Io(ref e) => Some(e),
Error::Psbt(ref e) => Some(e),
Expand All @@ -126,10 +125,6 @@ impl error::Error for Error {
| Error::UnknownInventoryType(..) => None,
}
}

fn description(&self) -> &str {
"description() is deprecated; use Display"
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -376,14 +371,14 @@ impl_int_encodable!(i64, read_i64, emit_i64);

impl VarInt {
/// Gets the length of this VarInt when encoded.
/// Returns 1 for 0...0xFC, 3 for 0xFD...(2^16-1), 5 for 0x10000...(2^32-1),
/// Returns 1 for 0..=0xFC, 3 for 0xFD..=(2^16-1), 5 for 0x10000..=(2^32-1),
/// and 9 otherwise.
#[inline]
pub fn len(&self) -> usize {
match self.0 {
0...0xFC => { 1 }
0xFD...0xFFFF => { 3 }
0x10000...0xFFFFFFFF => { 5 }
0..=0xFC => { 1 }
0xFD..=0xFFFF => { 3 }
0x10000..=0xFFFFFFFF => { 5 }
_ => { 9 }
}
}
Expand All @@ -393,16 +388,16 @@ impl Encodable for VarInt {
#[inline]
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, Error> {
match self.0 {
0...0xFC => {
0..=0xFC => {
(self.0 as u8).consensus_encode(s)?;
Ok(1)
},
0xFD...0xFFFF => {
0xFD..=0xFFFF => {
s.emit_u8(0xFD)?;
(self.0 as u16).consensus_encode(s)?;
Ok(3)
},
0x10000...0xFFFFFFFF => {
0x10000..=0xFFFFFFFF => {
s.emit_u8(0xFE)?;
(self.0 as u32).consensus_encode(s)?;
Ok(5)
Expand Down
5 changes: 1 addition & 4 deletions src/hash_types.rs
Expand Up @@ -16,10 +16,7 @@
//! to avoid mixing data of the same hash format (like SHA256d) but of different meaning
//! (transaction id, block hash etc).

use consensus::encode::{Encodable, Decodable, Error};
use hashes::{Hash, sha256, sha256d, ripemd160, hash160};
use hashes::hex::{FromHex, ToHex};
use util::key::PublicKey;
use hashes::{Hash, sha256, sha256d, hash160};

macro_rules! impl_hashencode {
($hashtype:ident) => {
Expand Down
18 changes: 1 addition & 17 deletions src/lib.rs
Expand Up @@ -23,17 +23,9 @@
//! software.
//!

#![crate_name = "bitcoin"]
#![crate_type = "dylib"]
#![crate_type = "rlib"]

// Experimental features we need
#![cfg_attr(all(test, feature = "unstable"), feature(test))]

// Clippy whitelist
#![cfg_attr(feature = "clippy", allow(needless_range_loop))] // suggests making a big mess of array newtypes
#![cfg_attr(feature = "clippy", allow(extend_from_slice))] // `extend_from_slice` only available since 1.6

// Coding conventions
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
Expand All @@ -44,23 +36,17 @@
#![deny(unused_imports)]
#![deny(missing_docs)]

// In general, rust is absolutely horrid at supporting users doing things like,
// for example, compiling Rust code for real environments. Disable useless lints
// that don't do anything but annoy us and cant actually ever be resolved.
#![allow(bare_trait_objects)]
#![allow(ellipsis_inclusive_range_patterns)]

// Re-exported dependencies.
#[macro_use] pub extern crate bitcoin_hashes as hashes;
pub extern crate secp256k1;
pub extern crate bech32;

#[cfg(feature="bitcoinconsensus")] extern crate bitcoinconsensus;
#[cfg(feature = "serde")] extern crate serde;
#[cfg(all(test, feature = "serde"))] #[macro_use] extern crate serde_derive; // for 1.22.0 compat
#[cfg(all(test, feature = "serde"))] extern crate serde_json;
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
#[cfg(all(test, feature = "unstable"))] extern crate test;
#[cfg(feature="bitcoinconsensus")] extern crate bitcoinconsensus;

#[cfg(target_pointer_width = "16")]
compile_error!("rust-bitcoin cannot be used on 16-bit architectures");
Expand All @@ -75,8 +61,6 @@ pub mod network;
pub mod blockdata;
pub mod util;
pub mod consensus;
// Do not remove: required in order to get hash types implementation macros to work correctly
#[allow(unused_imports)]
pub mod hash_types;

pub use hash_types::*;
Expand Down
6 changes: 1 addition & 5 deletions src/network/mod.rs
Expand Up @@ -60,13 +60,9 @@ impl From<io::Error> for Error {
}
}

#[allow(deprecated)]
impl error::Error for Error {
fn description(&self) -> &str {
"description() is deprecated; use Display"
}

fn cause(&self) -> Option<&error::Error> {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Io(ref e) => Some(e),
Error::SocketMutexPoisoned | Error::SocketNotConnectedToPeer => None,
Expand Down
10 changes: 3 additions & 7 deletions src/util/address.rs
Expand Up @@ -38,6 +38,7 @@

use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
use std::error;

use bech32;
use hashes::Hash;
Expand Down Expand Up @@ -86,19 +87,14 @@ impl fmt::Display for Error {
}
}

#[allow(deprecated)]
impl ::std::error::Error for Error {
fn cause(&self) -> Option<&::std::error::Error> {
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Base58(ref e) => Some(e),
Error::Bech32(ref e) => Some(e),
_ => None,
}
}

fn description(&self) -> &str {
"description() is deprecated; use Display"
}
}

#[doc(hidden)]
Expand Down

0 comments on commit 5355b36

Please sign in to comment.