@@ -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,51 @@ 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) => {
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
+
924
966
/// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
925
967
/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
926
968
/// 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 {
983
1025
984
1026
impl $crate:: util:: ser:: Readable for $st {
985
1027
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) ) ,* } ) )
994
1029
}
995
1030
}
996
1031
}
@@ -1176,14 +1211,7 @@ macro_rules! impl_writeable_tlv_based_enum {
1176
1211
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
1177
1212
// in the same function body. Instead, we define a closure and call it.
1178
1213
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) ) ,* } ) )
1187
1215
} ;
1188
1216
f( )
1189
1217
} ) ,*
@@ -1225,14 +1253,7 @@ macro_rules! impl_writeable_tlv_based_enum_legacy {
1225
1253
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
1226
1254
// in the same function body. Instead, we define a closure and call it.
1227
1255
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) ) ,* } ) )
1236
1257
} ;
1237
1258
f( )
1238
1259
} ) ,*
@@ -1288,14 +1309,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
1288
1309
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
1289
1310
// in the same function body. Instead, we define a closure and call it.
1290
1311
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) ) ,* } ) ) )
1299
1313
} ;
1300
1314
f( )
1301
1315
} ) ,*
@@ -1344,14 +1358,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable_legacy {
1344
1358
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
1345
1359
// in the same function body. Instead, we define a closure and call it.
1346
1360
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) ) ,* } ) ) )
1355
1362
} ;
1356
1363
f( )
1357
1364
} ) ,*
0 commit comments