From 062a02da1a93e45e043c9cfeddae09998abe8c6b Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 18 Oct 2018 14:58:56 -0400 Subject: [PATCH] Add a second Readable trait for state args, clean macros slightly --- src/util/ser.rs | 10 ++++++++++ src/util/ser_macros.rs | 8 +++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/util/ser.rs b/src/util/ser.rs index 92cc66b81..5429c0e66 100644 --- a/src/util/ser.rs +++ b/src/util/ser.rs @@ -82,6 +82,16 @@ pub trait Readable fn read(reader: &mut R) -> Result; } +/// A trait that various higher-level rust-lightning types implement allowing them to be read in +/// from a Read given some additional set of arguments which is required to deserialize. +pub trait ReadableArgs + where Self: Sized, + R: Read +{ + /// Reads a Self in from the given Read + fn read(reader: &mut R, params: P) -> Result; +} + macro_rules! impl_writeable_primitive { ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => { impl Writeable for $val_type { diff --git a/src/util/ser_macros.rs b/src/util/ser_macros.rs index 09a319fb4..900270f31 100644 --- a/src/util/ser_macros.rs +++ b/src/util/ser_macros.rs @@ -2,13 +2,15 @@ macro_rules! impl_writeable { ($st:ident, $len: expr, {$($field:ident),*}) => { impl Writeable for $st { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { - w.size_hint($len); + if $len != 0 { + w.size_hint($len); + } $( self.$field.write(w)?; )* Ok(()) } } - impl Readable for $st { + impl Readable for $st { fn read(r: &mut R) -> Result { Ok(Self { $($field: Readable::read(r)?),* @@ -29,7 +31,7 @@ macro_rules! impl_writeable_len_match { } } - impl Readable for $st { + impl Readable for $st { fn read(r: &mut R) -> Result { Ok(Self { $($field: Readable::read(r)?),* -- 2.39.5