@@ -11,6 +11,9 @@ macro_rules! encode_tlv {
11
11
( $stream: expr, $type: expr, $field: expr, ( default_value, $default: expr) ) => {
12
12
encode_tlv!( $stream, $type, $field, required)
13
13
} ;
14
+ ( $stream: expr, $type: expr, $field: expr, ( reset_on_reload, $value: expr) ) => {
15
+ let _ = & $field; // Ensure we "use" the $field
16
+ } ;
14
17
( $stream: expr, $type: expr, $field: expr, required) => {
15
18
BigSize ( $type) . write( $stream) ?;
16
19
BigSize ( $field. serialized_length( ) as u64 ) . write( $stream) ?;
@@ -34,6 +37,17 @@ macro_rules! encode_tlv {
34
37
} ;
35
38
}
36
39
40
+
41
+ macro_rules! _check_tlv_ordering {
42
+ ( $last_type: expr, $type: expr, ( reset_on_reload, $value: expr) ) => { } ;
43
+ ( $last_type: expr, $type: expr, $fieldty: tt) => {
44
+ if let Some ( t) = $last_type {
45
+ debug_assert!( t <= $type) ;
46
+ }
47
+ $last_type = Some ( $type) ;
48
+ } ;
49
+ }
50
+
37
51
macro_rules! encode_tlv_stream {
38
52
( $stream: expr, { $( ( $type: expr, $field: expr, $fieldty: tt) ) ,* $( , ) * } ) => { {
39
53
#[ allow( unused_imports) ]
@@ -52,10 +66,7 @@ macro_rules! encode_tlv_stream {
52
66
{
53
67
let mut last_seen: Option <u64 > = None ;
54
68
$(
55
- if let Some ( t) = last_seen {
56
- debug_assert!( t <= $type) ;
57
- }
58
- last_seen = Some ( $type) ;
69
+ _check_tlv_ordering!( last_seen, $type, $fieldty) ;
59
70
) *
60
71
}
61
72
} }
@@ -65,6 +76,8 @@ macro_rules! get_varint_length_prefixed_tlv_length {
65
76
( $len: expr, $type: expr, $field: expr, ( default_value, $default: expr) ) => {
66
77
get_varint_length_prefixed_tlv_length!( $len, $type, $field, required)
67
78
} ;
79
+ ( $len: expr, $type: expr, $field: expr, ( reset_on_reload, $value: expr) ) => {
80
+ } ;
68
81
( $len: expr, $type: expr, $field: expr, required) => {
69
82
BigSize ( $type) . write( & mut $len) . expect( "No in-memory data may fail to serialize" ) ;
70
83
let field_len = $field. serialized_length( ) ;
@@ -108,6 +121,8 @@ macro_rules! check_tlv_order {
108
121
$field = $default. into( ) ;
109
122
}
110
123
} } ;
124
+ ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( reset_on_reload, $value: expr) ) => {
125
+ } ;
111
126
( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => { {
112
127
#[ allow( unused_comparisons) ] // Note that $type may be 0 making the second comparison always true
113
128
let invalid_order = ( $last_seen_type. is_none( ) || $last_seen_type. unwrap( ) < $type) && $typ. 0 > $type;
@@ -140,6 +155,9 @@ macro_rules! check_missing_tlv {
140
155
$field = $default. into( ) ;
141
156
}
142
157
} } ;
158
+ ( $last_seen_type: expr, $type: expr, $field: expr, ( reset_on_reload, $value: expr) ) => {
159
+ $field = $value;
160
+ } ;
143
161
( $last_seen_type: expr, $type: expr, $field: ident, required) => { {
144
162
#[ allow( unused_comparisons) ] // Note that $type may be 0 making the second comparison always true
145
163
let missing_req_type = $last_seen_type. is_none( ) || $last_seen_type. unwrap( ) < $type;
@@ -168,6 +186,8 @@ macro_rules! decode_tlv {
168
186
( $reader: expr, $field: ident, ( default_value, $default: expr) ) => { {
169
187
decode_tlv!( $reader, $field, required)
170
188
} } ;
189
+ ( $reader: expr, $field: ident, ( reset_on_reload, $value: expr) ) => { {
190
+ } } ;
171
191
( $reader: expr, $field: ident, required) => { {
172
192
$field = $crate:: util:: ser:: Readable :: read( & mut $reader) ?;
173
193
} } ;
@@ -195,6 +215,11 @@ macro_rules! decode_tlv {
195
215
} } ;
196
216
}
197
217
218
+ macro_rules! _decode_tlv_stream_match_check {
219
+ ( $val: ident, $type: expr, ( reset_on_reload, $value: expr) ) => { false } ;
220
+ ( $val: ident, $type: expr, $fieldty: tt) => { $val == $type }
221
+ }
222
+
198
223
// `$decode_custom_tlv` is a closure that may be optionally provided to handle custom message types.
199
224
// If it is provided, it will be called with the custom type and the `FixedLengthReader` containing
200
225
// the message contents. It should return `Ok(true)` if the custom message is successfully parsed,
@@ -264,7 +289,7 @@ macro_rules! decode_tlv_stream_range {
264
289
let length: ser:: BigSize = $crate:: util:: ser:: Readable :: read( & mut stream_ref) ?;
265
290
let mut s = ser:: FixedLengthReader :: new( & mut stream_ref, length. 0 ) ;
266
291
match typ. 0 {
267
- $( $type => {
292
+ $( _t if _decode_tlv_stream_match_check! ( _t , $type, $fieldty ) => {
268
293
decode_tlv!( s, $field, $fieldty) ;
269
294
if s. bytes_remain( ) {
270
295
s. eat_remaining( ) ?; // Return ShortRead if there's actually not enough bytes
@@ -405,6 +430,9 @@ macro_rules! init_tlv_based_struct_field {
405
430
( $field: ident, ( default_value, $default: expr) ) => {
406
431
$field. 0 . unwrap( )
407
432
} ;
433
+ ( $field: ident, ( reset_on_reload, $value: expr) ) => {
434
+ $field
435
+ } ;
408
436
( $field: ident, option) => {
409
437
$field
410
438
} ;
@@ -420,6 +448,9 @@ macro_rules! init_tlv_field_var {
420
448
( $field: ident, ( default_value, $default: expr) ) => {
421
449
let mut $field = $crate:: util:: ser:: OptionDeserWrapper ( None ) ;
422
450
} ;
451
+ ( $field: ident, ( reset_on_reload, $value: expr) ) => {
452
+ let $field;
453
+ } ;
423
454
( $field: ident, required) => {
424
455
let mut $field = $crate:: util:: ser:: OptionDeserWrapper ( None ) ;
425
456
} ;
0 commit comments