From a8ed95ea07e6a4c2336e946d72ad94ac98defb2f Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Mon, 24 Jan 2022 11:33:03 +1100 Subject: [PATCH] Refactor where statements Our usage of `where` statements is not uniform, nor is it inline with the typical layout suggested by `rustfmt`. Make an effort to be more uniform with usage of `where` statements. However, explicitly do _not_ do every usage since sometimes our usage favours terseness (all on a single line). --- src/blockdata/script.rs | 20 +++++++++------- src/blockdata/transaction.rs | 9 ++++++-- src/internal_macros.rs | 7 +++--- src/serde_utils.rs | 45 +++++++++++++++++++++--------------- src/util/amount.rs | 8 ++++--- src/util/hash.rs | 15 +++++++----- src/util/merkleblock.rs | 9 ++++++-- src/util/taproot.rs | 9 +++++--- 8 files changed, 76 insertions(+), 46 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index d67ebc22..db6a4081 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -89,9 +89,8 @@ impl fmt::UpperHex for Script { impl hex::FromHex for Script { fn from_byte_iter(iter: I) -> Result - where I: Iterator> + - ExactSizeIterator + - DoubleEndedIterator, + where + I: Iterator> + ExactSizeIterator + DoubleEndedIterator, { Vec::from_byte_iter(iter).map(|v| Script(Box::<[u8]>::from(v))) } @@ -949,7 +948,8 @@ impl_index_newtype!(Builder, u8); #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl<'de> serde::Deserialize<'de> for Script { fn deserialize(deserializer: D) -> Result - where D: serde::Deserializer<'de>, + where + D: serde::Deserializer<'de>, { use core::fmt::Formatter; use hashes::hex::FromHex; @@ -965,20 +965,23 @@ impl<'de> serde::Deserialize<'de> for Script { } fn visit_str(self, v: &str) -> Result - where E: serde::de::Error, + where + E: serde::de::Error, { let v = Vec::from_hex(v).map_err(E::custom)?; Ok(Script::from(v)) } fn visit_borrowed_str(self, v: &'de str) -> Result - where E: serde::de::Error, + where + E: serde::de::Error, { self.visit_str(v) } fn visit_string(self, v: String) -> Result - where E: serde::de::Error, + where + E: serde::de::Error, { self.visit_str(&v) } @@ -995,7 +998,8 @@ impl<'de> serde::Deserialize<'de> for Script { } fn visit_bytes(self, v: &[u8]) -> Result - where E: serde::de::Error, + where + E: serde::de::Error, { Ok(Script::from(v.to_vec())) } diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 2011b8b1..d6d045ed 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -504,7 +504,9 @@ impl Transaction { #[cfg(feature="bitcoinconsensus")] #[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))] pub fn verify(&self, spent: S) -> Result<(), script::Error> - where S: FnMut(&OutPoint) -> Option { + where + S: FnMut(&OutPoint) -> Option + { self.verify_with_flags(spent, ::bitcoinconsensus::VERIFY_ALL) } @@ -513,7 +515,10 @@ impl Transaction { #[cfg(feature="bitcoinconsensus")] #[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))] pub fn verify_with_flags(&self, mut spent: S, flags: F) -> Result<(), script::Error> - where S: FnMut(&OutPoint) -> Option, F : Into { + where + S: FnMut(&OutPoint) -> Option, + F: Into + { let tx = encode::serialize(&*self); let flags: u32 = flags.into(); for (idx, input) in self.input.iter().enumerate() { diff --git a/src/internal_macros.rs b/src/internal_macros.rs index daf78ab3..53ce2d60 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -446,9 +446,10 @@ macro_rules! impl_bytes_newtype { impl $crate::hashes::hex::FromHex for $t { fn from_byte_iter(iter: I) -> Result - where I: ::core::iter::Iterator> + - ::core::iter::ExactSizeIterator + - ::core::iter::DoubleEndedIterator, + where + I: ::core::iter::Iterator> + + ::core::iter::ExactSizeIterator + + ::core::iter::DoubleEndedIterator, { if iter.len() == $len { let mut ret = [0; $len]; diff --git a/src/serde_utils.rs b/src/serde_utils.rs index 739f5eb2..da4011e5 100644 --- a/src/serde_utils.rs +++ b/src/serde_utils.rs @@ -13,8 +13,8 @@ pub mod btreemap_byte_values { use hashes::hex::{FromHex, ToHex}; use serde; - pub fn serialize(v: &BTreeMap>, s: S) - -> Result where + pub fn serialize(v: &BTreeMap>, s: S) -> Result + where S: serde::Serializer, T: serde::Serialize + ::core::hash::Hash + Eq + Ord, { @@ -32,15 +32,16 @@ pub mod btreemap_byte_values { } } - pub fn deserialize<'de, D, T>(d: D) - -> Result>, D::Error> where + pub fn deserialize<'de, D, T>(d: D) -> Result>, D::Error> + where D: serde::Deserializer<'de>, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, { use ::core::marker::PhantomData; struct Visitor(PhantomData); - impl<'de, T> serde::de::Visitor<'de> for Visitor where + impl<'de, T> serde::de::Visitor<'de> for Visitor + where T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, { type Value = BTreeMap>; @@ -79,8 +80,8 @@ pub mod btreemap_as_seq { use prelude::*; use serde; - pub fn serialize(v: &BTreeMap, s: S) - -> Result where + pub fn serialize(v: &BTreeMap, s: S) -> Result + where S: serde::Serializer, T: serde::Serialize + ::core::hash::Hash + Eq + Ord, U: serde::Serialize, @@ -99,8 +100,8 @@ pub mod btreemap_as_seq { } } - pub fn deserialize<'de, D, T, U>(d: D) - -> Result, D::Error> where + pub fn deserialize<'de, D, T, U>(d: D) -> Result, D::Error> + where D: serde::Deserializer<'de>, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, U: serde::Deserialize<'de>, @@ -108,7 +109,8 @@ pub mod btreemap_as_seq { use ::core::marker::PhantomData; struct Visitor(PhantomData<(T, U)>); - impl<'de, T, U> serde::de::Visitor<'de> for Visitor where + impl<'de, T, U> serde::de::Visitor<'de> for Visitor + where T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, U: serde::Deserialize<'de>, { @@ -164,8 +166,8 @@ pub mod btreemap_as_seq_byte_values { &'a [u8], ); - pub fn serialize(v: &BTreeMap>, s: S) - -> Result where + pub fn serialize(v: &BTreeMap>, s: S) -> Result + where S: serde::Serializer, T: serde::Serialize + ::core::hash::Hash + Eq + Ord + 'static, { @@ -183,15 +185,16 @@ pub mod btreemap_as_seq_byte_values { } } - pub fn deserialize<'de, D, T>(d: D) - -> Result>, D::Error> where + pub fn deserialize<'de, D, T>(d: D) -> Result>, D::Error> + where D: serde::Deserializer<'de>, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, { use ::core::marker::PhantomData; struct Visitor(PhantomData); - impl<'de, T> serde::de::Visitor<'de> for Visitor where + impl<'de, T> serde::de::Visitor<'de> for Visitor + where T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, { type Value = BTreeMap>; @@ -228,7 +231,8 @@ pub mod hex_bytes { use serde; pub fn serialize(bytes: &T, s: S) -> Result - where T: serde::Serialize + AsRef<[u8]>, S: serde::Serializer + where + T: serde::Serialize + AsRef<[u8]>, S: serde::Serializer { // Don't do anything special when not human readable. if !s.is_human_readable() { @@ -239,7 +243,8 @@ pub mod hex_bytes { } pub fn deserialize<'de, D, B>(d: D) -> Result - where D: serde::Deserializer<'de>, B: serde::Deserialize<'de> + FromHex, + where + D: serde::Deserializer<'de>, B: serde::Deserialize<'de> + FromHex, { struct Visitor(::core::marker::PhantomData); @@ -251,7 +256,8 @@ pub mod hex_bytes { } fn visit_bytes(self, v: &[u8]) -> Result - where E: serde::de::Error, + where + E: serde::de::Error, { if let Ok(hex) = ::core::str::from_utf8(v) { FromHex::from_hex(hex).map_err(E::custom) @@ -261,7 +267,8 @@ pub mod hex_bytes { } fn visit_str(self, v: &str) -> Result - where E: serde::de::Error, + where + E: serde::de::Error, { FromHex::from_hex(v).map_err(E::custom) } diff --git a/src/util/amount.rs b/src/util/amount.rs index 71d264a5..ec814066 100644 --- a/src/util/amount.rs +++ b/src/util/amount.rs @@ -1133,12 +1133,13 @@ pub mod serde { fn visit_none(self) -> Result where - E: de::Error { + E: de::Error, + { Ok(None) } fn visit_some(self, d: D) -> Result where - D: Deserializer<'de> + D: Deserializer<'de>, { Ok(Some(X::des_sat(d)?)) } @@ -1196,7 +1197,8 @@ pub mod serde { fn visit_none(self) -> Result where - E: de::Error { + E: de::Error, + { Ok(None) } fn visit_some(self, d: D) -> Result diff --git a/src/util/hash.rs b/src/util/hash.rs index 268ac15a..5d9f540d 100644 --- a/src/util/hash.rs +++ b/src/util/hash.rs @@ -36,7 +36,8 @@ use consensus::encode::Encodable; /// - `Some(hash)` if `hashes` contains one element. A single hash is by definition the merkle root. /// - `Some(merkle_root)` if length of `hashes` is greater than one. pub fn bitcoin_merkle_root_inline(hashes: &mut [T]) -> Option - where T: Hash + Encodable, +where + T: Hash + Encodable, ::Engine: io::Write, { match hashes.len() { @@ -53,9 +54,10 @@ pub fn bitcoin_merkle_root_inline(hashes: &mut [T]) -> Option /// - `Some(hash)` if `hashes` contains one element. A single hash is by definition the merkle root. /// - `Some(merkle_root)` if length of `hashes` is greater than one. pub fn bitcoin_merkle_root(mut hashes: I) -> Option - where T: Hash + Encodable, - ::Engine: io::Write, - I: Iterator, +where + T: Hash + Encodable, + ::Engine: io::Write, + I: Iterator, { let first = hashes.next()?; let second = match hashes.next() { @@ -84,8 +86,9 @@ pub fn bitcoin_merkle_root(mut hashes: I) -> Option // `hashes` must contain at least one hash. fn merkle_root_r(hashes: &mut [T]) -> T - where T: Hash + Encodable, - ::Engine: io::Write, +where + T: Hash + Encodable, + ::Engine: io::Write, { if hashes.len() == 1 { return hashes[0] diff --git a/src/util/merkleblock.rs b/src/util/merkleblock.rs index a0ff2448..a3e3df3d 100644 --- a/src/util/merkleblock.rs +++ b/src/util/merkleblock.rs @@ -432,7 +432,9 @@ impl MerkleBlock { /// assert_eq!(txid, matches[0]); /// ``` pub fn from_block_with_predicate(block: &Block, match_txids: F) -> Self - where F: Fn(&Txid) -> bool { + where + F: Fn(&Txid) -> bool + { let block_txids: Vec<_> = block.txdata.iter().map(Transaction::txid).collect(); Self::from_header_txids_with_predicate(&block.header, &block_txids, match_txids) } @@ -453,7 +455,10 @@ impl MerkleBlock { header: &BlockHeader, block_txids: &[Txid], match_txids: F, - ) -> Self where F: Fn(&Txid) -> bool { + ) -> Self + where + F: Fn(&Txid) -> bool + { let matches: Vec = block_txids .iter() .map(match_txids) diff --git a/src/util/taproot.rs b/src/util/taproot.rs index f49eabd2..d5f44d4a 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -874,8 +874,8 @@ impl fmt::UpperHex for LeafVersion { #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl ::serde::Serialize for LeafVersion { fn serialize(&self, serializer: S) -> Result - where - S: ::serde::Serializer, + where + S: ::serde::Serializer, { serializer.serialize_u8(self.to_consensus()) } @@ -885,7 +885,10 @@ impl ::serde::Serialize for LeafVersion { #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl<'de> ::serde::Deserialize<'de> for LeafVersion { - fn deserialize(deserializer: D) -> Result where D: ::serde::Deserializer<'de> { + fn deserialize(deserializer: D) -> Result + where + D: ::serde::Deserializer<'de> + { struct U8Visitor; impl<'de> ::serde::de::Visitor<'de> for U8Visitor { type Value = LeafVersion;