Skip to content

Commit

Permalink
Enable formatter for "src"
Browse files Browse the repository at this point in the history
Remove the ignore for the "src" directory, this enables the formatter
for all source files in the `src/` directory.
  • Loading branch information
tcharding committed Jun 23, 2022
1 parent 690de3f commit b45060f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 93 deletions.
1 change: 0 additions & 1 deletion rustfmt.toml
@@ -1,6 +1,5 @@
# Eventually this shoud be: ignore = []
ignore = [
"src",
"src/blockdata",
"src/consensus",
"src/network",
Expand Down
3 changes: 2 additions & 1 deletion src/hash_types.rs
Expand Up @@ -20,6 +20,7 @@
//! hash).
//!

#[rustfmt::skip]
macro_rules! impl_hashencode {
($hashtype:ident) => {
impl $crate::consensus::Encodable for $hashtype {
Expand All @@ -34,7 +35,7 @@ macro_rules! impl_hashencode {
Ok(Self::from_inner(<<$hashtype as $crate::hashes::Hash>::Inner>::consensus_decode(d)?))
}
}
}
};
}

// newtypes module is solely here so we can rustfmt::skip.
Expand Down
19 changes: 8 additions & 11 deletions src/internal_macros.rs
Expand Up @@ -110,11 +110,9 @@ macro_rules! impl_array_newtype {
type Output = <[$ty] as core::ops::Index<I>>::Output;

#[inline]
fn index(&self, index: I) -> &Self::Output {
&self.0[index]
}
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
}
}
};
}

macro_rules! display_from_debug {
Expand All @@ -124,7 +122,7 @@ macro_rules! display_from_debug {
core::fmt::Debug::fmt(self, f)
}
}
}
};
}

