Skip to content

Commit 47d4b0d

Browse files
committed
f use a common macro for building
1 parent 602a4fa commit 47d4b0d

File tree

2 files changed

+52
-45
lines changed

2 files changed

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

9841026
impl $crate::util::ser::Readable for $st {
9851027
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-
}))
1028+
Ok($crate::_decode_and_build!(reader, Self, {$(($type, $field, $fieldty)),*}))
9941029
}
9951030
}
9961031
}
@@ -1176,14 +1211,7 @@ macro_rules! impl_writeable_tlv_based_enum {
11761211
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
11771212
// in the same function body. Instead, we define a closure and call it.
11781213
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-
}))
1214+
Ok($crate::_decode_and_build!(reader, $st::$variant_name, {$(($type, $field, $fieldty)),*}))
11871215
};
11881216
f()
11891217
}),*
@@ -1225,14 +1253,7 @@ macro_rules! impl_writeable_tlv_based_enum_legacy {
12251253
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
12261254
// in the same function body. Instead, we define a closure and call it.
12271255
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-
}))
1256+
Ok($crate::_decode_and_build!(reader, $st::$variant_name, {$(($type, $field, $fieldty)),*}))
12361257
};
12371258
f()
12381259
}),*
@@ -1288,14 +1309,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
12881309
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
12891310
// in the same function body. Instead, we define a closure and call it.
12901311
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-
})))
1312+
Ok(Some($crate::_decode_and_build!(reader, $st::$variant_name, {$(($type, $field, $fieldty)),*})))
12991313
};
13001314
f()
13011315
}),*
@@ -1344,14 +1358,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable_legacy {
13441358
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
13451359
// in the same function body. Instead, we define a closure and call it.
13461360
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-
})))
1361+
Ok(Some($crate::_decode_and_build!(reader, $st::$variant_name, {$(($type, $field, $fieldty)),*})))
13551362
};
13561363
f()
13571364
}),*

0 commit comments

Comments
 (0)