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