Skip to content

Commit 229a712

Browse files
committed
rustfmt: Prepare util/ser_macros.rs
1 parent 437528b commit 229a712

File tree

1 file changed

+55
-38
lines changed

1 file changed

+55
-38
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ macro_rules! _check_encoded_tlv_order {
8585
($last_type: expr, $type: expr, (static_value, $value: expr)) => { };
8686
($last_type: expr, $type: expr, $fieldty: tt) => {
8787
if let Some(t) = $last_type {
88-
#[allow(unused_comparisons)] // Note that $type may be 0 making the following comparison always false
88+
// Note that $type may be 0 making the following comparison always false
89+
#[allow(unused_comparisons)]
8990
(debug_assert!(t < $type))
9091
}
9192
$last_type = Some($type);
@@ -196,7 +197,8 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
196197
$len.0 += field_len;
197198
};
198199
($len: expr, $type: expr, $field: expr, required_vec) => {
199-
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $crate::util::ser::WithoutLength(&$field), required);
200+
let field = $crate::util::ser::WithoutLength(&$field);
201+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, field, required);
200202
};
201203
($len: expr, $optional_type: expr, $optional_field: expr, option) => {
202204
if let Some(ref field) = $optional_field {
@@ -215,7 +217,8 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
215217
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
216218
};
217219
($len: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
218-
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field.map(|f| $encoding(f)), option);
220+
let field = $field.map(|f| $encoding(f));
221+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, field, option);
219222
};
220223
($len: expr, $type: expr, $field: expr, upgradable_required) => {
221224
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
@@ -260,7 +263,8 @@ macro_rules! _encode_varint_length_prefixed_tlv {
260263
#[macro_export]
261264
macro_rules! _check_decoded_tlv_order {
262265
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{
263-
#[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
266+
// Note that $type may be 0 making the second comparison always false
267+
#[allow(unused_comparisons)]
264268
let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
265269
if invalid_order {
266270
$field = $default.into();
@@ -269,7 +273,8 @@ macro_rules! _check_decoded_tlv_order {
269273
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (static_value, $value: expr)) => {
270274
};
271275
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => {{
272-
#[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
276+
// Note that $type may be 0 making the second comparison always false
277+
#[allow(unused_comparisons)]
273278
let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
274279
if invalid_order {
275280
return Err(DecodeError::InvalidValue);
@@ -313,7 +318,8 @@ macro_rules! _check_decoded_tlv_order {
313318
#[macro_export]
314319
macro_rules! _check_missing_tlv {
315320
($last_seen_type: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{
316-
#[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
321+
// Note that $type may be 0 making the second comparison always false
322+
#[allow(unused_comparisons)]
317323
let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type;
318324
if missing_req_type {
319325
$field = $default.into();
@@ -323,7 +329,8 @@ macro_rules! _check_missing_tlv {
323329
$field = $value;
324330
};
325331
($last_seen_type: expr, $type: expr, $field: ident, required) => {{
326-
#[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
332+
// Note that $type may be 0 making the second comparison always false
333+
#[allow(unused_comparisons)]
327334
let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type;
328335
if missing_req_type {
329336
return Err(DecodeError::InvalidValue);
@@ -1339,53 +1346,59 @@ mod tests {
13391346
#[test]
13401347
fn tlv_v_short_read() {
13411348
// We only expect a u32 for type 3 (which we are given), but the L says its 8 bytes.
1342-
if let Err(DecodeError::ShortRead) = tlv_reader(&<Vec<u8>>::from_hex(
1343-
concat!("0100", "0208deadbeef1badbeef", "0308deadbeef")
1344-
).unwrap()[..]) {
1349+
let buf = <Vec<u8>>::from_hex(
1350+
concat!("0100", "0208deadbeef1badbeef", "0308deadbeef")
1351+
).unwrap();
1352+
if let Err(DecodeError::ShortRead) = tlv_reader(&buf[..]) {
13451353
} else { panic!(); }
13461354
}
13471355

13481356
#[test]
13491357
fn tlv_types_out_of_order() {
1350-
if let Err(DecodeError::InvalidValue) = tlv_reader(&<Vec<u8>>::from_hex(
1351-
concat!("0100", "0304deadbeef", "0208deadbeef1badbeef")
1352-
).unwrap()[..]) {
1358+
let buf = <Vec<u8>>::from_hex(concat!("0100", "0304deadbeef", "0208deadbeef1badbeef")).unwrap();
1359+
if let Err(DecodeError::InvalidValue) = tlv_reader(&buf[..]) {
13531360
} else { panic!(); }
13541361
// ...even if its some field we don't understand
1355-
if let Err(DecodeError::InvalidValue) = tlv_reader(&<Vec<u8>>::from_hex(
1356-
concat!("0208deadbeef1badbeef", "0100", "0304deadbeef")
1357-
).unwrap()[..]) {
1362+
let buf = <Vec<u8>>::from_hex(
1363+
concat!("0208deadbeef1badbeef", "0100", "0304deadbeef")
1364+
).unwrap();
1365+
if let Err(DecodeError::InvalidValue) = tlv_reader(&buf[..]) {
13581366
} else { panic!(); }
13591367
}
13601368

13611369
#[test]
13621370
fn tlv_req_type_missing_or_extra() {
13631371
// It's also bad if they included even fields we don't understand
1364-
if let Err(DecodeError::UnknownRequiredFeature) = tlv_reader(&<Vec<u8>>::from_hex(
1372+
let buf = <Vec<u8>>::from_hex(
13651373
concat!("0100", "0208deadbeef1badbeef", "0304deadbeef", "0600")
1366-
).unwrap()[..]) {
1374+
).unwrap();
1375+
if let Err(DecodeError::UnknownRequiredFeature) = tlv_reader(&buf[..]) {
13671376
} else { panic!(); }
13681377
// ... or if they're missing fields we need
1369-
if let Err(DecodeError::InvalidValue) = tlv_reader(&<Vec<u8>>::from_hex(
1370-
concat!("0100", "0208deadbeef1badbeef")
1371-
).unwrap()[..]) {
1378+
let buf = <Vec<u8>>::from_hex(
1379+
concat!("0100", "0208deadbeef1badbeef")
1380+
).unwrap();
1381+
if let Err(DecodeError::InvalidValue) = tlv_reader(&buf[..]) {
13721382
} else { panic!(); }
13731383
// ... even if that field is even
1374-
if let Err(DecodeError::InvalidValue) = tlv_reader(&<Vec<u8>>::from_hex(
1384+
let buf = <Vec<u8>>::from_hex(
13751385
concat!("0304deadbeef", "0500")
1376-
).unwrap()[..]) {
1386+
).unwrap();
1387+
if let Err(DecodeError::InvalidValue) = tlv_reader(&buf[..]) {
13771388
} else { panic!(); }
13781389
}
13791390

13801391
#[test]
13811392
fn tlv_simple_good_cases() {
1382-
assert_eq!(tlv_reader(&<Vec<u8>>::from_hex(
1383-
concat!("0208deadbeef1badbeef", "03041bad1dea")
1384-
).unwrap()[..]).unwrap(),
1393+
let buf = <Vec<u8>>::from_hex(
1394+
concat!("0208deadbeef1badbeef", "03041bad1dea")
1395+
).unwrap();
1396+
assert_eq!(tlv_reader(&buf[..]).unwrap(),
13851397
(0xdeadbeef1badbeef, 0x1bad1dea, None));
1386-
assert_eq!(tlv_reader(&<Vec<u8>>::from_hex(
1387-
concat!("0208deadbeef1badbeef", "03041bad1dea", "040401020304")
1388-
).unwrap()[..]).unwrap(),
1398+
let buf = <Vec<u8>>::from_hex(
1399+
concat!("0208deadbeef1badbeef", "03041bad1dea", "040401020304")
1400+
).unwrap();
1401+
assert_eq!(tlv_reader(&buf[..]).unwrap(),
13891402
(0xdeadbeef1badbeef, 0x1bad1dea, Some(0x01020304)));
13901403
}
13911404

@@ -1407,26 +1420,30 @@ mod tests {
14071420

14081421
#[test]
14091422
fn upgradable_tlv_simple_good_cases() {
1410-
assert_eq!(upgradable_tlv_reader(&<Vec<u8>>::from_hex(
1411-
concat!("0204deadbeef", "03041bad1dea", "0404deadbeef")
1412-
).unwrap()[..]).unwrap(),
1423+
let buf = <Vec<u8>>::from_hex(
1424+
concat!("0204deadbeef", "03041bad1dea", "0404deadbeef")
1425+
).unwrap();
1426+
assert_eq!(upgradable_tlv_reader(&buf[..]).unwrap(),
14131427
Some(TestUpgradable { a: 0xdeadbeef, b: 0x1bad1dea, c: Some(0xdeadbeef) }));
14141428

1415-
assert_eq!(upgradable_tlv_reader(&<Vec<u8>>::from_hex(
1429+
let buf = <Vec<u8>>::from_hex(
14161430
concat!("0204deadbeef", "03041bad1dea")
1417-
).unwrap()[..]).unwrap(),
1431+
).unwrap();
1432+
assert_eq!(upgradable_tlv_reader(&buf[..]).unwrap(),
14181433
Some(TestUpgradable { a: 0xdeadbeef, b: 0x1bad1dea, c: None}));
14191434
}
14201435

14211436
#[test]
14221437
fn missing_required_upgradable() {
1423-
if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&<Vec<u8>>::from_hex(
1438+
let buf = <Vec<u8>>::from_hex(
14241439
concat!("0100", "0204deadbeef")
1425-
).unwrap()[..]) {
1440+
).unwrap();
1441+
if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&buf[..]) {
14261442
} else { panic!(); }
1427-
if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&<Vec<u8>>::from_hex(
1443+
let buf = <Vec<u8>>::from_hex(
14281444
concat!("0100", "03041bad1dea")
1429-
).unwrap()[..]) {
1445+
).unwrap();
1446+
if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&buf[..]) {
14301447
} else { panic!(); }
14311448
}
14321449

0 commit comments

Comments
 (0)