Skip to content

Commit 35ad8bb

Browse files
committed
Change the legacy write-side expr to a lambda which takes self
In the previous commit we added the ability to write legacy fields without having a corresponding fields in the in-memory encoding of structs and enums and without breaking out of the high-level utility macros like `impl_writeable_tlv_based`. However, the write-side expr was required to be a static expr, without the ability to access `self`, limiting its ability to write useful fields. Here we swap it for a `Fn` which takes the object being serialized.
1 parent 85243c7 commit 35ad8bb

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

lightning/src/util/ser_macros.rs

+43-43
Original file line numberDiff line numberDiff line change
@@ -33,48 +33,48 @@
3333
#[doc(hidden)]
3434
#[macro_export]
3535
macro_rules! _encode_tlv {
36-
($stream: expr, $type: expr, $field: expr, (default_value, $default: expr)) => {
36+
($stream: expr, $type: expr, $field: expr, (default_value, $default: expr) $(, $self: ident)?) => {
3737
$crate::_encode_tlv!($stream, $type, $field, required)
3838
};
39-
($stream: expr, $type: expr, $field: expr, (static_value, $value: expr)) => {
39+
($stream: expr, $type: expr, $field: expr, (static_value, $value: expr) $(, $self: ident)?) => {
4040
let _ = &$field; // Ensure we "use" the $field
4141
};
42-
($stream: expr, $type: expr, $field: expr, required) => {
42+
($stream: expr, $type: expr, $field: expr, required $(, $self: ident)?) => {
4343
BigSize($type).write($stream)?;
4444
BigSize($field.serialized_length() as u64).write($stream)?;
4545
$field.write($stream)?;
4646
};
47-
($stream: expr, $type: expr, $field: expr, required_vec) => {
47+
($stream: expr, $type: expr, $field: expr, required_vec $(, $self: ident)?) => {
4848
$crate::_encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required);
4949
};
50-
($stream: expr, $optional_type: expr, $optional_field: expr, option) => {
50+
($stream: expr, $optional_type: expr, $optional_field: expr, option $(, $self: ident)?) => {
5151
if let Some(ref field) = $optional_field {
5252
BigSize($optional_type).write($stream)?;
5353
BigSize(field.serialized_length() as u64).write($stream)?;
5454
field.write($stream)?;
5555
}
5656
};
57-
($stream: expr, $optional_type: expr, $optional_field: expr, (legacy, $fieldty: ty, $read: expr, $write: expr)) => {
58-
$crate::_encode_tlv!($stream, $optional_type, $write, option);
57+
($stream: expr, $optional_type: expr, $optional_field: expr, (legacy, $fieldty: ty, $read: expr, $write: expr) $(, $self: ident)?) => {
58+
$crate::_encode_tlv!($stream, $optional_type, $write($($self)?), option);
5959
};
60-
($stream: expr, $type: expr, $field: expr, optional_vec) => {
60+
($stream: expr, $type: expr, $field: expr, optional_vec $(, $self: ident)?) => {
6161
if !$field.is_empty() {
6262
$crate::_encode_tlv!($stream, $type, $field, required_vec);
6363
}
6464
};
65-
($stream: expr, $type: expr, $field: expr, upgradable_required) => {
65+
($stream: expr, $type: expr, $field: expr, upgradable_required $(, $self: ident)?) => {
6666
$crate::_encode_tlv!($stream, $type, $field, required);
6767
};
68-
($stream: expr, $type: expr, $field: expr, upgradable_option) => {
68+
($stream: expr, $type: expr, $field: expr, upgradable_option $(, $self: ident)?) => {
6969
$crate::_encode_tlv!($stream, $type, $field, option);
7070
};
71-
($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
71+
($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident) $(, $self: ident)?)) => {
7272
$crate::_encode_tlv!($stream, $type, $field.map(|f| $encoding(f)), option);
7373
};
74-
($stream: expr, $type: expr, $field: expr, (option, encoding: $fieldty: ty)) => {
74+
($stream: expr, $type: expr, $field: expr, (option, encoding: $fieldty: ty) $(, $self: ident)?) => {
7575
$crate::_encode_tlv!($stream, $type, $field, option);
7676
};
77-
($stream: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
77+
($stream: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?) $(, $self: ident)?) => {
7878
// Just a read-mapped type
7979
$crate::_encode_tlv!($stream, $type, $field, option);
8080
};
@@ -146,10 +146,10 @@ macro_rules! encode_tlv_stream {
146146
#[doc(hidden)]
147147
#[macro_export]
148148
macro_rules! _encode_tlv_stream {
149-
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { {
150-
$crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* }, &[])
149+
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt $(, $self: ident)?)),* $(,)*}) => { {
150+
$crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty $(, $self)?)),* }, &[])
151151
} };
152-
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}, $extra_tlvs: expr) => { {
152+
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt $(, $self: ident)?)),* $(,)*}, $extra_tlvs: expr) => { {
153153
#[allow(unused_imports)]
154154
use $crate::{
155155
ln::msgs::DecodeError,
@@ -159,7 +159,7 @@ macro_rules! _encode_tlv_stream {
159159
};
160160

161161
$(
162-
$crate::_encode_tlv!($stream, $type, $field, $fieldty);
162+
$crate::_encode_tlv!($stream, $type, $field, $fieldty $(, $self)?);
163163
)*
164164
for tlv in $extra_tlvs {
165165
let (typ, value): &(u64, Vec<u8>) = tlv;
@@ -188,23 +188,23 @@ macro_rules! _encode_tlv_stream {
188188
#[doc(hidden)]
189189
#[macro_export]
190190
macro_rules! _get_varint_length_prefixed_tlv_length {
191-
($len: expr, $type: expr, $field: expr, (default_value, $default: expr)) => {
191+
($len: expr, $type: expr, $field: expr, (default_value, $default: expr) $(, $self: ident)?) => {
192192
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required)
193193
};
194-
($len: expr, $type: expr, $field: expr, (static_value, $value: expr)) => {};
195-
($len: expr, $type: expr, $field: expr, required) => {
194+
($len: expr, $type: expr, $field: expr, (static_value, $value: expr) $(, $self: ident)?) => {};
195+
($len: expr, $type: expr, $field: expr, required $(, $self: ident)?) => {
196196
BigSize($type).write(&mut $len).expect("No in-memory data may fail to serialize");
197197
let field_len = $field.serialized_length();
198198
BigSize(field_len as u64)
199199
.write(&mut $len)
200200
.expect("No in-memory data may fail to serialize");
201201
$len.0 += field_len;
202202
};
203-
($len: expr, $type: expr, $field: expr, required_vec) => {
203+
($len: expr, $type: expr, $field: expr, required_vec $(, $self: ident)?) => {
204204
let field = $crate::util::ser::WithoutLength(&$field);
205205
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, field, required);
206206
};
207-
($len: expr, $optional_type: expr, $optional_field: expr, option) => {
207+
($len: expr, $optional_type: expr, $optional_field: expr, option $(, $self: ident)?) => {
208208
if let Some(ref field) = $optional_field {
209209
BigSize($optional_type)
210210
.write(&mut $len)
@@ -216,25 +216,25 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
216216
$len.0 += field_len;
217217
}
218218
};
219-
($len: expr, $optional_type: expr, $optional_field: expr, (legacy, $fieldty: ty, $read: expr, $write: expr)) => {
220-
$crate::_get_varint_length_prefixed_tlv_length!($len, $optional_type, $write, option);
219+
($len: expr, $optional_type: expr, $optional_field: expr, (legacy, $fieldty: ty, $read: expr, $write: expr) $(, $self: ident)?) => {
220+
$crate::_get_varint_length_prefixed_tlv_length!($len, $optional_type, $write($($self)?), option);
221221
};
222-
($len: expr, $type: expr, $field: expr, optional_vec) => {
222+
($len: expr, $type: expr, $field: expr, optional_vec $(, $self: ident)?) => {
223223
if !$field.is_empty() {
224224
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required_vec);
225225
}
226226
};
227-
($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
227+
($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?) $(, $self: ident)?) => {
228228
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
229229
};
230-
($len: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
230+
($len: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident)) $(, $self: ident)?) => {
231231
let field = $field.map(|f| $encoding(f));
232232
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, field, option);
233233
};
234-
($len: expr, $type: expr, $field: expr, upgradable_required) => {
234+
($len: expr, $type: expr, $field: expr, upgradable_required $(, $self: ident)?) => {
235235
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
236236
};
237-
($len: expr, $type: expr, $field: expr, upgradable_option) => {
237+
($len: expr, $type: expr, $field: expr, upgradable_option $(, $self: ident)?) => {
238238
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
239239
};
240240
}
@@ -244,18 +244,18 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
244244
#[doc(hidden)]
245245
#[macro_export]
246246
macro_rules! _encode_varint_length_prefixed_tlv {
247-
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}) => { {
248-
$crate::_encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*}, &[])
247+
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt $(, $self: ident)?)),*}) => { {
248+
$crate::_encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty $(, $self)?)),*}, &[])
249249
} };
250-
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}, $extra_tlvs: expr) => { {
250+
($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt $(, $self: ident)?)),*}, $extra_tlvs: expr) => { {
251251
extern crate alloc;
252252
use $crate::util::ser::BigSize;
253253
use alloc::vec::Vec;
254254
let len = {
255255
#[allow(unused_mut)]
256256
let mut len = $crate::util::ser::LengthCalculatingWriter(0);
257257
$(
258-
$crate::_get_varint_length_prefixed_tlv_length!(len, $type, $field, $fieldty);
258+
$crate::_get_varint_length_prefixed_tlv_length!(len, $type, $field, $fieldty $(, $self)?);
259259
)*
260260
for tlv in $extra_tlvs {
261261
let (typ, value): &(u64, Vec<u8>) = tlv;
@@ -264,7 +264,7 @@ macro_rules! _encode_varint_length_prefixed_tlv {
264264
len.0
265265
};
266266
BigSize(len as u64).write($stream)?;
267-
$crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* }, $extra_tlvs);
267+
$crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty $(, $self)?)),* }, $extra_tlvs);
268268
} };
269269
}
270270

