Skip to content

Commit 628458d

Browse files
committed
Use less vertical lines
In this library we specifically do not use rustfmt and tend to favour terse statements that do not use extra lines unnecessarily. In order to help new devs understand the style modify code that seems to use an unnecessary number of lines. None of these changes should reduce the readability of the code.
1 parent e418117 commit 628458d

File tree

15 files changed

+60
-196
lines changed

15 files changed

+60
-196
lines changed

src/blockdata/script.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,38 +1355,19 @@ mod test {
13551355
let slop_v_nonmin: Result<Vec<Instruction>, Error> = nonminimal.instructions().collect();
13561356
let slop_v_nonmin_alt: Result<Vec<Instruction>, Error> = nonminimal_alt.instructions().collect();
13571357

1358-
assert_eq!(
1359-
v_zero.unwrap(),
1360-
vec![
1361-
Instruction::PushBytes(&[]),
1362-
]
1363-
);
1364-
assert_eq!(
1365-
v_zeropush.unwrap(),
1366-
vec![
1367-
Instruction::PushBytes(&[0]),
1368-
]
1369-
);
1358+
assert_eq!(v_zero.unwrap(), vec![Instruction::PushBytes(&[])]);
1359+
assert_eq!(v_zeropush.unwrap(), vec![Instruction::PushBytes(&[0])]);
13701360

13711361
assert_eq!(
13721362
v_min.clone().unwrap(),
1373-
vec![
1374-
Instruction::PushBytes(&[105]),
1375-
Instruction::Op(opcodes::OP_NOP3),
1376-
]
1363+
vec![Instruction::PushBytes(&[105]), Instruction::Op(opcodes::OP_NOP3)]
13771364
);
13781365

1379-
assert_eq!(
1380-
v_nonmin.err().unwrap(),
1381-
Error::NonMinimalPush
1382-
);
1366+
assert_eq!(v_nonmin.err().unwrap(), Error::NonMinimalPush);
13831367

13841368
assert_eq!(
13851369
v_nonmin_alt.clone().unwrap(),
1386-
vec![
1387-
Instruction::PushBytes(&[105, 0]),
1388-
Instruction::Op(opcodes::OP_NOP3),
1389-
]
1370+
vec![Instruction::PushBytes(&[105, 0]), Instruction::Op(opcodes::OP_NOP3)]
13901371
);
13911372

13921373
assert_eq!(v_min.clone().unwrap(), slop_v_min.unwrap());

src/blockdata/transaction.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ impl OutPoint {
5959
/// Creates a new [`OutPoint`].
6060
#[inline]
6161
pub fn new(txid: Txid, vout: u32) -> OutPoint {
62-
OutPoint {
63-
txid,
64-
vout,
65-
}
62+
OutPoint { txid, vout }
6663
}
6764

6865
/// Creates a "null" `OutPoint`.
@@ -639,9 +636,7 @@ impl Decodable for Transaction {
639636
}
640637
}
641638
// We don't support anything else
642-
x => {
643-
Err(encode::Error::UnsupportedSegwitFlag(x))
644-
}
639+
x => Err(encode::Error::UnsupportedSegwitFlag(x)),
645640
}
646641
// non-segwit
647642
} else {

src/blockdata/witness.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,8 @@ impl Witness {
202202
self.last = self.content.len();
203203
let element_len_varint = VarInt(new_element.len() as u64);
204204
let current_content_len = self.content.len();
205-
self.content.resize(
206-
current_content_len + element_len_varint.len() + new_element.len(),
207-
0,
208-
);
205+
self.content
206+
.resize(current_content_len + element_len_varint.len() + new_element.len(), 0);
209207
let end_varint = current_content_len + element_len_varint.len();
210208
element_len_varint
211209
.consensus_encode(&mut self.content[current_content_len..end_varint])
@@ -359,14 +357,9 @@ mod test {
359357
for (i, wit_el) in tx.input[0].witness.iter().enumerate() {
360358
assert_eq!(expected_wit[i], wit_el.to_hex());
361359
}
362-
assert_eq!(
363-
expected_wit[1],
364-
tx.input[0].witness.last().unwrap().to_hex()
365-
);
366-
assert_eq!(
367-
expected_wit[0],
368-
tx.input[0].witness.second_to_last().unwrap().to_hex()
369-
);
360+
assert_eq!(expected_wit[1], tx.input[0].witness.last().unwrap().to_hex());
361+
assert_eq!(expected_wit[0], tx.input[0].witness.second_to_last().unwrap().to_hex());
362+
370363
let tx_bytes_back = serialize(&tx);
371364
assert_eq!(tx_bytes_back, tx_bytes);
372365
}

src/consensus/encode.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ pub fn deserialize<T: Decodable>(data: &[u8]) -> Result<T, Error> {
164164

165165
/// Deserialize an object from a vector, but will not report an error if said deserialization
166166
/// doesn't consume the entire vector.
167-
pub fn deserialize_partial<T: Decodable>(
168-
data: &[u8],
169-
) -> Result<(T, usize), Error> {
167+
pub fn deserialize_partial<T: Decodable>(data: &[u8]) -> Result<(T, usize), Error> {
170168
let mut decoder = Cursor::new(data);
171169
let rv = Decodable::consensus_decode(&mut decoder)?;
172170
let consumed = decoder.position() as usize;

src/network/address.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ impl Address {
5858
if addr[0..3] == ONION {
5959
return Err(io::Error::from(io::ErrorKind::AddrNotAvailable));
6060
}
61-
let ipv6 = Ipv6Addr::new(
62-
addr[0],addr[1],addr[2],addr[3],
63-
addr[4],addr[5],addr[6],addr[7]
64-
);
61+
let ipv6 = Ipv6Addr::new(addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
6562
if let Some(ipv4) = ipv6.to_ipv4() {
6663
Ok(SocketAddr::V4(SocketAddrV4::new(ipv4, self.port)))
6764
} else {
@@ -190,10 +187,7 @@ impl Decodable for AddrV2 {
190187
if addr[0..6] == [0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF] {
191188
return Err(encode::Error::ParseFailed("IPV4 wrapped address sent with IPv6 network id"));
192189
}
193-
AddrV2::Ipv6(Ipv6Addr::new(
194-
addr[0],addr[1],addr[2],addr[3],
195-
addr[4],addr[5],addr[6],addr[7]
196-
))
190+
AddrV2::Ipv6(Ipv6Addr::new(addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]))
197191
},
198192
3 => {
199193
if len != 10 {
@@ -226,10 +220,7 @@ impl Decodable for AddrV2 {
226220
return Err(encode::Error::ParseFailed("Invalid CJDNS address"));
227221
}
228222
let addr = addr_to_be(addr);
229-
AddrV2::Cjdns(Ipv6Addr::new(
230-
addr[0],addr[1],addr[2],addr[3],
231-
addr[4],addr[5],addr[6],addr[7]
232-
))
223+
AddrV2::Cjdns(Ipv6Addr::new(addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]))
233224
},
234225
_ => {
235226
// len already checked above to be <= 512

src/network/constants.rs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -293,39 +293,16 @@ mod tests {
293293

294294
#[test]
295295
fn serialize_test() {
296-
assert_eq!(
297-
serialize(&Network::Bitcoin.magic()),
298-
&[0xf9, 0xbe, 0xb4, 0xd9]
299-
);
300-
assert_eq!(
301-
serialize(&Network::Testnet.magic()),
302-
&[0x0b, 0x11, 0x09, 0x07]
303-
);
304-
assert_eq!(
305-
serialize(&Network::Signet.magic()),
306-
&[0x0a, 0x03, 0xcf, 0x40]
307-
);
308-
assert_eq!(
309-
serialize(&Network::Regtest.magic()),
310-
&[0xfa, 0xbf, 0xb5, 0xda]
311-
);
312-
313-
assert_eq!(
314-
deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(),
315-
Some(Network::Bitcoin.magic())
316-
);
317-
assert_eq!(
318-
deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(),
319-
Some(Network::Testnet.magic())
320-
);
321-
assert_eq!(
322-
deserialize(&[0x0a, 0x03, 0xcf, 0x40]).ok(),
323-
Some(Network::Signet.magic())
324-
);
325-
assert_eq!(
326-
deserialize(&[0xfa, 0xbf, 0xb5, 0xda]).ok(),
327-
Some(Network::Regtest.magic())
328-
);
296+
assert_eq!(serialize(&Network::Bitcoin.magic()), &[0xf9, 0xbe, 0xb4, 0xd9]);
297+
assert_eq!(serialize(&Network::Testnet.magic()), &[0x0b, 0x11, 0x09, 0x07]);
298+
assert_eq!(serialize(&Network::Signet.magic()), &[0x0a, 0x03, 0xcf, 0x40]);
299+
assert_eq!(serialize(&Network::Regtest.magic()), &[0xfa, 0xbf, 0xb5, 0xda]);
300+
301+
assert_eq!(deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(), Some(Network::Bitcoin.magic()));
302+
assert_eq!(deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(), Some(Network::Testnet.magic()));
303+
assert_eq!(deserialize(&[0x0a, 0x03, 0xcf, 0x40]).ok(), Some(Network::Signet.magic()));
304+
assert_eq!(deserialize(&[0xfa, 0xbf, 0xb5, 0xda]).ok(), Some(Network::Regtest.magic()));
305+
329306
}
330307

331308
#[test]

src/network/message_blockdata.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ impl Encodable for Inventory {
5757
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
5858
macro_rules! encode_inv {
5959
($code:expr, $item:expr) => {
60-
u32::consensus_encode(&$code, &mut s)? +
61-
$item.consensus_encode(&mut s)?
60+
u32::consensus_encode(&$code, &mut s)? + $item.consensus_encode(&mut s)?
6261
}
6362
}
6463
Ok(match *self {

src/util/address.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,10 @@ impl fmt::Display for Error {
9494
Error::InvalidWitnessVersion(v) => write!(f, "invalid witness script version: {}", v),
9595
Error::UnparsableWitnessVersion(_) => write!(f, "incorrect format of a witness version byte"),
9696
Error::MalformedWitnessVersion => f.write_str("bitcoin script opcode does not match any known witness version, the script is malformed"),
97-
Error::InvalidWitnessProgramLength(l) => write!(f,
98-
"the witness program must be between 2 and 40 bytes in length: length={}", l,
99-
),
100-
Error::InvalidSegwitV0ProgramLength(l) => write!(f,
101-
"a v0 witness program must be either of length 20 or 32 bytes: length={}", l,
102-
),
103-
Error::UncompressedPubkey => write!(f,
104-
"an uncompressed pubkey was used where it is not allowed",
105-
),
106-
Error::ExcessiveScriptSize => write!(f,
107-
"Script size exceed 520 bytes")
97+
Error::InvalidWitnessProgramLength(l) => write!(f, "the witness program must be between 2 and 40 bytes in length: length={}", l),
98+
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l),
99+
Error::UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"),
100+
Error::ExcessiveScriptSize => write!(f, "Script size exceed 520 bytes"),
108101
}
109102
}
110103
}
@@ -395,14 +388,11 @@ impl Payload {
395388
/// Generates a script pubkey spending to this [Payload].
396389
pub fn script_pubkey(&self) -> script::Script {
397390
match *self {
398-
Payload::PubkeyHash(ref hash) =>
399-
script::Script::new_p2pkh(hash),
400-
Payload::ScriptHash(ref hash) =>
401-
script::Script::new_p2sh(hash),
402-
Payload::WitnessProgram {
403-
version,
404-
program: ref prog,
405-
} => script::Script::new_witness_program(version, prog)
391+
Payload::PubkeyHash(ref hash) => script::Script::new_p2pkh(hash),
392+
Payload::ScriptHash(ref hash) => script::Script::new_p2sh(hash),
393+
Payload::WitnessProgram { version, program: ref prog } => {
394+
script::Script::new_witness_program(version, prog)
395+
}
406396
}
407397
}
408398

@@ -622,10 +612,7 @@ impl Address {
622612
/// Creates a pay to taproot address from a pre-tweaked output key.
623613
///
624614
/// This method is not recommended for use, [`Address::p2tr()`] should be used where possible.
625-
pub fn p2tr_tweaked(
626-
output_key: TweakedPublicKey,
627-
network: Network
628-
) -> Address {
615+
pub fn p2tr_tweaked(output_key: TweakedPublicKey, network: Network) -> Address {
629616
Address {
630617
network,
631618
payload: Payload::p2tr_tweaked(output_key),

src/util/amount.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -961,10 +961,7 @@ impl<T> CheckedSum<SignedAmount> for T where T: Iterator<Item = SignedAmount> {
961961
fn checked_sum(mut self) -> Option<SignedAmount> {
962962
let first = Some(self.next().unwrap_or_default());
963963

964-
self.fold(
965-
first,
966-
|acc, item| acc.and_then(|acc| acc.checked_add(item))
967-
)
964+
self.fold(first, |acc, item| acc.and_then(|acc| acc.checked_add(item)))
968965
}
969966
}
970967

@@ -1534,10 +1531,7 @@ mod tests {
15341531
samt: SignedAmount::from_sat(-123456789),
15351532
},
15361533
&[
1537-
serde_test::Token::Struct {
1538-
name: "T",
1539-
len: 2,
1540-
},
1534+
serde_test::Token::Struct { name: "T", len: 2 },
15411535
serde_test::Token::Str("amt"),
15421536
serde_test::Token::U64(123456789),
15431537
serde_test::Token::Str("samt"),

src/util/bip32.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -913,23 +913,14 @@ mod tests {
913913
let mut pk = ExtendedPubKey::from_priv(secp, &sk);
914914

915915
// Check derivation convenience method for ExtendedPrivKey
916-
assert_eq!(
917-
&sk.derive_priv(secp, &path).unwrap().to_string()[..],
918-
expected_sk
919-
);
916+
assert_eq!(&sk.derive_priv(secp, &path).unwrap().to_string()[..], expected_sk);
920917

921918
// Check derivation convenience method for ExtendedPubKey, should error
922919
// appropriately if any ChildNumber is hardened
923920
if path.0.iter().any(|cnum| cnum.is_hardened()) {
924-
assert_eq!(
925-
pk.derive_pub(secp, &path),
926-
Err(Error::CannotDeriveFromHardenedKey)
927-
);
921+
assert_eq!(pk.derive_pub(secp, &path), Err(Error::CannotDeriveFromHardenedKey));
928922
} else {
929-
assert_eq!(
930-
&pk.derive_pub(secp, &path).unwrap().to_string()[..],
931-
expected_pk
932-
);
923+
assert_eq!(&pk.derive_pub(secp, &path).unwrap().to_string()[..], expected_pk);
933924
}
934925

935926
// Derive keys, checking hardened and non-hardened derivation one-by-one

0 commit comments

Comments
 (0)