]> git.bitcoin.ninja Git - rust-lightning/blob - src/util/ser_macros.rs
Merge pull request #223 from TheBlueMatt/2018-10-chanmanager-serialize
[rust-lightning] / src / util / ser_macros.rs
1 macro_rules! impl_writeable {
2         ($st:ident, $len: expr, {$($field:ident),*}) => {
3                 impl ::util::ser::Writeable for $st {
4                         fn write<W: ::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
5                                 if $len != 0 {
6                                         w.size_hint($len);
7                                 }
8                                 $( self.$field.write(w)?; )*
9                                 Ok(())
10                         }
11                 }
12
13                 impl<R: ::std::io::Read> ::util::ser::Readable<R> for $st {
14                         fn read(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
15                                 Ok(Self {
16                                         $($field: ::util::ser::Readable::read(r)?),*
17                                 })
18                         }
19                 }
20         }
21 }
22 macro_rules! impl_writeable_len_match {
23         ($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
24                 impl Writeable for $st {
25                         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
26                                 w.size_hint(match *self {
27                                         $($m => $l,)*
28                                 });
29                                 $( self.$field.write(w)?; )*
30                                 Ok(())
31                         }
32                 }
33
34                 impl<R: ::std::io::Read> Readable<R> for $st {
35                         fn read(r: &mut R) -> Result<Self, DecodeError> {
36                                 Ok(Self {
37                                         $($field: Readable::read(r)?),*
38                                 })
39                         }
40                 }
41         }
42 }