Skip to content

Commit a13c7e2

Browse files
committed
f use a common macro
1 parent 04b701b commit a13c7e2

File tree

2 files changed

+50
-45
lines changed

2 files changed

+50
-45
lines changed

lightning-macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub fn drop_legacy_field_definition(expr: TokenStream) -> TokenStream {
277277
for field in new_fields {
278278
if let syn::Expr::Macro(syn::ExprMacro { mac, .. }) = &field.expr {
279279
let macro_name = mac.path.segments.last().unwrap().ident.to_string();
280-
let is_init = macro_name == "_init_tlv_based_struct_field";
280+
let is_init = macro_name == "_ignore_arg";
281281
let ty_tokens = mac.tokens.clone().into_iter().skip(2).next();
282282
if let Some(proc_macro2::TokenTree::Group(group)) = ty_tokens {
283283
let first_token = group.stream().into_iter().next();

lightning/src/util/ser_macros.rs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,13 @@ macro_rules! decode_tlv_stream_with_custom_tlv_decode {
569569
#[macro_export]
570570
macro_rules! _decode_tlv_stream_range {
571571
($stream: expr, $range: expr, $rewind: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
572+
$(, $decode_custom_tlv: expr)?) => { {
573+
$crate::_decode_tlv_stream_range!(SKIP_LEGACY, $stream, $range, $rewind, {$(($type, $field, $fieldty)),*} $(, $decode_custom_tlv)?);
574+
$({
575+
$crate::_run_legacy_tlv_read_logic!($field, $fieldty);
576+
})*
577+
} };
578+
(SKIP_LEGACY, $stream: expr, $range: expr, $rewind: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
572579
$(, $decode_custom_tlv: expr)?) => { {
573580
use $crate::ln::msgs::DecodeError;
574581
let mut last_seen_type: Option<u64> = None;
@@ -648,9 +655,6 @@ macro_rules! _decode_tlv_stream_range {
648655
$({
649656
$crate::_check_missing_tlv!(last_seen_type, $type, $field, $fieldty);
650657
})*
651-
$({
652-
$crate::_run_legacy_tlv_read_logic!($field, $fieldty);
653-
})*
654658
} }
655659
}
656660

@@ -914,13 +918,49 @@ macro_rules! _init_and_read_tlv_stream {
914918
$(
915919
$crate::_init_tlv_field_var!($field, $fieldty);
916920
)*
917-
918921
$crate::decode_tlv_stream!($reader, {
919922
$(($type, $field, $fieldty)),*
920923
});
921924
}
922925
}
923926

927+
/// Dummy macro that drops the second argument. Used by
928+
/// [`lightning_macros::drop_legacy_field_definition`] to match for legacy fields.
929+
#[doc(hidden)]
930+
#[macro_export]
931+
macro_rules! _ignore_arg {
932+
($field: ident, $fieldty: tt) => {$field}
933+
}
934+
935+
/// Reads a TLV stream with the given fields to build a struct/enum of type `$thing`
936+
#[doc(hidden)]
937+
#[macro_export]
938+
macro_rules! _decode_and_build {
939+
($stream: ident, $thing: path, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { {
940+
$(
941+
$crate::_init_tlv_field_var!($field, $fieldty);
942+
)*
943+
let tlv_len: $crate::util::ser::BigSize = $crate::util::ser::Readable::read($stream)?;
944+
let mut rd = $crate::util::ser::FixedLengthReader::new($stream, tlv_len.0);
945+
let rewind = |_, _| { unreachable!() };
946+
$crate::_decode_tlv_stream_range!(SKIP_LEGACY, &mut rd, .., rewind, {$(($type, $field, $fieldty)),*});
947+
rd.eat_remaining().map_err(|_| $crate::ln::msgs::DecodeError::ShortRead)?;
948+
// We mark fields as `mut` as we read them so that legacy post-read actions can modify them
949+
// if desired.
950+
$(
951+
#[allow(unused_mut)]
952+
let mut $field = $crate::_init_tlv_based_struct_field!($field, $fieldty);
953+
)*
954+
$({
955+
$crate::_run_legacy_tlv_read_logic!($field, $fieldty);
956+
})*
957+
958+
::lightning_macros::drop_legacy_field_definition!($thing {
959+
$($field: $crate::_ignore_arg!($field, $fieldty)),*
960+
})
961+
} }
962+
}
963+
924964
/// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
925965
/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
926966
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
@@ -983,14 +1023,7 @@ macro_rules! impl_writeable_tlv_based {
9831023

9841024
impl $crate::util::ser::Readable for $st {
9851025
fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
986-
$crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
987-
$(($type, $field, $fieldty)),*
988-
});
989-
Ok(::lightning_macros::drop_legacy_field_definition!(Self {
990-
$(
991-
$field: $crate::_init_tlv_based_struct_field!($field, $fieldty)
992-
),*
993-
}))
1026+
Ok($crate::_decode_and_build!(reader, Self, {$(($type, $field, $fieldty)),*}))
9941027
}
9951028
}
9961029
}
@@ -1176,14 +1209,7 @@ macro_rules! impl_writeable_tlv_based_enum {
11761209
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
11771210
// in the same function body. Instead, we define a closure and call it.
11781211
let mut f = || {
1179-
$crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
1180-
$(($type, $field, $fieldty)),*
1181-
});
1182-
Ok(::lightning_macros::drop_legacy_field_definition!($st::$variant_name {
1183-
$(
1184-
$field: $crate::_init_tlv_based_struct_field!($field, $fieldty)
1185-
),*
1186-
}))
1212+
Ok($crate::_decode_and_build!(reader, $st::$variant_name, {$(($type, $field, $fieldty)),*}))
11871213
};
11881214
f()
11891215
}),*
@@ -1225,14 +1251,7 @@ macro_rules! impl_writeable_tlv_based_enum_legacy {
12251251
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
12261252
// in the same function body. Instead, we define a closure and call it.
12271253
let mut f = || {
1228-
$crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
1229-
$(($type, $field, $fieldty)),*
1230-
});
1231-
Ok(::lightning_macros::drop_legacy_field_definition!($st::$variant_name {
1232-
$(
1233-
$field: $crate::_init_tlv_based_struct_field!($field, $fieldty)
1234-
),*
1235-
}))
1254+
Ok($crate::_decode_and_build!(reader, $st::$variant_name, {$(($type, $field, $fieldty)),*}))
12361255
};
12371256
f()
12381257
}),*
@@ -1288,14 +1307,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
12881307
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
12891308
// in the same function body. Instead, we define a closure and call it.
12901309
let mut f = || {
1291-
$crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
1292-
$(($type, $field, $fieldty)),*
1293-
});
1294-
Ok(Some(::lightning_macros::drop_legacy_field_definition!($st::$variant_name {
1295-
$(
1296-
$field: $crate::_init_tlv_based_struct_field!($field, $fieldty)
1297-
),*
1298-
})))
1310+
Ok(Some($crate::_decode_and_build!(reader, $st::$variant_name, {$(($type, $field, $fieldty)),*})))
12991311
};
13001312
f()
13011313
}),*
@@ -1344,14 +1356,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable_legacy {
13441356
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
13451357
// in the same function body. Instead, we define a closure and call it.
13461358
let mut f = || {
1347-
$crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
1348-
$(($type, $field, $fieldty)),*
1349-
});
1350-
Ok(Some(::lightning_macros::drop_legacy_field_definition!($st::$variant_name {
1351-
$(
1352-
$field: $crate::_init_tlv_based_struct_field!($field, $fieldty)
1353-
),*
1354-
})))
1359+
Ok(Some($crate::_decode_and_build!(reader, $st::$variant_name, {$(($type, $field, $fieldty)),*})))
13551360
};
13561361
f()
13571362
}),*

0 commit comments

Comments
 (0)