Skip to content

Commit 6aa5ebb

Browse files
committed
Support ReadableArgs types across in the TLV struct serialization
This adds `required` support for trait-wrapped reading (e.g. for objects read via `ReadableArgs`) as well as support for the trait-wrapped reading syntax across the TLV struct/enum serialization macros.
1 parent 9364154 commit 6aa5ebb

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lightning/src/util/ser.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,23 @@ impl<T: Readable> MaybeReadable for T {
289289
}
290290

291291
/// Wrapper to read a required (non-optional) TLV record.
292-
pub struct RequiredWrapper<T: Readable>(pub Option<T>);
292+
pub struct RequiredWrapper<T>(pub Option<T>);
293293
impl<T: Readable> Readable for RequiredWrapper<T> {
294294
#[inline]
295295
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
296296
Ok(Self(Some(Readable::read(reader)?)))
297297
}
298298
}
299+
impl<A, T: ReadableArgs<A>> ReadableArgs<A> for RequiredWrapper<T> {
300+
#[inline]
301+
fn read<R: Read>(reader: &mut R, args: A) -> Result<Self, DecodeError> {
302+
Ok(Self(Some(ReadableArgs::read(reader, args)?)))
303+
}
304+
}
299305
/// When handling `default_values`, we want to map the default-value T directly
300306
/// to a `RequiredWrapper<T>` in a way that works for `field: T = t;` as
301307
/// well. Thus, we assume `Into<T> for T` does nothing and use that.
302-
impl<T: Readable> From<T> for RequiredWrapper<T> {
308+
impl<T> From<T> for RequiredWrapper<T> {
303309
fn from(t: T) -> RequiredWrapper<T> { RequiredWrapper(Some(t)) }
304310
}
305311

lightning/src/util/ser_macros.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ macro_rules! _encode_tlv {
5151
($stream: expr, $type: expr, $field: expr, (option, encoding: $fieldty: ty)) => {
5252
$crate::_encode_tlv!($stream, $type, $field, option);
5353
};
54+
($stream: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
55+
// Just a read-mapped type
56+
$crate::_encode_tlv!($stream, $type, $field, option);
57+
};
5458
}
5559

5660
/// Panics if the last seen TLV type is not numerically less than the TLV type currently being checked.
@@ -161,6 +165,9 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
161165
$len.0 += field_len;
162166
}
163167
};
168+
($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
169+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
170+
};
164171
($len: expr, $type: expr, $field: expr, upgradable_required) => {
165172
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
166173
};
@@ -210,6 +217,9 @@ macro_rules! _check_decoded_tlv_order {
210217
return Err(DecodeError::InvalidValue);
211218
}
212219
}};
220+
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
221+
$crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
222+
}};
213223
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{
214224
// no-op
215225
}};
@@ -252,6 +262,9 @@ macro_rules! _check_missing_tlv {
252262
return Err(DecodeError::InvalidValue);
253263
}
254264
}};
265+
($last_seen_type: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
266+
$crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
267+
}};
255268
($last_seen_type: expr, $type: expr, $field: ident, vec_type) => {{
256269
// no-op
257270
}};
@@ -285,6 +298,9 @@ macro_rules! _decode_tlv {
285298
($reader: expr, $field: ident, required) => {{
286299
$field = $crate::util::ser::Readable::read(&mut $reader)?;
287300
}};
301+
($reader: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
302+
$field = $trait::read(&mut $reader $(, $read_arg)*)?;
303+
}};
288304
($reader: expr, $field: ident, vec_type) => {{
289305
let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
290306
$field = Some(f.0);
@@ -644,6 +660,9 @@ macro_rules! _init_tlv_based_struct_field {
644660
($field: ident, option) => {
645661
$field
646662
};
663+
($field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {
664+
$crate::_init_tlv_based_struct_field!($field, option)
665+
};
647666
($field: ident, upgradable_required) => {
648667
$field.0.unwrap()
649668
};
@@ -673,12 +692,18 @@ macro_rules! _init_tlv_field_var {
673692
($field: ident, required) => {
674693
let mut $field = $crate::util::ser::RequiredWrapper(None);
675694
};
695+
($field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {
696+
$crate::_init_tlv_field_var!($field, required);
697+
};
676698
($field: ident, vec_type) => {
677699
let mut $field = Some(Vec::new());
678700
};
679701
($field: ident, option) => {
680702
let mut $field = None;
681703
};
704+
($field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {
705+
$crate::_init_tlv_field_var!($field, option);
706+
};
682707
($field: ident, upgradable_required) => {
683708
let mut $field = $crate::util::ser::UpgradableRequired(None);
684709
};

0 commit comments

Comments
 (0)