@@ -15,25 +15,36 @@ use util::byte_utils::{be64_to_array, be32_to_array, be16_to_array, slice_to_be1
15
15
16
16
const MAX_BUF_SIZE : usize = 64 * 1024 ;
17
17
18
- pub struct Writer < W > { writer : W }
19
- pub struct Reader < R > { reader : R }
20
-
18
+ /// A struct that holds an std::io::Write-impl'ing object and implements various
19
+ /// rust-lightning-specific write functions.
20
+ pub struct Writer < W : Write > { writer : W }
21
+ /// A struct that holds an std::io::Read-impl'ing object and implements various
22
+ /// rust-lightning-specific read functions.
23
+ pub struct Reader < R : Read > { reader : R }
24
+
25
+ /// A trait that various rust-lightning types implement allowing them to be written out to a Writer
21
26
pub trait Writeable < W : Write > {
27
+ /// Writes self out to the given Writer
22
28
fn write ( & self , writer : & mut Writer < W > ) -> Result < ( ) , DecodeError > ;
23
29
}
24
30
31
+ /// A trait that various rust-lightning types implement allowing them to be read in from a Reader
25
32
pub trait Readable < R >
26
33
where Self : Sized ,
27
34
R : Read
28
35
{
36
+ /// Reads a Self in from the given Reader
29
37
fn read ( reader : & mut Reader < R > ) -> Result < Self , DecodeError > ;
30
38
}
31
39
32
40
impl < W : Write > Writer < W > {
41
+ /// Creates a new Writer from an std::io::Write-impl'ing object
33
42
pub fn new ( writer : W ) -> Writer < W > {
34
43
return Writer { writer }
35
44
}
45
+ /// Consumes this object and returns the original writer
36
46
pub fn into_inner ( self ) -> W { self . writer }
47
+ /// Gets a reference to the original writer
37
48
pub fn get_ref ( & self ) -> & W { & self . writer }
38
49
fn write_u64 ( & mut self , v : u64 ) -> Result < ( ) , DecodeError > {
39
50
Ok ( self . writer . write_all ( & be64_to_array ( v) ) ?)
@@ -50,16 +61,19 @@ impl<W: Write> Writer<W> {
50
61
fn write_bool ( & mut self , v : bool ) -> Result < ( ) , DecodeError > {
51
62
Ok ( self . writer . write_all ( & [ if v { 1 } else { 0 } ] ) ?)
52
63
}
53
- pub fn write_all ( & mut self , v : & [ u8 ] ) -> Result < ( ) , DecodeError > {
64
+ pub ( crate ) fn write_all ( & mut self , v : & [ u8 ] ) -> Result < ( ) , DecodeError > {
54
65
Ok ( self . writer . write_all ( v) ?)
55
66
}
56
67
}
57
68
58
69
impl < R : Read > Reader < R > {
70
+ /// Creates a new Reader from an std::io::Read-impl'ing object
59
71
pub fn new ( reader : R ) -> Reader < R > {
60
72
return Reader { reader }
61
73
}
74
+ /// Consumes this object and returns the original reader
62
75
pub fn into_inner ( self ) -> R { self . reader }
76
+ /// Gets a reference to the original reader
63
77
pub fn get_ref ( & self ) -> & R { & self . reader }
64
78
65
79
fn read_u64 ( & mut self ) -> Result < u64 , DecodeError > {
@@ -93,10 +107,10 @@ impl<R: Read> Reader<R> {
93
107
}
94
108
Ok ( buf[ 0 ] == 1 )
95
109
}
96
- pub fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> Result < ( ) , DecodeError > {
110
+ pub ( crate ) fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> Result < ( ) , DecodeError > {
97
111
Ok ( self . reader . read_exact ( buf) ?)
98
112
}
99
- pub fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> Result < usize , DecodeError > {
113
+ pub ( crate ) fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> Result < usize , DecodeError > {
100
114
Ok ( self . reader . read_to_end ( buf) ?)
101
115
}
102
116
}
@@ -306,22 +320,3 @@ impl<R: Read> Readable<R> for Signature {
306
320
}
307
321
}
308
322
}
309
-
310
- macro_rules! impl_writeable {
311
- ( $st: ident, { $( $field: ident) ,* } ) => {
312
- impl <W : :: std:: io:: Write > Writeable <W > for $st {
313
- fn write( & self , w: & mut Writer <W >) -> Result <( ) , DecodeError > {
314
- $( self . $field. write( w) ?; ) *
315
- Ok ( ( ) )
316
- }
317
- }
318
-
319
- impl <R : :: std:: io:: Read > Readable <R > for $st {
320
- fn read( r: & mut Reader <R >) -> Result <Self , DecodeError > {
321
- Ok ( Self {
322
- $( $field: Readable :: read( r) ?) ,*
323
- } )
324
- }
325
- }
326
- }
327
- }
0 commit comments