]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Improve TLV serialization macro callability very slightly
authorMatt Corallo <git@bluematt.me>
Mon, 5 Jul 2021 23:18:41 +0000 (23:18 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 13 Aug 2021 23:02:23 +0000 (23:02 +0000)
This allows decode_tlv_stream!() to be called with either a mutable
reference to a stream or a stream itself and allows
encode_tlv_stream!() to be called with an excess , at the end of
the parameter list.

lightning/src/util/ser_macros.rs

index 960fae45d26181d806e95c3da1884aa7be3f930a..0ad9710a794b11f6e545a4a9e3ac627dc634f654 100644 (file)
@@ -159,6 +159,7 @@ macro_rules! decode_tlv_stream {
        ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { {
                use ln::msgs::DecodeError;
                let mut last_seen_type: Option<u64> = None;
+               let mut stream_ref = $stream;
                'tlv_read: loop {
                        use util::ser;
 
@@ -168,7 +169,7 @@ macro_rules! decode_tlv_stream {
                                // determine whether we should break or return ShortRead if we get an
                                // UnexpectedEof. This should in every case be largely cosmetic, but its nice to
                                // pass the TLV test vectors exactly, which requre this distinction.
-                               let mut tracking_reader = ser::ReadTrackingReader::new($stream);
+                               let mut tracking_reader = ser::ReadTrackingReader::new(&mut stream_ref);
                                match ser::Readable::read(&mut tracking_reader) {
                                        Err(DecodeError::ShortRead) => {
                                                if !tracking_reader.have_read {
@@ -196,8 +197,8 @@ macro_rules! decode_tlv_stream {
                        last_seen_type = Some(typ.0);
 
                        // Finally, read the length and value itself:
-                       let length: ser::BigSize = ser::Readable::read($stream)?;
-                       let mut s = ser::FixedLengthReader::new($stream, length.0);
+                       let length: ser::BigSize = ser::Readable::read(&mut stream_ref)?;
+                       let mut s = ser::FixedLengthReader::new(&mut stream_ref, length.0);
                        match typ.0 {
                                $($type => {
                                        decode_tlv!(s, $field, $fieldty);