@@ -33,7 +33,10 @@ macro_rules! _encode_tlv {
3333 $crate:: _encode_tlv!( $stream, $type, $field, required) ;
3434 } ;
3535 ( $stream: expr, $type: expr, $field: expr, required_vec $( , $self: ident) ?) => {
36- $crate:: _encode_tlv!( $stream, $type, $crate:: util:: ser:: WithoutLength ( & $field) , required) ;
36+ $crate:: _encode_tlv!( $stream, $type, $crate:: util:: ser:: WithoutLength ( $field) , required) ;
37+ } ;
38+ ( $stream: expr, $type: expr, $field: expr, ( required_vec, encoding: ( $fieldty: ty, $encoding: ident) ) $( , $self: ident) ?) => {
39+ $crate:: _encode_tlv!( $stream, $type, $encoding( $field) , required) ;
3740 } ;
3841 ( $stream: expr, $optional_type: expr, $optional_field: expr, option $( , $self: ident) ?) => {
3942 if let Some ( ref field) = $optional_field {
@@ -166,7 +169,7 @@ macro_rules! _encode_tlv_stream {
166169 ) *
167170 for tlv in $extra_tlvs {
168171 let ( typ, value) : & ( u64 , Vec <u8 >) = tlv;
169- $crate:: _encode_tlv!( $stream, * typ, * value, required_vec) ;
172+ $crate:: _encode_tlv!( $stream, * typ, value, required_vec) ;
170173 }
171174
172175 #[ allow( unused_mut, unused_variables, unused_assignments) ]
@@ -207,11 +210,15 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
207210 $crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, $field, required) ;
208211 } ;
209212 ( $len: expr, $type: expr, $field: expr, required_vec $( , $self: ident) ?) => {
210- let field = $crate:: util:: ser:: WithoutLength ( & $field) ;
213+ let field = $crate:: util:: ser:: WithoutLength ( $field) ;
214+ $crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, field, required) ;
215+ } ;
216+ ( $len: expr, $type: expr, $field: expr, ( required_vec, encoding: ( $fieldty: ty, $encoding: ident) ) $( , $self: ident) ?) => {
217+ let field = $encoding( $field) ;
211218 $crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, field, required) ;
212219 } ;
213220 ( $len: expr, $optional_type: expr, $optional_field: expr, option $( , $self: ident) ?) => {
214- if let Some ( ref field) = $optional_field {
221+ if let Some ( ref field) = $optional_field. as_ref ( ) {
215222 BigSize ( $optional_type)
216223 . write( & mut $len)
217224 . expect( "No in-memory data may fail to serialize" ) ;
@@ -265,7 +272,7 @@ macro_rules! _encode_varint_length_prefixed_tlv {
265272 ) *
266273 for tlv in $extra_tlvs {
267274 let ( typ, value) : & ( u64 , Vec <u8 >) = tlv;
268- $crate:: _get_varint_length_prefixed_tlv_length!( len, * typ, * value, required_vec) ;
275+ $crate:: _get_varint_length_prefixed_tlv_length!( len, * typ, value, required_vec) ;
269276 }
270277 len. 0
271278 } ;
@@ -316,6 +323,9 @@ macro_rules! _check_decoded_tlv_order {
316323 ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required_vec) => { {
317324 $crate:: _check_decoded_tlv_order!( $last_seen_type, $typ, $type, $field, required) ;
318325 } } ;
326+ ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( required_vec, encoding: $encoding: tt) ) => { {
327+ // no-op
328+ } } ;
319329 ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, optional_vec) => { {
320330 // no-op
321331 } } ;
@@ -363,6 +373,10 @@ macro_rules! _check_missing_tlv {
363373 ( $last_seen_type: expr, $type: expr, $field: ident, required_vec) => { {
364374 $crate:: _check_missing_tlv!( $last_seen_type, $type, $field, required) ;
365375 } } ;
376+ ( $last_seen_type: expr, $type: expr, $field: ident, ( required_vec, encoding: $encoding: tt) ) => { {
377+ // TODO: added this
378+ $crate:: _check_missing_tlv!( $last_seen_type, $type, $field, required) ;
379+ } } ;
366380 ( $last_seen_type: expr, $type: expr, $field: ident, option) => { {
367381 // no-op
368382 } } ;
@@ -412,6 +426,12 @@ macro_rules! _decode_tlv {
412426 let f: $crate:: util:: ser:: WithoutLength <Vec <_>> = $crate:: util:: ser:: LengthReadable :: read_from_fixed_length_buffer( & mut $reader) ?;
413427 $field = f. 0 ;
414428 } } ;
429+ ( $outer_reader: expr, $reader: expr, $field: ident, ( required_vec, encoding: ( $fieldty: ty, $encoding: ident) ) ) => { {
430+ $field = {
431+ let field: $encoding<$fieldty> = ser:: LengthReadable :: read_from_fixed_length_buffer( & mut $reader) ?;
432+ $crate:: util:: ser:: RequiredWrapper ( Some ( field. 0 ) )
433+ } ;
434+ } } ;
415435 ( $outer_reader: expr, $reader: expr, $field: ident, option) => { {
416436 $field = Some ( $crate:: util:: ser:: LengthReadable :: read_from_fixed_length_buffer( & mut $reader) ?) ;
417437 } } ;
@@ -762,7 +782,7 @@ macro_rules! write_ver_prefix {
762782#[ macro_export]
763783macro_rules! write_tlv_fields {
764784 ( $stream: expr, { $( ( $type: expr, $field: expr, $fieldty: tt) ) ,* $( , ) * } ) => {
765- $crate:: _encode_varint_length_prefixed_tlv!( $stream, { $( ( $type, $field, $fieldty) ) ,* } )
785+ $crate:: _encode_varint_length_prefixed_tlv!( $stream, { $( ( $type, & $field, $fieldty) ) ,* } )
766786 }
767787}
768788
@@ -830,6 +850,9 @@ macro_rules! _init_tlv_based_struct_field {
830850 ( $field: ident, required_vec) => {
831851 $field
832852 } ;
853+ ( $field: ident, ( required_vec, encoding: ( $fieldty: ty, $encoding: ident) ) ) => {
854+ $crate:: _init_tlv_based_struct_field!( $field, required)
855+ } ;
833856 ( $field: ident, optional_vec) => {
834857 $field. unwrap( )
835858 } ;
@@ -856,6 +879,9 @@ macro_rules! _init_tlv_field_var {
856879 ( $field: ident, required_vec) => {
857880 let mut $field = Vec :: new( ) ;
858881 } ;
882+ ( $field: ident, ( required_vec, encoding: ( $fieldty: ty, $encoding: ident) ) ) => {
883+ $crate:: _init_tlv_field_var!( $field, required) ;
884+ } ;
859885 ( $field: ident, option) => {
860886 let mut $field = None ;
861887 } ;
@@ -983,7 +1009,7 @@ macro_rules! impl_writeable_tlv_based {
9831009 impl $crate:: util:: ser:: Writeable for $st {
9841010 fn write<W : $crate:: util:: ser:: Writer >( & self , writer: & mut W ) -> Result <( ) , $crate:: io:: Error > {
9851011 $crate:: _encode_varint_length_prefixed_tlv!( writer, {
986- $( ( $type, self . $field, $fieldty, self ) ) ,*
1012+ $( ( $type, & self . $field, $fieldty, self ) ) ,*
9871013 } ) ;
9881014 Ok ( ( ) )
9891015 }
@@ -995,7 +1021,7 @@ macro_rules! impl_writeable_tlv_based {
9951021 #[ allow( unused_mut) ]
9961022 let mut len = $crate:: util:: ser:: LengthCalculatingWriter ( 0 ) ;
9971023 $(
998- $crate:: _get_varint_length_prefixed_tlv_length!( len, $type, self . $field, $fieldty, self ) ;
1024+ $crate:: _get_varint_length_prefixed_tlv_length!( len, $type, & self . $field, $fieldty, self ) ;
9991025 ) *
10001026 len. 0
10011027 } ;
@@ -1112,7 +1138,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
11121138 let id: u8 = $variant_id;
11131139 id. write( writer) ?;
11141140 $crate:: _encode_varint_length_prefixed_tlv!( writer, {
1115- $( ( $type, * $field, $fieldty, self ) ) ,*
1141+ $( ( $type, $field, $fieldty, self ) ) ,*
11161142 } ) ;
11171143 } ) ,*
11181144 $( $st:: $tuple_variant_name ( ref field) => {
@@ -1370,7 +1396,8 @@ mod tests {
13701396 use crate :: io:: { self , Cursor } ;
13711397 use crate :: ln:: msgs:: DecodeError ;
13721398 use crate :: util:: ser:: {
1373- HighZeroBytesDroppedBigSize , LengthReadable , MaybeReadable , Readable , VecWriter , Writeable ,
1399+ FixedLengthReader , HighZeroBytesDroppedBigSize , LengthReadable , MaybeReadable , Readable ,
1400+ VecWriter , WithoutLength , Writeable ,
13741401 } ;
13751402 use bitcoin:: hex:: FromHex ;
13761403 use bitcoin:: secp256k1:: PublicKey ;
@@ -1879,4 +1906,25 @@ mod tests {
18791906 let read = <ExpandedField as Readable >:: read ( & mut & encoded[ ..] ) . unwrap ( ) ;
18801907 assert_eq ! ( read, ExpandedField { new_field: ( 42 , 0 ) } ) ;
18811908 }
1909+
1910+ #[ test]
1911+ fn required_vec_with_encoding ( ) {
1912+ // Ensure that serializing a required vec with a specified encoding will survive a ser round
1913+ // trip.
1914+ #[ derive( PartialEq , Eq , Debug ) ]
1915+ struct MyCustomStruct {
1916+ tlv_field : Vec < u8 > ,
1917+ }
1918+ impl_writeable_tlv_based ! ( MyCustomStruct , {
1919+ ( 0 , tlv_field, ( required_vec, encoding: ( Vec <u8 >, WithoutLength ) ) ) ,
1920+ } ) ;
1921+
1922+ let instance = MyCustomStruct { tlv_field : vec ! [ 42 ; 32 ] } ;
1923+ let encoded = instance. encode ( ) ;
1924+ let mut buffer = & encoded[ ..] ;
1925+ let mut reader = FixedLengthReader :: new ( & mut buffer, encoded. len ( ) as u64 ) ;
1926+ let decoded: MyCustomStruct =
1927+ LengthReadable :: read_from_fixed_length_buffer ( & mut reader) . unwrap ( ) ;
1928+ assert_eq ! ( decoded, instance) ;
1929+ }
18821930}
0 commit comments