]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add generic serialization implementations for Boxs and 3-tuples
authorMatt Corallo <git@bluematt.me>
Sun, 30 May 2021 23:47:52 +0000 (23:47 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 1 Jun 2021 16:22:51 +0000 (16:22 +0000)
These are used in the next commit(s).

lightning/src/util/ser.rs
lightning/src/util/ser_macros.rs

index a49b033db3d51010073260f8d0023dac5e6a5c1c..4e69a37d20e9fcc5e0d093804136ef9f90be1747 100644 (file)
@@ -698,6 +698,18 @@ impl Readable for PaymentSecret {
        }
 }
 
+impl<T: Writeable> Writeable for Box<T> {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               T::write(&**self, w)
+       }
+}
+
+impl<T: Readable> Readable for Box<T> {
+       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               Ok(Box::new(Readable::read(r)?))
+       }
+}
+
 impl<T: Writeable> Writeable for Option<T> {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
                match *self {
@@ -824,3 +836,19 @@ impl<A: Writeable, B: Writeable> Writeable for (A, B) {
                self.1.write(w)
        }
 }
+
+impl<A: Readable, B: Readable, C: Readable> Readable for (A, B, C) {
+       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               let a: A = Readable::read(r)?;
+               let b: B = Readable::read(r)?;
+               let c: C = Readable::read(r)?;
+               Ok((a, b, c))
+       }
+}
+impl<A: Writeable, B: Writeable, C: Writeable> Writeable for (A, B, C) {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+               self.0.write(w)?;
+               self.1.write(w)?;
+               self.2.write(w)
+       }
+}
index 44bd9ec49729fc1ecb4351c00e086beaf07d9df1..78c3b80962e7a890264b7796710518115381bc59 100644 (file)
@@ -523,7 +523,7 @@ macro_rules! impl_writeable_tlv_based_enum {
 #[cfg(test)]
 mod tests {
        use prelude::*;
-       use std::io::{Cursor, Read};
+       use std::io::Cursor;
        use ln::msgs::DecodeError;
        use util::ser::{Readable, Writeable, HighZeroBytesDroppedVarInt, VecWriter};
        use bitcoin::secp256k1::PublicKey;
@@ -593,13 +593,6 @@ mod tests {
                        (0xdeadbeef1badbeef, 0x1bad1dea, Some(0x01020304)));
        }
 
-       impl Readable for (PublicKey, u64, u64) {
-               #[inline]
-               fn read<R: Read>(reader: &mut R) -> Result<(PublicKey, u64, u64), DecodeError> {
-                       Ok((Readable::read(reader)?, Readable::read(reader)?, Readable::read(reader)?))
-               }
-       }
-
        // BOLT TLV test cases
        fn tlv_reader_n1(s: &[u8]) -> Result<(Option<HighZeroBytesDroppedVarInt<u64>>, Option<u64>, Option<(PublicKey, u64, u64)>, Option<u16>), DecodeError> {
                let mut s = Cursor::new(s);