From: Matt Corallo Date: Sun, 30 May 2021 23:47:52 +0000 (+0000) Subject: Add generic serialization implementations for Boxs and 3-tuples X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=097c5da22893bcf07025a714a525871636e1a772;p=rust-lightning Add generic serialization implementations for Boxs and 3-tuples These are used in the next commit(s). --- diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index a49b033db..4e69a37d2 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 44bd9ec49..78c3b8096 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);