@@ -569,6 +569,13 @@ macro_rules! decode_tlv_stream_with_custom_tlv_decode {
569
569
#[ macro_export]
570
570
macro_rules! _decode_tlv_stream_range {
571
571
( $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) ) ,* $( , ) * }
572
579
$( , $decode_custom_tlv: expr) ?) => { {
573
580
use $crate:: ln:: msgs:: DecodeError ;
574
581
let mut last_seen_type: Option <u64 > = None ;
@@ -648,9 +655,6 @@ macro_rules! _decode_tlv_stream_range {
648
655
$( {
649
656
$crate:: _check_missing_tlv!( last_seen_type, $type, $field, $fieldty) ;
650
657
} ) *
651
- $( {
652
- $crate:: _run_legacy_tlv_read_logic!( $field, $fieldty) ;
653
- } ) *
654
658
} }
655
659
}
656
660
@@ -914,13 +918,49 @@ macro_rules! _init_and_read_tlv_stream {
914
918
$(
915
919
$crate:: _init_tlv_field_var!( $field, $fieldty) ;
916
920
) *
917
-
918
921
$crate:: decode_tlv_stream!( $reader, {
919
922
$( ( $type, $field, $fieldty) ) ,*
920
923
} ) ;
921
924
}
922
925
}
923
926
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
+
924
964
/// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
925
965
/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
926
966
/// 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 {
983
1023
984
1024
impl $crate:: util:: ser:: Readable for $st {
985
1025
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) ) ,* } ) )
994
1027
}
995
1028
}
996
1029
}
@@ -1176,14 +1209,7 @@ macro_rules! impl_writeable_tlv_based_enum {
1176
1209
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
1177
1210
// in the same function body. Instead, we define a closure and call it.
1178
1211
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) ) ,* } ) )
1187
1213
} ;
1188
1214
f( )
1189
1215
} ) ,*
@@ -1225,14 +1251,7 @@ macro_rules! impl_writeable_tlv_based_enum_legacy {
1225
1251
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
1226
1252
// in the same function body. Instead, we define a closure and call it.
1227
1253
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) ) ,* } ) )
1236
1255
} ;
1237
1256
f( )
1238
1257
} ) ,*
@@ -1288,14 +1307,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
1288
1307
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
1289
1308
// in the same function body. Instead, we define a closure and call it.
1290
1309
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) ) ,* } ) ) )
1299
1311
} ;
1300
1312
f( )
1301
1313
} ) ,*
@@ -1344,14 +1356,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable_legacy {
1344
1356
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
1345
1357
// in the same function body. Instead, we define a closure and call it.
1346
1358
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) ) ,* } ) ) )
1355
1360
} ;
1356
1361
f( )
1357
1362
} ) ,*
0 commit comments