Skip to content

Commit 1c82cf9

Browse files
committed
f - refactor tlv_stream-related macros
1 parent 9ed915e commit 1c82cf9

File tree

1 file changed

+22
-58
lines changed

1 file changed

+22
-58
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ macro_rules! encode_tlv {
2626
field.write($stream)?;
2727
}
2828
};
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);
3634
};
3735
}
3836

@@ -186,12 +184,15 @@ macro_rules! decode_tlv {
186184
($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
187185
$field = Some($trait::read(&mut $reader $(, $read_arg)*)?);
188186
}};
189-
($reader: expr, $field: ident, (tlv_record, $fieldty:tt)) => {{
187+
($reader: expr, $field: ident, (tlv_record, ($fieldty:ty, $wrapper:ident))) => {{
190188
$field = {
191-
let field: encoded_tlv_record_type!($fieldty) = ser::Readable::read(&mut $reader)?;
189+
let field: $wrapper<$fieldty> = ser::Readable::read(&mut $reader)?;
192190
Some(field.into())
193191
};
194192
}};
193+
($reader: expr, $field: ident, (tlv_record, $fieldty:ty)) => {{
194+
decode_tlv!($reader, $field, option);
195+
}};
195196
}
196197

197198
macro_rules! decode_tlv_stream {
@@ -453,7 +454,9 @@ macro_rules! impl_writeable_tlv_based {
453454
/// Defines a struct for a TLV stream and a similar struct using references for non-primitive types,
454455
/// implementing [`Readable`] for the former and [`Writeable`] for the latter. Useful as an
455456
/// 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.
457460
///
458461
/// [`Readable`]: crate::util::ser::Readable
459462
/// [`Writeable`]: crate::util::ser::Writeable
@@ -503,57 +506,18 @@ macro_rules! tlv_stream {
503506
}
504507

505508
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 };
512511
}
513512

514513
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 };
557521
}
558522

559523
macro_rules! _impl_writeable_tlv_based_enum_common {

0 commit comments

Comments
 (0)