From: Jeffrey Czyz Date: Mon, 21 Oct 2024 19:43:12 +0000 (-0500) Subject: Refactor tuple serialization into a macro X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=169b2604935f4992528346bbd5e8b393d58227c5;p=rust-lightning Refactor tuple serialization into a macro --- 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> {