@@ -973,10 +973,10 @@ macro_rules! _decode_and_build {
973973
/// If `$fieldty` is `option`, then `$field` is optional field.
974974
/// If `$fieldty` is `optional_vec`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
975975
/// Note that for `optional_vec` no bytes are written if the vec is empty
976-
/// If `$fieldty` is `(legacy, $ty, $read, $write)` then, when writing, the expression $write will
977-
/// be called which returns an `Option` and is written as a TLV if `Some`. When reading, an
978-
/// optional field of type `$ty` is read. The code in `$read` is always executed after all TLVs
979-
/// have been read.
976+
/// If `$fieldty` is `(legacy, $ty, $read, $write)` then, when writing, the function $write will be
977+
/// called with the object being serialized and a returned `Option` and is written as a TLV if
978+
/// `Some`. When reading, an optional field of type `$ty` is read. The code in `$read` is always
979+
/// executed after all TLVs have been read (and the read object may be accessed by its name).
980980
///
981981
/// For example,
982982
/// ```
@@ -1004,8 +1004,8 @@ macro_rules! impl_writeable_tlv_based {
10041004
($st: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
10051005
impl $crate::util::ser::Writeable for $st {
10061006
fn write<W: $crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
1007-
$crate::write_tlv_fields!(writer, {
1008-
$(($type, self.$field, $fieldty)),*
1007+
$crate::_encode_varint_length_prefixed_tlv!(writer, {
1008+
$(($type, self.$field, $fieldty, self)),*
10091009
});
10101010
Ok(())
10111011
}
@@ -1017,7 +1017,7 @@ macro_rules! impl_writeable_tlv_based {
10171017
#[allow(unused_mut)]
10181018
let mut len = $crate::util::ser::LengthCalculatingWriter(0);
10191019
$(
1020-
$crate::_get_varint_length_prefixed_tlv_length!(len, $type, self.$field, $fieldty);
1020+
$crate::_get_varint_length_prefixed_tlv_length!(len, $type, self.$field, $fieldty, self);
10211021
)*
10221022
len.0
10231023
};
@@ -1133,8 +1133,8 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
11331133
$($st::$variant_name { $(ref $field: $fieldty, )* .. } => {
11341134
let id: u8 = $variant_id;
11351135
id.write(writer)?;
1136-
$crate::write_tlv_fields!(writer, {
1137-
$(($type, *$field, $fieldty)),*
1136+
$crate::_encode_varint_length_prefixed_tlv!(writer, {
1137+
$(($type, *$field, $fieldty, self)),*
11381138
});
11391139
}),*
11401140
$($st::$tuple_variant_name (ref field) => {

0 commit comments

Comments
 (0)