From: Matt Corallo Date: Thu, 18 Oct 2018 18:58:56 +0000 (-0400) Subject: Add a second Readable trait for state args, clean macros slightly X-Git-Tag: v0.0.12~283^2~10 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=062a02da1a93e45e043c9cfeddae09998abe8c6b;p=rust-lightning Add a second Readable trait for state args, clean macros slightly --- 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)?),*