From: Matt Corallo Date: Sun, 30 May 2021 22:27:37 +0000 (+0000) Subject: Add a macro to implement serialization of enums using TLVs X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=ce5c45de0b333aaa836df16e6bf9194b37e89635;p=rust-lightning Add a macro to implement serialization of enums using TLVs --- diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 45435ecdd..c3829a650 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -325,26 +325,26 @@ macro_rules! read_tlv_fields { // `Self { ,,vecfield: vecfield }` which is obviously incorrect. Instead, we have to match here to // detect at least one empty field set and skip the potentially-extra comma. macro_rules! _init_tlv_based_struct { - ({}, {$($field: ident),*}, {$($vecfield: ident),*}) => { - Ok(Self { + ($($objs: ident)::*, {}, {$($field: ident),*}, {$($vecfield: ident),*}) => { + Ok($($objs)::* { $($field),*, $($vecfield: $vecfield.unwrap().0),* }) }; - ({$($reqfield: ident),*}, {}, {$($vecfield: ident),*}) => { - Ok(Self { + ($($objs: ident)::*, {$($reqfield: ident),*}, {}, {$($vecfield: ident),*}) => { + Ok($($objs)::* { $($reqfield: $reqfield.0.unwrap()),*, $($vecfield: $vecfield.unwrap().0),* }) }; - ({$($reqfield: ident),*}, {$($field: ident),*}, {}) => { - Ok(Self { + ($($objs: ident)::*, {$($reqfield: ident),*}, {$($field: ident),*}, {}) => { + Ok($($objs)::* { $($reqfield: $reqfield.0.unwrap()),*, $($field),* }) }; - ({$($reqfield: ident),*}, {$($field: ident),*}, {$($vecfield: ident),*}) => { - Ok(Self { + ($($objs: ident)::*, {$($reqfield: ident),*}, {$($field: ident),*}, {$($vecfield: ident),*}) => { + Ok($($objs)::* { $($reqfield: $reqfield.0.unwrap()),*, $($field),*, $($vecfield: $vecfield.unwrap().0),* @@ -433,7 +433,87 @@ macro_rules! impl_writeable_tlv_based { }, { $(($vectype, $vecfield)),* }); - _init_tlv_based_struct!({$($reqfield),*}, {$($field),*}, {$($vecfield),*}) + _init_tlv_based_struct!($st, {$($reqfield),*}, {$($field),*}, {$($vecfield),*}) + } + } + } +} + +/// Implement Readable and Writeable for an enum, with struct variants stored as TLVs and tuple +/// variants stored directly. +/// The format is, for example +/// impl_writeable_tlv_based_enum!(EnumName, +/// (0, VariantName) => { (0, variant_field)}, {(1, variant_optional_field)}, {}; +/// (1, TupleVariant), (2, TupleVariantB), +/// ); +/// The type is written as a single byte, followed by any variant data. +/// Attempts to read an unknown type byte result in DecodeError::UnknownRequiredFeature. +macro_rules! impl_writeable_tlv_based_enum { + ($st: ident, $(($variant_id: expr, $variant_name: ident) => + {$(($reqtype: expr, $reqfield: ident)),* $(,)*}, + {$(($type: expr, $field: ident)),* $(,)*}, + {$(($vectype: expr, $vecfield: ident)),* $(,)*} + ),* $(,)*; + $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*) => { + impl ::util::ser::Writeable for $st { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + match self { + $($st::$variant_name { $(ref $reqfield),* $(ref $field),*, $(ref $vecfield),* } => { + let id: u8 = $variant_id; + id.write(writer)?; + _write_tlv_fields!(writer, { + $(($reqtype, $reqfield)),* + }, { + $(($type, $field)),* + }, { + $(($vectype, Some(::util::ser::VecWriteWrapper(&$vecfield)))),* + }); + }),* + $($st::$tuple_variant_name (ref field) => { + let id: u8 = $tuple_variant_id; + id.write(writer)?; + field.write(writer)?; + }),* + } + Ok(()) + } + } + + impl ::util::ser::Readable for $st { + fn read(reader: &mut R) -> Result { + let id: u8 = ::util::ser::Readable::read(reader)?; + match id { + $($variant_id => { + // Because read_tlv_fields creates a labeled loop, we cannot call it twice + // in the same function body. Instead, we define a closure and call it. + let f = || { + $( + let mut $reqfield = ::util::ser::OptionDeserWrapper(None); + )* + $( + let mut $field = None; + )* + $( + let mut $vecfield = Some(::util::ser::VecReadWrapper(Vec::new())); + )* + _read_tlv_fields!(reader, { + $(($reqtype, $reqfield)),* + }, { + $(($type, $field)),* + }, { + $(($vectype, $vecfield)),* + }); + _init_tlv_based_struct!($st::$variant_name, {$($reqfield),*}, {$($field),*}, {$($vecfield),*}) + }; + f() + }),* + $($tuple_variant_id => { + Ok($st::$tuple_variant_name(Readable::read(reader)?)) + }),* + _ => { + Err(DecodeError::UnknownRequiredFeature)? + }, + } } } }