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).
This commit is contained in:
Tobin Harding 2022-01-24 11:33:03 +11:00
parent 6d84998168
commit a8ed95ea07
8 changed files with 76 additions and 46 deletions

View File

@ -89,9 +89,8 @@ impl fmt::UpperHex for Script {
impl hex::FromHex for Script { impl hex::FromHex for Script {
fn from_byte_iter<I>(iter: I) -> Result<Self, hex::Error> fn from_byte_iter<I>(iter: I) -> Result<Self, hex::Error>
where I: Iterator<Item=Result<u8, hex::Error>> + where
ExactSizeIterator + I: Iterator<Item=Result<u8, hex::Error>> + ExactSizeIterator + DoubleEndedIterator,
DoubleEndedIterator,
{ {
Vec::from_byte_iter(iter).map(|v| Script(Box::<[u8]>::from(v))) 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")))] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> serde::Deserialize<'de> for Script { impl<'de> serde::Deserialize<'de> for Script {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: serde::Deserializer<'de>, where
D: serde::Deserializer<'de>,
{ {
use core::fmt::Formatter; use core::fmt::Formatter;
use hashes::hex::FromHex; use hashes::hex::FromHex;
@ -965,20 +965,23 @@ impl<'de> serde::Deserialize<'de> for Script {
} }
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where E: serde::de::Error, where
E: serde::de::Error,
{ {
let v = Vec::from_hex(v).map_err(E::custom)?; let v = Vec::from_hex(v).map_err(E::custom)?;
Ok(Script::from(v)) Ok(Script::from(v))
} }
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where E: serde::de::Error, where
E: serde::de::Error,
{ {
self.visit_str(v) self.visit_str(v)
} }
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where E: serde::de::Error, where
E: serde::de::Error,
{ {
self.visit_str(&v) self.visit_str(&v)
} }
@ -995,7 +998,8 @@ impl<'de> serde::Deserialize<'de> for Script {
} }
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where E: serde::de::Error, where
E: serde::de::Error,
{ {
Ok(Script::from(v.to_vec())) Ok(Script::from(v.to_vec()))
} }

View File

@ -504,7 +504,9 @@ impl Transaction {
#[cfg(feature="bitcoinconsensus")] #[cfg(feature="bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))] #[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
pub fn verify<S>(&self, spent: S) -> Result<(), script::Error> pub fn verify<S>(&self, spent: S) -> Result<(), script::Error>
where S: FnMut(&OutPoint) -> Option<TxOut> { where
S: FnMut(&OutPoint) -> Option<TxOut>
{
self.verify_with_flags(spent, ::bitcoinconsensus::VERIFY_ALL) self.verify_with_flags(spent, ::bitcoinconsensus::VERIFY_ALL)
} }
@ -513,7 +515,10 @@ impl Transaction {
#[cfg(feature="bitcoinconsensus")] #[cfg(feature="bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))] #[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
pub fn verify_with_flags<S, F>(&self, mut spent: S, flags: F) -> Result<(), script::Error> pub fn verify_with_flags<S, F>(&self, mut spent: S, flags: F) -> Result<(), script::Error>
where S: FnMut(&OutPoint) -> Option<TxOut>, F : Into<u32> { where
S: FnMut(&OutPoint) -> Option<TxOut>,
F: Into<u32>
{
let tx = encode::serialize(&*self); let tx = encode::serialize(&*self);
let flags: u32 = flags.into(); let flags: u32 = flags.into();
for (idx, input) in self.input.iter().enumerate() { for (idx, input) in self.input.iter().enumerate() {

View File

@ -446,9 +446,10 @@ macro_rules! impl_bytes_newtype {
impl $crate::hashes::hex::FromHex for $t { impl $crate::hashes::hex::FromHex for $t {
fn from_byte_iter<I>(iter: I) -> Result<Self, $crate::hashes::hex::Error> fn from_byte_iter<I>(iter: I) -> Result<Self, $crate::hashes::hex::Error>
where I: ::core::iter::Iterator<Item=Result<u8, $crate::hashes::hex::Error>> + where
::core::iter::ExactSizeIterator + I: ::core::iter::Iterator<Item=Result<u8, $crate::hashes::hex::Error>>
::core::iter::DoubleEndedIterator, + ::core::iter::ExactSizeIterator
+ ::core::iter::DoubleEndedIterator,
{ {
if iter.len() == $len { if iter.len() == $len {
let mut ret = [0; $len]; let mut ret = [0; $len];

View File

@ -13,8 +13,8 @@ pub mod btreemap_byte_values {
use hashes::hex::{FromHex, ToHex}; use hashes::hex::{FromHex, ToHex};
use serde; use serde;
pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S) pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S) -> Result<S::Ok, S::Error>
-> Result<S::Ok, S::Error> where where
S: serde::Serializer, S: serde::Serializer,
T: serde::Serialize + ::core::hash::Hash + Eq + Ord, 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) pub fn deserialize<'de, D, T>(d: D) -> Result<BTreeMap<T, Vec<u8>>, D::Error>
-> Result<BTreeMap<T, Vec<u8>>, D::Error> where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord,
{ {
use ::core::marker::PhantomData; use ::core::marker::PhantomData;
struct Visitor<T>(PhantomData<T>); struct Visitor<T>(PhantomData<T>);
impl<'de, T> serde::de::Visitor<'de> for Visitor<T> where impl<'de, T> serde::de::Visitor<'de> for Visitor<T>
where
T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord,
{ {
type Value = BTreeMap<T, Vec<u8>>; type Value = BTreeMap<T, Vec<u8>>;
@ -79,8 +80,8 @@ pub mod btreemap_as_seq {
use prelude::*; use prelude::*;
use serde; use serde;
pub fn serialize<S, T, U>(v: &BTreeMap<T, U>, s: S) pub fn serialize<S, T, U>(v: &BTreeMap<T, U>, s: S) -> Result<S::Ok, S::Error>
-> Result<S::Ok, S::Error> where where
S: serde::Serializer, S: serde::Serializer,
T: serde::Serialize + ::core::hash::Hash + Eq + Ord, T: serde::Serialize + ::core::hash::Hash + Eq + Ord,
U: serde::Serialize, U: serde::Serialize,
@ -99,8 +100,8 @@ pub mod btreemap_as_seq {
} }
} }
pub fn deserialize<'de, D, T, U>(d: D) pub fn deserialize<'de, D, T, U>(d: D) -> Result<BTreeMap<T, U>, D::Error>
-> Result<BTreeMap<T, U>, D::Error> where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord,
U: serde::Deserialize<'de>, U: serde::Deserialize<'de>,
@ -108,7 +109,8 @@ pub mod btreemap_as_seq {
use ::core::marker::PhantomData; use ::core::marker::PhantomData;
struct Visitor<T, U>(PhantomData<(T, U)>); struct Visitor<T, U>(PhantomData<(T, U)>);
impl<'de, T, U> serde::de::Visitor<'de> for Visitor<T, U> where impl<'de, T, U> serde::de::Visitor<'de> for Visitor<T, U>
where
T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord,
U: serde::Deserialize<'de>, U: serde::Deserialize<'de>,
{ {
@ -164,8 +166,8 @@ pub mod btreemap_as_seq_byte_values {
&'a [u8], &'a [u8],
); );
pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S) pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S) -> Result<S::Ok, S::Error>
-> Result<S::Ok, S::Error> where where
S: serde::Serializer, S: serde::Serializer,
T: serde::Serialize + ::core::hash::Hash + Eq + Ord + 'static, 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) pub fn deserialize<'de, D, T>(d: D) -> Result<BTreeMap<T, Vec<u8>>, D::Error>
-> Result<BTreeMap<T, Vec<u8>>, D::Error> where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord,
{ {
use ::core::marker::PhantomData; use ::core::marker::PhantomData;
struct Visitor<T>(PhantomData<T>); struct Visitor<T>(PhantomData<T>);
impl<'de, T> serde::de::Visitor<'de> for Visitor<T> where impl<'de, T> serde::de::Visitor<'de> for Visitor<T>
where
T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord, T: serde::Deserialize<'de> + ::core::hash::Hash + Eq + Ord,
{ {
type Value = BTreeMap<T, Vec<u8>>; type Value = BTreeMap<T, Vec<u8>>;
@ -228,7 +231,8 @@ pub mod hex_bytes {
use serde; use serde;
pub fn serialize<T, S>(bytes: &T, s: S) -> Result<S::Ok, S::Error> pub fn serialize<T, S>(bytes: &T, s: S) -> Result<S::Ok, S::Error>
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. // Don't do anything special when not human readable.
if !s.is_human_readable() { if !s.is_human_readable() {
@ -239,7 +243,8 @@ pub mod hex_bytes {
} }
pub fn deserialize<'de, D, B>(d: D) -> Result<B, D::Error> pub fn deserialize<'de, D, B>(d: D) -> Result<B, D::Error>
where D: serde::Deserializer<'de>, B: serde::Deserialize<'de> + FromHex, where
D: serde::Deserializer<'de>, B: serde::Deserialize<'de> + FromHex,
{ {
struct Visitor<B>(::core::marker::PhantomData<B>); struct Visitor<B>(::core::marker::PhantomData<B>);
@ -251,7 +256,8 @@ pub mod hex_bytes {
} }
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where E: serde::de::Error, where
E: serde::de::Error,
{ {
if let Ok(hex) = ::core::str::from_utf8(v) { if let Ok(hex) = ::core::str::from_utf8(v) {
FromHex::from_hex(hex).map_err(E::custom) FromHex::from_hex(hex).map_err(E::custom)
@ -261,7 +267,8 @@ pub mod hex_bytes {
} }
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where E: serde::de::Error, where
E: serde::de::Error,
{ {
FromHex::from_hex(v).map_err(E::custom) FromHex::from_hex(v).map_err(E::custom)
} }

View File

@ -1133,12 +1133,13 @@ pub mod serde {
fn visit_none<E>(self) -> Result<Self::Value, E> fn visit_none<E>(self) -> Result<Self::Value, E>
where where
E: de::Error { E: de::Error,
{
Ok(None) Ok(None)
} }
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error> fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where where
D: Deserializer<'de> D: Deserializer<'de>,
{ {
Ok(Some(X::des_sat(d)?)) Ok(Some(X::des_sat(d)?))
} }
@ -1196,7 +1197,8 @@ pub mod serde {
fn visit_none<E>(self) -> Result<Self::Value, E> fn visit_none<E>(self) -> Result<Self::Value, E>
where where
E: de::Error { E: de::Error,
{
Ok(None) Ok(None)
} }
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error> fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>

View File

@ -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(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. /// - `Some(merkle_root)` if length of `hashes` is greater than one.
pub fn bitcoin_merkle_root_inline<T>(hashes: &mut [T]) -> Option<T> pub fn bitcoin_merkle_root_inline<T>(hashes: &mut [T]) -> Option<T>
where T: Hash + Encodable, where
T: Hash + Encodable,
<T as Hash>::Engine: io::Write, <T as Hash>::Engine: io::Write,
{ {
match hashes.len() { match hashes.len() {
@ -53,7 +54,8 @@ pub fn bitcoin_merkle_root_inline<T>(hashes: &mut [T]) -> Option<T>
/// - `Some(hash)` if `hashes` contains one element. A single hash is by definition the merkle root. /// - `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. /// - `Some(merkle_root)` if length of `hashes` is greater than one.
pub fn bitcoin_merkle_root<T, I>(mut hashes: I) -> Option<T> pub fn bitcoin_merkle_root<T, I>(mut hashes: I) -> Option<T>
where T: Hash + Encodable, where
T: Hash + Encodable,
<T as Hash>::Engine: io::Write, <T as Hash>::Engine: io::Write,
I: Iterator<Item = T>, I: Iterator<Item = T>,
{ {
@ -84,7 +86,8 @@ pub fn bitcoin_merkle_root<T, I>(mut hashes: I) -> Option<T>
// `hashes` must contain at least one hash. // `hashes` must contain at least one hash.
fn merkle_root_r<T>(hashes: &mut [T]) -> T fn merkle_root_r<T>(hashes: &mut [T]) -> T
where T: Hash + Encodable, where
T: Hash + Encodable,
<T as Hash>::Engine: io::Write, <T as Hash>::Engine: io::Write,
{ {
if hashes.len() == 1 { if hashes.len() == 1 {

View File

@ -432,7 +432,9 @@ impl MerkleBlock {
/// assert_eq!(txid, matches[0]); /// assert_eq!(txid, matches[0]);
/// ``` /// ```
pub fn from_block_with_predicate<F>(block: &Block, match_txids: F) -> Self pub fn from_block_with_predicate<F>(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(); let block_txids: Vec<_> = block.txdata.iter().map(Transaction::txid).collect();
Self::from_header_txids_with_predicate(&block.header, &block_txids, match_txids) Self::from_header_txids_with_predicate(&block.header, &block_txids, match_txids)
} }
@ -453,7 +455,10 @@ impl MerkleBlock {
header: &BlockHeader, header: &BlockHeader,
block_txids: &[Txid], block_txids: &[Txid],
match_txids: F, match_txids: F,
) -> Self where F: Fn(&Txid) -> bool { ) -> Self
where
F: Fn(&Txid) -> bool
{
let matches: Vec<bool> = block_txids let matches: Vec<bool> = block_txids
.iter() .iter()
.map(match_txids) .map(match_txids)

View File

@ -885,7 +885,10 @@ impl ::serde::Serialize for LeafVersion {
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> ::serde::Deserialize<'de> for LeafVersion { impl<'de> ::serde::Deserialize<'de> for LeafVersion {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: ::serde::Deserializer<'de> { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>
{
struct U8Visitor; struct U8Visitor;
impl<'de> ::serde::de::Visitor<'de> for U8Visitor { impl<'de> ::serde::de::Visitor<'de> for U8Visitor {
type Value = LeafVersion; type Value = LeafVersion;