@@ -45,7 +45,7 @@ macro_rules! _encode_tlv {
4545 field. write( $stream) ?;
4646 }
4747 } ;
48- ( $stream: expr, $optional_type: expr, $optional_field: expr, ( legacy, $fieldty: ty, $write: expr) $( , $self: ident) ?) => { {
48+ ( $stream: expr, $optional_type: expr, $optional_field: expr, ( legacy, $fieldty: ty, $read : expr , $ write: expr) $( , $self: ident) ?) => { {
4949 let value: Option <_> = $write( $( $self) ?) ;
5050 #[ cfg( debug_assertions) ]
5151 {
@@ -64,7 +64,7 @@ macro_rules! _encode_tlv {
6464 $crate:: _encode_tlv!( $stream, $optional_type, value, option) ;
6565 } } ;
6666 ( $stream: expr, $optional_type: expr, $optional_field: expr, ( custom, $fieldty: ty, $read: expr, $write: expr) $( , $self: ident) ?) => { {
67- $crate:: _encode_tlv!( $stream, $optional_type, $optional_field, ( legacy, $fieldty, $write) $( , $self) ?) ;
67+ $crate:: _encode_tlv!( $stream, $optional_type, $optional_field, ( legacy, $fieldty, $read , $ write) $( , $self) ?) ;
6868 } } ;
6969 ( $stream: expr, $type: expr, $field: expr, optional_vec $( , $self: ident) ?) => {
7070 if !$field. is_empty( ) {
@@ -232,11 +232,11 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
232232 $len. 0 += field_len;
233233 }
234234 } ;
235- ( $len: expr, $optional_type: expr, $optional_field: expr, ( legacy, $fieldty: ty, $write: expr) $( , $self: ident) ?) => {
235+ ( $len: expr, $optional_type: expr, $optional_field: expr, ( legacy, $fieldty: ty, $read : expr , $ write: expr) $( , $self: ident) ?) => {
236236 $crate:: _get_varint_length_prefixed_tlv_length!( $len, $optional_type, $write( $( $self) ?) , option) ;
237237 } ;
238238 ( $len: expr, $optional_type: expr, $optional_field: expr, ( custom, $fieldty: ty, $read: expr, $write: expr) $( , $self: ident) ?) => {
239- $crate:: _get_varint_length_prefixed_tlv_length!( $len, $optional_type, $optional_field, ( legacy, $fieldty, $write) $( , $self) ?) ;
239+ $crate:: _get_varint_length_prefixed_tlv_length!( $len, $optional_type, $optional_field, ( legacy, $fieldty, $read , $ write) $( , $self) ?) ;
240240 } ;
241241 ( $len: expr, $type: expr, $field: expr, optional_vec $( , $self: ident) ?) => {
242242 if !$field. is_empty( ) {
@@ -320,7 +320,7 @@ macro_rules! _check_decoded_tlv_order {
320320 ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( option, explicit_type: $fieldty: ty) ) => { {
321321 // no-op
322322 } } ;
323- ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( legacy, $fieldty: ty, $write: expr) ) => { {
323+ ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( legacy, $fieldty: ty, $read : expr , $ write: expr) ) => { {
324324 // no-op
325325 } } ;
326326 ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( custom, $fieldty: ty, $read: expr, $write: expr) $( , $self: ident) ?) => { {
@@ -398,8 +398,13 @@ macro_rules! _check_missing_tlv {
398398 ( $last_seen_type: expr, $type: expr, $field: ident, ( option, explicit_type: $fieldty: ty) ) => { {
399399 // no-op
400400 } } ;
401- ( $last_seen_type: expr, $type: expr, $field: ident, ( legacy, $fieldty: ty, $write: expr) ) => { {
402- // no-op
401+ ( $last_seen_type: expr, $type: expr, $field: ident, ( legacy, $fieldty: ty, $read: expr, $write: expr) ) => { {
402+ use $crate:: ln:: msgs:: DecodeError ;
403+ let read_result: Result <_, DecodeError > = $read( $field) ;
404+ #[ allow( unused_assignments) ]
405+ {
406+ $field = read_result?;
407+ }
403408 } } ;
404409 ( $last_seen_type: expr, $type: expr, $field: ident, ( custom, $fieldty: ty, $read: expr, $write: expr) ) => { {
405410 // Note that $type may be 0 making the second comparison always false
@@ -463,7 +468,7 @@ macro_rules! _decode_tlv {
463468 let _field: & Option <$fieldty> = & $field;
464469 $crate:: _decode_tlv!( $outer_reader, $reader, $field, option) ;
465470 } } ;
466- ( $outer_reader: expr, $reader: expr, $field: ident, ( legacy, $fieldty: ty, $write: expr) ) => { {
471+ ( $outer_reader: expr, $reader: expr, $field: ident, ( legacy, $fieldty: ty, $read : expr , $ write: expr) ) => { {
467472 $crate:: _decode_tlv!( $outer_reader, $reader, $field, ( option, explicit_type: $fieldty) ) ;
468473 } } ;
469474 ( $outer_reader: expr, $reader: expr, $field: ident, ( custom, $fieldty: ty, $read: expr, $write: expr) ) => { {
@@ -858,7 +863,7 @@ macro_rules! _init_tlv_based_struct_field {
858863 ( $field: ident, option) => {
859864 $field
860865 } ;
861- ( $field: ident, ( legacy, $fieldty: ty, $write: expr) ) => {
866+ ( $field: ident, ( legacy, $fieldty: ty, $read : expr , $ write: expr) ) => {
862867 $crate:: _init_tlv_based_struct_field!( $field, option)
863868 } ;
864869 ( $field: ident, ( custom, $fieldty: ty, $read: expr, $write: expr) ) => {
@@ -927,7 +932,7 @@ macro_rules! _init_tlv_field_var {
927932 ( $field: ident, ( option, explicit_type: $fieldty: ty) ) => {
928933 let mut $field: Option <$fieldty> = None ;
929934 } ;
930- ( $field: ident, ( legacy, $fieldty: ty, $write: expr) ) => {
935+ ( $field: ident, ( legacy, $fieldty: ty, $read : expr , $ write: expr) ) => {
931936 $crate:: _init_tlv_field_var!( $field, ( option, explicit_type: $fieldty) ) ;
932937 } ;
933938 ( $field: ident, ( custom, $fieldty: ty, $read: expr, $write: expr) ) => {
@@ -1012,10 +1017,12 @@ macro_rules! _decode_and_build {
10121017/// [`MaybeReadable`], requiring the TLV to be present.
10131018/// If `$fieldty` is `optional_vec`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
10141019/// Note that for `optional_vec` no bytes are written if the vec is empty
1015- /// If `$fieldty` is `(legacy, $ty, $write)` then, when writing, the function $write will be
1020+ /// If `$fieldty` is `(legacy, $ty, $read, $ write)` then, when writing, the function $write will be
10161021/// called with the object being serialized and a returned `Option` and is written as a TLV if
1017- /// `Some`. When reading, an optional field of type `$ty` is read (which can be used in later
1018- /// `default_value` or `static_value` fields by referring to the value by name).
1022+ /// `Some`. When reading, an optional field of type `$ty` is read, and after all TLV fields are
1023+ /// read, the `$read` closure is called with the `Option<$ty>` value. The `$read` closure should
1024+ /// return a `Result<Option<$ty>, DecodeError>`. Legacy field values can be used in later
1025+ /// `default_value` or `static_value` fields by referring to the value by name.
10191026/// If `$fieldty` is `(custom, $ty, $read, $write)` then, when writing, the same behavior as
10201027/// `legacy`, above is used. When reading, if a TLV is present, it is read as `$ty` and the
10211028/// `$read` method is called with `Some(decoded_$ty_object)`. If no TLV is present, the field
@@ -1039,7 +1046,7 @@ macro_rules! _decode_and_build {
10391046/// (1, tlv_default_integer, (default_value, 7)),
10401047/// (2, tlv_optional_integer, option),
10411048/// (3, tlv_vec_type_integer, optional_vec),
1042- /// (4, unwritten_type, (legacy, u32, |us: &LightningMessage| Some(us.tlv_integer))),
1049+ /// (4, unwritten_type, (legacy, u32, Ok, |us: &LightningMessage| Some(us.tlv_integer))),
10431050/// (_unused, tlv_upgraded_integer, (static_value, unwritten_type.unwrap_or(0) * 2))
10441051/// });
10451052/// ```
@@ -1931,7 +1938,7 @@ mod tests {
19311938 new_field : ( u8 , u8 ) ,
19321939 }
19331940 impl_writeable_tlv_based ! ( ExpandedField , {
1934- ( 0 , old_field, ( legacy, u8 , |us: & ExpandedField | Some ( us. new_field. 0 ) ) ) ,
1941+ ( 0 , old_field, ( legacy, u8 , Ok , |us: & ExpandedField | Some ( us. new_field. 0 ) ) ) ,
19351942 ( 1 , new_field, ( default_value, ( old_field. ok_or( DecodeError :: InvalidValue ) ?, 0 ) ) ) ,
19361943 } ) ;
19371944
0 commit comments