@@ -39,6 +39,11 @@ macro_rules! _encode_tlv {
39
39
field. write( $stream) ?;
40
40
}
41
41
} ;
42
+ ( $stream: expr, $type: expr, $field: expr, optional_vec) => {
43
+ if !$field. is_empty( ) {
44
+ $crate:: _encode_tlv!( $stream, $type, vec_type) ;
45
+ }
46
+ } ;
42
47
( $stream: expr, $type: expr, $field: expr, upgradable_required) => {
43
48
$crate:: _encode_tlv!( $stream, $type, $field, required) ;
44
49
} ;
@@ -165,6 +170,11 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
165
170
$len. 0 += field_len;
166
171
}
167
172
} ;
173
+ ( $len: expr, $type: expr, $field: expr, optional_vec) => {
174
+ if !$field. is_empty( ) {
175
+ $crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, $field, vec_type) ;
176
+ }
177
+ }
168
178
( $len: expr, $type: expr, $field: expr, ( option: $trait: ident $( , $read_arg: expr) ?) ) => {
169
179
$crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, $field, option) ;
170
180
} ;
@@ -226,6 +236,9 @@ macro_rules! _check_decoded_tlv_order {
226
236
( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, vec_type) => { {
227
237
// no-op
228
238
} } ;
239
+ ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, optional_vec) => { {
240
+ // no-op
241
+ } } ;
229
242
( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, upgradable_required) => { {
230
243
_check_decoded_tlv_order!( $last_seen_type, $typ, $type, $field, required)
231
244
} } ;
@@ -271,6 +284,9 @@ macro_rules! _check_missing_tlv {
271
284
( $last_seen_type: expr, $type: expr, $field: ident, option) => { {
272
285
// no-op
273
286
} } ;
287
+ ( $last_seen_type: expr, $type: expr, $field: ident, optional_vec) => { {
288
+ // no-op
289
+ } } ;
274
290
( $last_seen_type: expr, $type: expr, $field: ident, upgradable_required) => { {
275
291
_check_missing_tlv!( $last_seen_type, $type, $field, required)
276
292
} } ;
@@ -308,6 +324,9 @@ macro_rules! _decode_tlv {
308
324
( $reader: expr, $field: ident, option) => { {
309
325
$field = Some ( $crate:: util:: ser:: Readable :: read( & mut $reader) ?) ;
310
326
} } ;
327
+ ( $reader: expr, $field: ident, optional_vec) => { {
328
+ $crate:: _decode_tlv!( $reader, $field, vec_type) ;
329
+ } } ;
311
330
// `upgradable_required` indicates we're reading a required TLV that may have been upgraded
312
331
// without backwards compat. We'll error if the field is missing, and return `Ok(None)` if the
313
332
// field is present but we can no longer understand it.
@@ -675,6 +694,9 @@ macro_rules! _init_tlv_based_struct_field {
675
694
( $field: ident, vec_type) => {
676
695
$field. unwrap( )
677
696
} ;
697
+ ( $field: ident, optional_vec) => {
698
+ $field. unwrap( )
699
+ } ;
678
700
}
679
701
680
702
/// Initializes the variable we are going to read the TLV into.
@@ -701,6 +723,9 @@ macro_rules! _init_tlv_field_var {
701
723
( $field: ident, option) => {
702
724
let mut $field = None ;
703
725
} ;
726
+ ( $field: ident, optional_vec) => {
727
+ let mut $field = Some ( Vec :: new( ) ) ;
728
+ } ;
704
729
( $field: ident, ( option: $trait: ident $( , $read_arg: expr) ?) ) => {
705
730
$crate:: _init_tlv_field_var!( $field, option) ;
706
731
} ;
@@ -733,7 +758,8 @@ macro_rules! _init_and_read_tlv_fields {
733
758
/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
734
759
/// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
735
760
/// If `$fieldty` is `option`, then `$field` is optional field.
736
- /// If `$fieldty` is `vec_type`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
761
+ /// If `$fieldty` is `optional_vec`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
762
+ /// Note that for `optional_vec` no bytes are written if the vec is empty
737
763
///
738
764
/// For example,
739
765
/// ```
@@ -749,7 +775,7 @@ macro_rules! _init_and_read_tlv_fields {
749
775
/// (0, tlv_integer, required),
750
776
/// (1, tlv_default_integer, (default_value, 7)),
751
777
/// (2, tlv_optional_integer, option),
752
- /// (3, tlv_vec_type_integer, vec_type ),
778
+ /// (3, tlv_vec_type_integer, optional_vec ),
753
779
/// });
754
780
/// ```
755
781
///
@@ -907,7 +933,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
907
933
/// ```ignore
908
934
/// impl_writeable_tlv_based_enum!(EnumName,
909
935
/// (0, StructVariantA) => {(0, required_variant_field, required), (1, optional_variant_field, option)},
910
- /// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, vec_type )};
936
+ /// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, optional_vec )};
911
937
/// (2, TupleVariantA), (3, TupleVariantB),
912
938
/// );
913
939
/// ```
0 commit comments