X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser_macros.rs;h=0aefaf383062b3d8ddcafbeb1465242ccf802e95;hb=30b9d9fbeaa62537beb8d3ea0b2866703d0d7c92;hp=afd7fcb2e0fc93b3f536a052c15cec841f8b43b0;hpb=a03db3ca309413c3408f5971b5ed5a8020422a81;p=rust-lightning diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index afd7fcb2..0aefaf38 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -286,6 +286,9 @@ macro_rules! _decode_tlv { ($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{ $field = Some($trait::read(&mut $reader $(, $read_arg)*)?); }}; + ($reader: expr, $field: ident, (option, encoding: ($fieldty: ty, $encoding: ident, $encoder:ty))) => {{ + $crate::_decode_tlv!($reader, $field, (option, encoding: ($fieldty, $encoding))); + }}; ($reader: expr, $field: ident, (option, encoding: ($fieldty: ty, $encoding: ident))) => {{ $field = { let field: $encoding<$fieldty> = ser::Readable::read(&mut $reader)?; @@ -457,20 +460,50 @@ macro_rules! _decode_tlv_stream_range { } } } +/// Implements [`Readable`]/[`Writeable`] for a message struct that may include non-TLV and +/// TLV-encoded parts. +/// +/// This is useful to implement a [`CustomMessageReader`]. +/// +/// Currently `$fieldty` may only be `option`, i.e., `$tlvfield` is optional field. +/// +/// For example, +/// ``` +/// # use lightning::impl_writeable_msg; +/// struct MyCustomMessage { +/// pub field_1: u32, +/// pub field_2: bool, +/// pub field_3: String, +/// pub tlv_optional_integer: Option, +/// } +/// +/// impl_writeable_msg!(MyCustomMessage, { +/// field_1, +/// field_2, +/// field_3 +/// }, { +/// (1, tlv_optional_integer, option), +/// }); +/// ``` +/// +/// [`Readable`]: crate::util::ser::Readable +/// [`Writeable`]: crate::util::ser::Writeable +/// [`CustomMessageReader`]: crate::ln::wire::CustomMessageReader +#[macro_export] macro_rules! impl_writeable_msg { ($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => { impl $crate::util::ser::Writeable for $st { fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { $( self.$field.write(w)?; )* - encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*}); + $crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*}); Ok(()) } } impl $crate::util::ser::Readable for $st { fn read(r: &mut R) -> Result { $(let $field = $crate::util::ser::Readable::read(r)?;)* - $(_init_tlv_field_var!($tlvfield, $fieldty);)* - decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*}); + $($crate::_init_tlv_field_var!($tlvfield, $fieldty);)* + $crate::decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*}); Ok(Self { $($field),*, $($tlvfield),* @@ -642,10 +675,10 @@ macro_rules! _init_and_read_tlv_fields { } /// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs -/// If `$fieldty` is `required`, then `$field` is a required field that is not an Option nor a Vec. +/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`]. /// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present. /// If `$fieldty` is `option`, then `$field` is optional field. -/// If `$fieldty` is `vec_type`, then `$field` is a Vec, which needs to have its individual elements serialized. +/// If `$fieldty` is `vec_type`, then `$field` is a [`Vec`], which needs to have its individual elements serialized. /// /// For example, /// ``` @@ -730,7 +763,8 @@ macro_rules! tlv_stream { )* } - #[derive(Debug, PartialEq)] + #[cfg_attr(test, derive(PartialEq))] + #[derive(Debug)] pub(super) struct $nameref<'a> { $( pub(super) $field: Option, @@ -770,6 +804,7 @@ macro_rules! tlv_stream { macro_rules! tlv_record_type { (($type:ty, $wrapper:ident)) => { $type }; + (($type:ty, $wrapper:ident, $encoder:ty)) => { $type }; ($type:ty) => { $type }; } @@ -780,6 +815,7 @@ macro_rules! tlv_record_ref_type { ((u32, $wrapper: ident)) => { u32 }; ((u64, $wrapper: ident)) => { u64 }; (($type:ty, $wrapper:ident)) => { &'a $type }; + (($type:ty, $wrapper:ident, $encoder:ty)) => { $encoder }; ($type:ty) => { &'a $type }; }