]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add a second Readable trait for state args, clean macros slightly
authorMatt Corallo <git@bluematt.me>
Thu, 18 Oct 2018 18:58:56 +0000 (14:58 -0400)
committerMatt Corallo <git@bluematt.me>
Sat, 27 Oct 2018 13:42:04 +0000 (09:42 -0400)
src/util/ser.rs
src/util/ser_macros.rs

index 92cc66b81e30af9599f35bcd8cec1bcb088d2713..5429c0e664eb655cb588383cda1a8d5b90937491 100644 (file)
@@ -82,6 +82,16 @@ pub trait Readable<R>
        fn read(reader: &mut R) -> Result<Self, DecodeError>;
 }
 
+/// 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<R, P>
+       where Self: Sized,
+             R: Read
+{
+       /// Reads a Self in from the given Read
+       fn read(reader: &mut R, params: P) -> Result<Self, DecodeError>;
+}
+
 macro_rules! impl_writeable_primitive {
        ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
                impl Writeable for $val_type {
index 09a319fb45951a043106f9ae6d7fe4d13b94779b..900270f31056f20e6d5527798c9184324a6d8325 100644 (file)
@@ -2,13 +2,15 @@ macro_rules! impl_writeable {
        ($st:ident, $len: expr, {$($field:ident),*}) => {
                impl Writeable for $st {
                        fn write<W: Writer>(&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<R: Read> Readable<R> for $st {
+               impl<R: ::std::io::Read> Readable<R> for $st {
                        fn read(r: &mut R) -> Result<Self, DecodeError> {
                                Ok(Self {
                                        $($field: Readable::read(r)?),*
@@ -29,7 +31,7 @@ macro_rules! impl_writeable_len_match {
                        }
                }
 
-               impl<R: Read> Readable<R> for $st {
+               impl<R: ::std::io::Read> Readable<R> for $st {
                        fn read(r: &mut R) -> Result<Self, DecodeError> {
                                Ok(Self {
                                        $($field: Readable::read(r)?),*