From 68313da695f421a66f7870b4812347125ca78276 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 30 May 2021 23:47:52 +0000 Subject: [PATCH] Add generic serialization implementations for Boxs and 3-tuples These are used in the next commit(s). --- lightning/src/util/ser.rs | 28 ++++++++++++++++++++++++++++ lightning/src/util/ser_macros.rs | 9 +-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index a49b033d..4e69a37d 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -698,6 +698,18 @@ impl Readable for PaymentSecret { } } +impl Writeable for Box { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + T::write(&**self, w) + } +} + +impl Readable for Box { + fn read(r: &mut R) -> Result { + Ok(Box::new(Readable::read(r)?)) + } +} + impl Writeable for Option { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { match *self { @@ -824,3 +836,19 @@ impl Writeable for (A, B) { self.1.write(w) } } + +impl Readable for (A, B, C) { + fn read(r: &mut R) -> Result { + let a: A = Readable::read(r)?; + let b: B = Readable::read(r)?; + let c: C = Readable::read(r)?; + Ok((a, b, c)) + } +} +impl Writeable for (A, B, C) { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + self.0.write(w)?; + self.1.write(w)?; + self.2.write(w) + } +} diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 44bd9ec4..78c3b809 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -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(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>, Option, Option<(PublicKey, u64, u64)>, Option), DecodeError> { let mut s = Cursor::new(s); -- 2.30.2