@@ -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) ?;
@@ -28,6 +31,17 @@ macro_rules! encode_tlv {
28
31
} ;
29
32
}
30
33
34
+
35
+ macro_rules! _check_tlv_ordering {
36
+ ( $last_type: expr, $type: expr, ( reset_on_reload, $value: expr) ) => { } ;
37
+ ( $last_type: expr, $type: expr, $fieldty: tt) => {
38
+ if let Some ( t) = $last_type {
39
+ debug_assert!( t <= $type) ;
40
+ }
41
+ $last_type = Some ( $type) ;
42
+ } ;
43
+ }
44
+
31
45
macro_rules! encode_tlv_stream {
32
46
( $stream: expr, { $( ( $type: expr, $field: expr, $fieldty: tt) ) ,* $( , ) * } ) => { {
33
47
#[ allow( unused_imports) ]
@@ -46,10 +60,7 @@ macro_rules! encode_tlv_stream {
46
60
{
47
61
let mut last_seen: Option <u64 > = None ;
48
62
$(
49
- if let Some ( t) = last_seen {
50
- debug_assert!( t <= $type) ;
51
- }
52
- last_seen = Some ( $type) ;
63
+ _check_tlv_ordering!( last_seen, $type, $fieldty) ;
53
64
) *
54
65
}
55
66
} }
@@ -59,6 +70,8 @@ macro_rules! get_varint_length_prefixed_tlv_length {
59
70
( $len: expr, $type: expr, $field: expr, ( default_value, $default: expr) ) => {
60
71
get_varint_length_prefixed_tlv_length!( $len, $type, $field, required)
61
72
} ;
73
+ ( $len: expr, $type: expr, $field: expr, ( reset_on_reload, $value: expr) ) => {
74
+ } ;
62
75
( $len: expr, $type: expr, $field: expr, required) => {
63
76
BigSize ( $type) . write( & mut $len) . expect( "No in-memory data may fail to serialize" ) ;
64
77
let field_len = $field. serialized_length( ) ;
@@ -102,6 +115,8 @@ macro_rules! check_tlv_order {
102
115
$field = $default. into( ) ;
103
116
}
104
117
} } ;
118
+ ( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( reset_on_reload, $value: expr) ) => {
119
+ } ;
105
120
( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => { {
106
121
#[ allow( unused_comparisons) ] // Note that $type may be 0 making the second comparison always true
107
122
let invalid_order = ( $last_seen_type. is_none( ) || $last_seen_type. unwrap( ) < $type) && $typ. 0 > $type;
@@ -131,6 +146,9 @@ macro_rules! check_missing_tlv {
131
146
$field = $default. into( ) ;
132
147
}
133
148
} } ;
149
+ ( $last_seen_type: expr, $type: expr, $field: expr, ( reset_on_reload, $value: expr) ) => {
150
+ $field = $value;
151
+ } ;
134
152
( $last_seen_type: expr, $type: expr, $field: ident, required) => { {
135
153
#[ allow( unused_comparisons) ] // Note that $type may be 0 making the second comparison always true
136
154
let missing_req_type = $last_seen_type. is_none( ) || $last_seen_type. unwrap( ) < $type;
@@ -156,6 +174,8 @@ macro_rules! decode_tlv {
156
174
( $reader: expr, $field: ident, ( default_value, $default: expr) ) => { {
157
175
decode_tlv!( $reader, $field, required)
158
176
} } ;
177
+ ( $reader: expr, $field: ident, ( reset_on_reload, $value: expr) ) => { {
178
+ } } ;
159
179
( $reader: expr, $field: ident, required) => { {
160
180
$field = $crate:: util:: ser:: Readable :: read( & mut $reader) ?;
161
181
} } ;
@@ -174,6 +194,11 @@ macro_rules! decode_tlv {
174
194
} } ;
175
195
}
176
196
197
+ macro_rules! _decode_tlv_stream_match_check {
198
+ ( $val: ident, $type: expr, ( reset_on_reload, $value: expr) ) => { false } ;
199
+ ( $val: ident, $type: expr, $fieldty: tt) => { $val == $type }
200
+ }
201
+
177
202
// `$decode_custom_tlv` is a closure that may be optionally provided to handle custom message types.
178
203
// If it is provided, it will be called with the custom type and the `FixedLengthReader` containing
179
204
// the message contents. It should return `Ok(true)` if the custom message is successfully parsed,
@@ -224,7 +249,7 @@ macro_rules! decode_tlv_stream {
224
249
let length: ser:: BigSize = $crate:: util:: ser:: Readable :: read( & mut stream_ref) ?;
225
250
let mut s = ser:: FixedLengthReader :: new( & mut stream_ref, length. 0 ) ;
226
251
match typ. 0 {
227
- $( $type => {
252
+ $( _t if _decode_tlv_stream_match_check! ( _t , $type, $fieldty ) => {
228
253
decode_tlv!( s, $field, $fieldty) ;
229
254
if s. bytes_remain( ) {
230
255
s. eat_remaining( ) ?; // Return ShortRead if there's actually not enough bytes
@@ -365,6 +390,9 @@ macro_rules! init_tlv_based_struct_field {
365
390
( $field: ident, ( default_value, $default: expr) ) => {
366
391
$field. 0 . unwrap( )
367
392
} ;
393
+ ( $field: ident, ( reset_on_reload, $value: expr) ) => {
394
+ $field
395
+ } ;
368
396
( $field: ident, option) => {
369
397
$field
370
398
} ;
@@ -380,6 +408,9 @@ macro_rules! init_tlv_field_var {
380
408
( $field: ident, ( default_value, $default: expr) ) => {
381
409
let mut $field = $crate:: util:: ser:: OptionDeserWrapper ( None ) ;
382
410
} ;
411
+ ( $field: ident, ( reset_on_reload, $value: expr) ) => {
412
+ let $field;
413
+ } ;
383
414
( $field: ident, required) => {
384
415
let mut $field = $crate:: util:: ser:: OptionDeserWrapper ( None ) ;
385
416
} ;
0 commit comments