Skip to content

Commit b7c5a29

Browse files
committed
Expose (de)serialziers as we'll need them and I don't like warnings
1 parent 5737b32 commit b7c5a29

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

src/util/mod.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
pub mod events;
22
pub mod errors;
3+
pub mod ser;
34

45
pub(crate) mod byte_utils;
56
pub(crate) mod chacha20poly1305rfc;
67
pub(crate) mod internal_traits;
78
pub(crate) mod rng;
89
pub(crate) mod transaction_utils;
910

11+
#[macro_use]
12+
pub(crate) mod ser_macros;
13+
#[macro_use]
14+
pub(crate) mod macro_logger;
15+
16+
// Logger has to come after macro_logger for tests to build:
17+
pub mod logger;
18+
1019
#[cfg(feature = "fuzztarget")]
1120
pub mod sha2;
1221
#[cfg(not(feature = "fuzztarget"))]
@@ -17,15 +26,3 @@ pub use self::rng::reset_rng_state;
1726

1827
#[cfg(test)]
1928
pub(crate) mod test_utils;
20-
21-
#[macro_use]
22-
pub(crate) mod macro_logger;
23-
24-
#[cfg(feature = "fuzztarget")]
25-
#[macro_use]
26-
pub mod ser;
27-
#[cfg(not(feature = "fuzztarget"))]
28-
#[macro_use]
29-
pub(crate) mod ser;
30-
31-
pub mod logger;

src/util/ser.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,36 @@ use util::byte_utils::{be64_to_array, be32_to_array, be16_to_array, slice_to_be1
1515

1616
const 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
2126
pub 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
2532
pub 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

3240
impl<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

5869
impl<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-
}

src/util/ser_macros.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
macro_rules! impl_writeable {
2+
($st:ident, {$($field:ident),*}) => {
3+
impl<W: ::std::io::Write> Writeable<W> for $st {
4+
fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
5+
$( self.$field.write(w)?; )*
6+
Ok(())
7+
}
8+
}
9+
10+
impl<R: ::std::io::Read> Readable<R> for $st {
11+
fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
12+
Ok(Self {
13+
$($field: Readable::read(r)?),*
14+
})
15+
}
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)