Skip to content

Commit 6ce8026

Browse files
Support deserializing an Option-al MaybeReadable
Prior to this change, our impl_writeable_tlv_based macros only supported deserializing a MaybeReadable if it's non-Optional.
1 parent f6d777f commit 6ce8026

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ macro_rules! _encode_tlv {
4242
($stream: expr, $type: expr, $field: expr, ignorable) => {
4343
$crate::_encode_tlv!($stream, $type, $field, required);
4444
};
45+
($stream: expr, $type: expr, $field: expr, ignorable_option) => {
46+
$crate::_encode_tlv!($stream, $type, $field, option);
47+
};
4548
($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
4649
$crate::_encode_tlv!($stream, $type, $field.map(|f| $encoding(f)), option);
4750
};
@@ -161,6 +164,9 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
161164
($len: expr, $type: expr, $field: expr, ignorable) => {
162165
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
163166
};
167+
($len: expr, $type: expr, $field: expr, ignorable_option) => {
168+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
169+
};
164170
}
165171

166172
/// See the documentation of [`write_tlv_fields`].
@@ -213,6 +219,9 @@ macro_rules! _check_decoded_tlv_order {
213219
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ignorable) => {{
214220
// no-op
215221
}};
222+
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ignorable_option) => {{
223+
// no-op
224+
}};
216225
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
217226
// no-op
218227
}};
@@ -252,6 +261,9 @@ macro_rules! _check_missing_tlv {
252261
($last_seen_type: expr, $type: expr, $field: ident, ignorable) => {{
253262
// no-op
254263
}};
264+
($last_seen_type: expr, $type: expr, $field: ident, ignorable_option) => {{
265+
// no-op
266+
}};
255267
($last_seen_type: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
256268
// no-op
257269
}};
@@ -283,6 +295,9 @@ macro_rules! _decode_tlv {
283295
($reader: expr, $field: ident, ignorable) => {{
284296
$field = $crate::util::ser::MaybeReadable::read(&mut $reader)?;
285297
}};
298+
($reader: expr, $field: ident, ignorable_option) => {{
299+
$field = $crate::util::ser::MaybeReadable::read(&mut $reader)?;
300+
}};
286301
($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
287302
$field = Some($trait::read(&mut $reader $(, $read_arg)*)?);
288303
}};
@@ -623,6 +638,9 @@ macro_rules! _init_tlv_based_struct_field {
623638
($field: ident, ignorable) => {
624639
if $field.is_none() { return Ok(None); } else { $field.unwrap() }
625640
};
641+
($field: ident, ignorable_option) => {
642+
$field
643+
};
626644
($field: ident, required) => {
627645
$field.0.unwrap()
628646
};
@@ -655,6 +673,9 @@ macro_rules! _init_tlv_field_var {
655673
($field: ident, ignorable) => {
656674
let mut $field = None;
657675
};
676+
($field: ident, ignorable_option) => {
677+
let mut $field = None;
678+
};
658679
}
659680

660681
/// Equivalent to running [`_init_tlv_field_var`] then [`read_tlv_fields`].

0 commit comments

Comments
 (0)