@@ -26,13 +26,11 @@ macro_rules! encode_tlv {
26
26
field. write( $stream) ?;
27
27
}
28
28
} ;
29
- ( $stream: expr, $type: expr, $field: expr, ( tlv_record, $fieldty: tt) ) => {
30
- if let Some ( field) = $field {
31
- let field: encoded_tlv_record_ref_type!( $fieldty) = From :: from( field) ;
32
- BigSize ( $type) . write( $stream) ?;
33
- BigSize ( field. serialized_length( ) as u64 ) . write( $stream) ?;
34
- field. write( $stream) ?;
35
- }
29
+ ( $stream: expr, $type: expr, $field: expr, ( tlv_record, ( $fieldty: ty, $wrapper: ident) ) ) => {
30
+ encode_tlv!( $stream, $type, $field. map( |f| $wrapper( f) ) , option) ;
31
+ } ;
32
+ ( $stream: expr, $type: expr, $field: expr, ( tlv_record, $fieldty: ty) ) => {
33
+ encode_tlv!( $stream, $type, $field, option) ;
36
34
} ;
37
35
}
38
36
@@ -186,12 +184,15 @@ macro_rules! decode_tlv {
186
184
( $reader: expr, $field: ident, ( option: $trait: ident $( , $read_arg: expr) ?) ) => { {
187
185
$field = Some ( $trait:: read( & mut $reader $( , $read_arg) * ) ?) ;
188
186
} } ;
189
- ( $reader: expr, $field: ident, ( tlv_record, $fieldty: tt ) ) => { {
187
+ ( $reader: expr, $field: ident, ( tlv_record, ( $fieldty: ty , $wrapper : ident ) ) ) => { {
190
188
$field = {
191
- let field: encoded_tlv_record_type! ( $ fieldty) = ser:: Readable :: read( & mut $reader) ?;
189
+ let field: $wrapper<$ fieldty> = ser:: Readable :: read( & mut $reader) ?;
192
190
Some ( field. into( ) )
193
191
} ;
194
192
} } ;
193
+ ( $reader: expr, $field: ident, ( tlv_record, $fieldty: ty) ) => { {
194
+ decode_tlv!( $reader, $field, option) ;
195
+ } } ;
195
196
}
196
197
197
198
macro_rules! decode_tlv_stream {
@@ -453,7 +454,9 @@ macro_rules! impl_writeable_tlv_based {
453
454
/// Defines a struct for a TLV stream and a similar struct using references for non-primitive types,
454
455
/// implementing [`Readable`] for the former and [`Writeable`] for the latter. Useful as an
455
456
/// intermediary format when reading or writing a type encoded as a TLV stream. Note that each field
456
- /// representing a TLV record has its type wrapped with an [`Option`].
457
+ /// representing a TLV record has its type wrapped with an [`Option`]. A tuple consisting of a type
458
+ /// and a serialization wrapper may be given in place of a type when custom serialization is
459
+ /// required.
457
460
///
458
461
/// [`Readable`]: crate::util::ser::Readable
459
462
/// [`Writeable`]: crate::util::ser::Writeable
@@ -503,57 +506,18 @@ macro_rules! tlv_stream {
503
506
}
504
507
505
508
macro_rules! tlv_record_type {
506
- ( ( $type: ty, $wrapper: ident) ) => {
507
- $type
508
- } ;
509
- ( $type: ty) => {
510
- $type
511
- } ;
509
+ ( ( $type: ty, $wrapper: ident) ) => { $type } ;
510
+ ( $type: ty) => { $type } ;
512
511
}
513
512
514
513
macro_rules! tlv_record_ref_type {
515
- ( u8 ) => {
516
- u8
517
- } ;
518
- ( char ) => {
519
- char
520
- } ;
521
- ( ( $type: ty, HighZeroBytesDroppedBigSize ) ) => {
522
- $type
523
- } ;
524
- ( ( $type: ty, $wrapper: ident) ) => {
525
- & ' a $type
526
- } ;
527
- ( $type: ty) => {
528
- & ' a $type
529
- } ;
530
- }
531
-
532
- macro_rules! encoded_tlv_record_type {
533
- ( ( $type: ty, $wrapper: ident) ) => {
534
- $wrapper<$type>
535
- } ;
536
- ( $type: ty) => {
537
- $type
538
- } ;
539
- }
540
-
541
- macro_rules! encoded_tlv_record_ref_type {
542
- ( u8 ) => {
543
- u8
544
- } ;
545
- ( char ) => {
546
- char
547
- } ;
548
- ( ( $type: ty, HighZeroBytesDroppedBigSize ) ) => {
549
- HighZeroBytesDroppedBigSize <$type>
550
- } ;
551
- ( ( $type: ty, $wrapper: ident) ) => {
552
- $wrapper<& $type>
553
- } ;
554
- ( $type: ty) => {
555
- & $type
556
- } ;
514
+ ( char ) => { char } ;
515
+ ( u8 ) => { u8 } ;
516
+ ( ( u16 , $wrapper: ident) ) => { u16 } ;
517
+ ( ( u32 , $wrapper: ident) ) => { u32 } ;
518
+ ( ( u64 , $wrapper: ident) ) => { u64 } ;
519
+ ( ( $type: ty, $wrapper: ident) ) => { & ' a $type } ;
520
+ ( $type: ty) => { & ' a $type } ;
557
521
}
558
522
559
523
macro_rules! _impl_writeable_tlv_based_enum_common {
0 commit comments