From 169b2604935f4992528346bbd5e8b393d58227c5 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Mon, 21 Oct 2024 14:43:12 -0500 Subject: [PATCH] Refactor tuple serialization into a macro --- lightning/src/util/ser.rs | 137 ++++++++------------------------------ 1 file changed, 28 insertions(+), 109 deletions(-) diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 2c88f9134..73dde00e7 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -1398,119 +1398,38 @@ impl Writeable for RwLock { } } -impl Readable for (A, B) { - fn read(r: &mut R) -> Result { - let a: A = Readable::read(r)?; - let b: B = Readable::read(r)?; - Ok((a, b)) - } -} -impl Writeable for (A, B) { - fn write(&self, w: &mut W) -> Result<(), io::Error> { - self.0.write(w)?; - 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<(), io::Error> { - self.0.write(w)?; - self.1.write(w)?; - self.2.write(w) - } -} - -impl Readable for (A, B, C, D) { - fn read(r: &mut R) -> Result { - let a: A = Readable::read(r)?; - let b: B = Readable::read(r)?; - let c: C = Readable::read(r)?; - let d: D = Readable::read(r)?; - Ok((a, b, c, d)) - } -} -impl Writeable for (A, B, C, D) { - fn write(&self, w: &mut W) -> Result<(), io::Error> { - self.0.write(w)?; - self.1.write(w)?; - self.2.write(w)?; - self.3.write(w) - } -} - -impl Readable for (A, B, C, D, E) { - fn read(r: &mut R) -> Result { - let a: A = Readable::read(r)?; - let b: B = Readable::read(r)?; - let c: C = Readable::read(r)?; - let d: D = Readable::read(r)?; - let e: E = Readable::read(r)?; - Ok((a, b, c, d, e)) - } -} -impl Writeable for (A, B, C, D, E) { - fn write(&self, w: &mut W) -> Result<(), io::Error> { - self.0.write(w)?; - self.1.write(w)?; - self.2.write(w)?; - self.3.write(w)?; - self.4.write(w) - } -} +macro_rules! impl_tuple_ser { + ($($i: ident : $type: tt),*) => { + impl<$($type),*> Readable for ($($type),*) + where $( + $type: Readable, + )* + { + fn read(r: &mut R) -> Result { + Ok(($(<$type as Readable>::read(r)?),*)) + } + } -impl Readable for (A, B, C, D, E, F) { - fn read(r: &mut R) -> Result { - let a: A = Readable::read(r)?; - let b: B = Readable::read(r)?; - let c: C = Readable::read(r)?; - let d: D = Readable::read(r)?; - let e: E = Readable::read(r)?; - let f: F = Readable::read(r)?; - Ok((a, b, c, d, e, f)) - } -} -impl Writeable for (A, B, C, D, E, F) { - fn write(&self, w: &mut W) -> Result<(), io::Error> { - self.0.write(w)?; - self.1.write(w)?; - self.2.write(w)?; - self.3.write(w)?; - self.4.write(w)?; - self.5.write(w) + impl<$($type),*> Writeable for ($($type),*) + where $( + $type: Writeable, + )* + { + fn write(&self, w: &mut W) -> Result<(), io::Error> { + let ($($i),*) = self; + $($i.write(w)?;)* + Ok(()) + } + } } } -impl Readable for (A, B, C, D, E, F, G) { - fn read(r: &mut R) -> Result { - let a: A = Readable::read(r)?; - let b: B = Readable::read(r)?; - let c: C = Readable::read(r)?; - let d: D = Readable::read(r)?; - let e: E = Readable::read(r)?; - let f: F = Readable::read(r)?; - let g: G = Readable::read(r)?; - Ok((a, b, c, d, e, f, g)) - } -} -impl Writeable for (A, B, C, D, E, F, G) { - fn write(&self, w: &mut W) -> Result<(), io::Error> { - self.0.write(w)?; - self.1.write(w)?; - self.2.write(w)?; - self.3.write(w)?; - self.4.write(w)?; - self.5.write(w)?; - self.6.write(w) - } -} +impl_tuple_ser!(a: A, b: B); +impl_tuple_ser!(a: A, b: B, c: C); +impl_tuple_ser!(a: A, b: B, c: C, d: D); +impl_tuple_ser!(a: A, b: B, c: C, d: D, e: E); +impl_tuple_ser!(a: A, b: B, c: C, d: D, e: E, f: F); +impl_tuple_ser!(a: A, b: B, c: C, d: D, e: E, f: F, g: G); impl Writeable for () { fn write(&self, _: &mut W) -> Result<(), io::Error> { -- 2.39.5