#[cfg(test)]
Expand Down Expand Up @@ -362,8 +360,7 @@ macro_rules! serde_struct_human_string_impl {
/// - core::str::FromStr
/// - hashes::hex::FromHex
macro_rules! impl_bytes_newtype {
($t:ident, $len:expr) => (

($t:ident, $len:expr) => {
impl core::fmt::LowerHex for $t {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
for &ch in self.0.iter() {
Expand All @@ -388,9 +385,9 @@ macro_rules! impl_bytes_newtype {
impl $crate::hashes::hex::FromHex for $t {
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>>
+ core::iter::ExactSizeIterator
+ core::iter::DoubleEndedIterator,
I: core::iter::Iterator<Item = Result<u8, $crate::hashes::hex::Error>>
+ core::iter::ExactSizeIterator
+ core::iter::DoubleEndedIterator,
{
if iter.len() == $len {
let mut ret = [0; $len];
Expand Down Expand Up @@ -487,7 +484,7 @@ macro_rules! impl_bytes_newtype {
}
}
}
)
};
}

macro_rules! user_enum {
Expand Down
104 changes: 44 additions & 60 deletions src/lib.rs
Expand Up @@ -41,12 +41,9 @@
//!

#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]

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

#![cfg_attr(docsrs, feature(doc_cfg))]

// Coding conventions
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
Expand Down Expand Up @@ -78,9 +75,10 @@ extern crate core2;
extern crate core; // for Rust 1.29 and no-std tests

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

#[cfg(feature = "no-std")]
extern crate hashbrown;
Expand All @@ -89,12 +87,19 @@ extern crate hashbrown;
#[cfg_attr(docsrs, doc(cfg(feature = "base64")))]
pub extern crate base64;

#[cfg(feature="bitcoinconsensus")] extern crate bitcoinconsensus;
#[cfg(feature = "serde")] #[macro_use] extern crate actual_serde as serde;
#[cfg(all(test, feature = "serde"))] extern crate serde_json;
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
#[cfg(all(test, feature = "serde"))] extern crate bincode;
#[cfg(all(test, feature = "unstable"))] extern crate test;
#[cfg(feature = "bitcoinconsensus")]
extern crate bitcoinconsensus;
#[cfg(feature = "serde")]
#[macro_use]
extern crate actual_serde as serde;
#[cfg(all(test, feature = "serde"))]
extern crate bincode;
#[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(test)]
#[macro_use]
Expand All @@ -107,43 +112,34 @@ mod serde_utils;
#[macro_use]
pub mod network;
pub mod blockdata;
pub mod util;
pub mod consensus;
pub mod hash_types;
pub mod policy;
pub mod util;

pub use crate::hash_types::*;
pub use crate::blockdata::block::Block;
pub use crate::blockdata::block::BlockHeader;
#[cfg(feature = "std")]
use std::io;

#[cfg(not(feature = "std"))]
use core2::io;

pub use crate::blockdata::block::{Block, BlockHeader};
pub use crate::blockdata::script::Script;
pub use crate::blockdata::transaction::Transaction;
pub use crate::blockdata::transaction::TxIn;
pub use crate::blockdata::transaction::TxOut;
pub use crate::blockdata::transaction::OutPoint;
pub use crate::blockdata::transaction::EcdsaSighashType;
#[allow(deprecated)]
pub use crate::blockdata::transaction::SigHashType;
pub use crate::blockdata::transaction::{EcdsaSighashType, OutPoint, Transaction, TxIn, TxOut};
pub use crate::blockdata::witness::Witness;
pub use crate::consensus::encode::VarInt;
pub use crate::hash_types::*;
pub use crate::network::constants::Network;
pub use crate::util::Error;
pub use crate::util::address::Address;
pub use crate::util::address::AddressType;
pub use crate::util::amount::Amount;
pub use crate::util::amount::Denomination;
pub use crate::util::amount::SignedAmount;
pub use crate::util::merkleblock::MerkleBlock;
pub use crate::util::sighash::SchnorrSighashType;

pub use crate::util::address::{Address, AddressType};
pub use crate::util::amount::{Amount, Denomination, SignedAmount};
pub use crate::util::ecdsa::{self, EcdsaSig, EcdsaSigError};
pub use crate::util::key::{KeyPair, PrivateKey, PublicKey, XOnlyPublicKey};
pub use crate::util::merkleblock::MerkleBlock;
pub use crate::util::schnorr::{self, SchnorrSig, SchnorrSigError};
pub use crate::util::key::{PrivateKey, PublicKey, XOnlyPublicKey, KeyPair};
pub use crate::util::psbt;
#[allow(deprecated)]
pub use crate::blockdata::transaction::SigHashType;

#[cfg(feature = "std")]
use std::io;
#[cfg(not(feature = "std"))]
use core2::io;
pub use crate::util::sighash::SchnorrSighashType;
pub use crate::util::{psbt, Error};

#[cfg(not(feature = "std"))]
mod io_extras {
Expand All @@ -153,20 +149,14 @@ mod io_extras {
}

/// Creates an instance of a writer which will successfully consume all data.
pub const fn sink() -> Sink {
Sink { _priv: () }
}
pub const fn sink() -> Sink { Sink { _priv: () } }

impl core2::io::Write for Sink {
#[inline]
fn write(&mut self, buf: &[u8]) -> core2::io::Result<usize> {
Ok(buf.len())
}
fn write(&mut self, buf: &[u8]) -> core2::io::Result<usize> { Ok(buf.len()) }

#[inline]
fn flush(&mut self) -> core2::io::Result<()> {
Ok(())
}
fn flush(&mut self) -> core2::io::Result<()> { Ok(()) }
}
}

Expand Down Expand Up @@ -197,32 +187,26 @@ mod prelude {
pub use std::collections::HashSet;
}

#[cfg(all(test, feature = "unstable"))] use tests::EmptyWrite;
#[cfg(all(test, feature = "unstable"))]
use tests::EmptyWrite;

#[cfg(all(test, feature = "unstable"))]
mod tests {
use core::fmt::Arguments;

use crate::io::{IoSlice, Result, Write};

#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct EmptyWrite;

impl Write for EmptyWrite {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
Ok(buf.len())
}
fn write(&mut self, buf: &[u8]) -> Result<usize> { Ok(buf.len()) }
fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize> {
Ok(bufs.iter().map(|s| s.len()).sum())
}
fn flush(&mut self) -> Result<()> {
Ok(())
}
fn flush(&mut self) -> Result<()> { Ok(()) }

fn write_all(&mut self, _: &[u8]) -> Result<()> {
Ok(())
}
fn write_fmt(&mut self, _: Arguments) -> Result<()> {
Ok(())
}
fn write_all(&mut self, _: &[u8]) -> Result<()> { Ok(()) }
fn write_fmt(&mut self, _: Arguments) -> Result<()> { Ok(()) }
}
}
3 changes: 2 additions & 1 deletion src/policy.rs
Expand Up @@ -24,9 +24,10 @@
//! These values were taken from bitcoind v0.21.1 (194b9b8792d9b0798fdb570b79fa51f1d1f5ebaf).
//!

use super::blockdata::constants::{MAX_BLOCK_SIGOPS_COST, WITNESS_SCALE_FACTOR};
use core::cmp;

use super::blockdata::constants::{MAX_BLOCK_SIGOPS_COST, WITNESS_SCALE_FACTOR};

/// Maximum weight of a transaction for it to be relayed by most nodes on the network
pub const MAX_STANDARD_TX_WEIGHT: u32 = 400_000;

Expand Down
44 changes: 25 additions & 19 deletions src/serde_utils.rs
Expand Up @@ -9,10 +9,11 @@ pub mod btreemap_byte_values {

// NOTE: This module can be exactly copied to use with HashMap.

use crate::prelude::*;
use crate::hashes::hex::{FromHex, ToHex};
use serde;

use crate::hashes::hex::{FromHex, ToHex};
use crate::prelude::*;

pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand Down Expand Up @@ -50,9 +51,10 @@ pub mod btreemap_byte_values {
write!(f, "a map with hexadecimal values")
}

fn visit_map<A: serde::de::MapAccess<'de>>(self, mut a: A)
-> Result<Self::Value, A::Error>
{
fn visit_map<A: serde::de::MapAccess<'de>>(
self,
mut a: A,
) -> Result<Self::Value, A::Error> {
let mut ret = BTreeMap::new();
while let Some((key, value)) = a.next_entry()? {
ret.insert(key, FromHex::from_hex(value).map_err(serde::de::Error::custom)?);
Expand All @@ -77,9 +79,10 @@ pub mod btreemap_as_seq {

// NOTE: This module can be exactly copied to use with HashMap.

use crate::prelude::*;
use serde;

use crate::prelude::*;

pub fn serialize<S, T, U>(v: &BTreeMap<T, U>, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand Down Expand Up @@ -120,9 +123,10 @@ pub mod btreemap_as_seq {
write!(f, "a sequence of pairs")
}

fn visit_seq<A: serde::de::SeqAccess<'de>>(self, mut a: A)
-> Result<Self::Value, A::Error>
{
fn visit_seq<A: serde::de::SeqAccess<'de>>(
self,
mut a: A,
) -> Result<Self::Value, A::Error> {
let mut ret = BTreeMap::new();
while let Some((key, value)) = a.next_element()? {
ret.insert(key, value);
Expand All @@ -147,25 +151,24 @@ pub mod btreemap_as_seq_byte_values {

// NOTE: This module can be exactly copied to use with HashMap.

use crate::prelude::*;
use serde;

use crate::prelude::*;

/// A custom key-value pair type that serialized the bytes as hex.
#[derive(Debug, Deserialize)]
#[serde(crate = "actual_serde")]
struct OwnedPair<T>(
T,
#[serde(deserialize_with = "crate::serde_utils::hex_bytes::deserialize")]
Vec<u8>,
#[serde(deserialize_with = "crate::serde_utils::hex_bytes::deserialize")] Vec<u8>,
);

/// A custom key-value pair type that serialized the bytes as hex.
#[derive(Debug, Serialize)]
#[serde(crate = "actual_serde")]
struct BorrowedPair<'a, T: 'static>(
&'a T,
#[serde(serialize_with = "crate::serde_utils::hex_bytes::serialize")]
&'a [u8],
#[serde(serialize_with = "crate::serde_utils::hex_bytes::serialize")] &'a [u8],
);

pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -205,9 +208,10 @@ pub mod btreemap_as_seq_byte_values {
write!(f, "a sequence of pairs")
}

fn visit_seq<A: serde::de::SeqAccess<'de>>(self, mut a: A)
-> Result<Self::Value, A::Error>
{
fn visit_seq<A: serde::de::SeqAccess<'de>>(
self,
mut a: A,
) -> Result<Self::Value, A::Error> {
let mut ret = BTreeMap::new();
while let Option::Some(OwnedPair(key, value)) = a.next_element()? {
ret.insert(key, value);
Expand All @@ -234,7 +238,8 @@ pub mod hex_bytes {

pub fn serialize<T, S>(bytes: &T, s: S) -> Result<S::Ok, S::Error>
where
T: serde::Serialize + AsRef<[u8]>, S: serde::Serializer
T: serde::Serialize + AsRef<[u8]>,
S: serde::Serializer,
{
// Don't do anything special when not human readable.
if !s.is_human_readable() {
Expand All @@ -246,7 +251,8 @@ pub mod hex_bytes {

pub fn deserialize<'de, D, B>(d: D) -> Result<B, D::Error>
where
D: serde::Deserializer<'de>, B: serde::Deserialize<'de> + FromHex,
D: serde::Deserializer<'de>,
B: serde::Deserialize<'de> + FromHex,
{
struct Visitor<B>(core::marker::PhantomData<B>);

Expand Down

0 comments on commit b45060f

Please sign in to comment.