@@ -15,25 +15,36 @@ use util::byte_utils::{be64_to_array, be32_to_array, be16_to_array, slice_to_be1
1515
1616const MAX_BUF_SIZE : usize = 64 * 1024 ;
1717
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
2126pub trait Writeable < W : Write > {
27+ /// Writes self out to the given Writer
2228 fn write ( & self , writer : & mut Writer < W > ) -> Result < ( ) , DecodeError > ;
2329}
2430
31+ /// A trait that various rust-lightning types implement allowing them to be read in from a Reader
2532pub trait Readable < R >
2633 where Self : Sized ,
2734 R : Read
2835{
36+ /// Reads a Self in from the given Reader
2937 fn read ( reader : & mut Reader < R > ) -> Result < Self , DecodeError > ;
3038}
3139
3240impl < W : Write > Writer < W > {
41+ /// Creates a new Writer from an std::io::Write-impl'ing object
3342 pub fn new ( writer : W ) -> Writer < W > {
3443 return Writer { writer }
3544 }
45+ /// Consumes this object and returns the original writer
3646 pub fn into_inner ( self ) -> W { self . writer }
47+ /// Gets a reference to the original writer
3748 pub fn get_ref ( & self ) -> & W { & self . writer }
3849 fn write_u64 ( & mut self , v : u64 ) -> Result < ( ) , DecodeError > {
3950 Ok ( self . writer . write_all ( & be64_to_array ( v) ) ?)
@@ -50,16 +61,19 @@ impl<W: Write> Writer<W> {
5061 fn write_bool ( & mut self , v : bool ) -> Result < ( ) , DecodeError > {
5162 Ok ( self . writer . write_all ( & [ if v { 1 } else { 0 } ] ) ?)
5263 }
53- pub fn write_all ( & mut self , v : & [ u8 ] ) -> Result < ( ) , DecodeError > {
64+ pub ( crate ) fn write_all ( & mut self , v : & [ u8 ] ) -> Result < ( ) , DecodeError > {
5465 Ok ( self . writer . write_all ( v) ?)
5566 }
5667}
5768
5869impl < R : Read > Reader < R > {
70+ /// Creates a new Reader from an std::io::Read-impl'ing object
5971 pub fn new ( reader : R ) -> Reader < R > {
6072 return Reader { reader }
6173 }
74+ /// Consumes this object and returns the original reader
6275 pub fn into_inner ( self ) -> R { self . reader }
76+ /// Gets a reference to the original reader
6377 pub fn get_ref ( & self ) -> & R { & self . reader }
6478
6579 fn read_u64 ( & mut self ) -> Result < u64 , DecodeError > {
@@ -93,10 +107,10 @@ impl<R: Read> Reader<R> {
93107 }
94108 Ok ( buf[ 0 ] == 1 )
95109 }
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 > {
97111 Ok ( self . reader . read_exact ( buf) ?)
98112 }
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 > {
100114 Ok ( self . reader . read_to_end ( buf) ?)
101115 }
102116}
@@ -306,22 +320,3 @@ impl<R: Read> Readable<R> for Signature {
306320 }
307321 }
308322}
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