From 51ba6ad2e95d4ddb172b4d4d4b067f94fcdb0aac Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 14 Sep 2018 16:43:47 -0400 Subject: [PATCH] Expose (de)serialziers as we'll need them and I don't like warnings --- src/util/mod.rs | 21 +++++++++----------- src/util/ser.rs | 45 +++++++++++++++++++----------------------- src/util/ser_macros.rs | 18 +++++++++++++++++ 3 files changed, 47 insertions(+), 37 deletions(-) create mode 100644 src/util/ser_macros.rs diff --git a/src/util/mod.rs b/src/util/mod.rs index 308825855..2244f9b2f 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,5 +1,6 @@ pub mod events; pub mod errors; +pub mod ser; pub(crate) mod byte_utils; pub(crate) mod chacha20poly1305rfc; @@ -7,6 +8,14 @@ pub(crate) mod internal_traits; pub(crate) mod rng; pub(crate) mod transaction_utils; +#[macro_use] +pub(crate) mod ser_macros; +#[macro_use] +pub(crate) mod macro_logger; + +// Logger has to come after macro_logger for tests to build: +pub mod logger; + #[cfg(feature = "fuzztarget")] pub mod sha2; #[cfg(not(feature = "fuzztarget"))] @@ -17,15 +26,3 @@ pub use self::rng::reset_rng_state; #[cfg(test)] pub(crate) mod test_utils; - -#[macro_use] -pub(crate) mod macro_logger; - -#[cfg(feature = "fuzztarget")] -#[macro_use] -pub mod ser; -#[cfg(not(feature = "fuzztarget"))] -#[macro_use] -pub(crate) mod ser; - -pub mod logger; diff --git a/src/util/ser.rs b/src/util/ser.rs index 299515d90..cd5e6a0db 100644 --- a/src/util/ser.rs +++ b/src/util/ser.rs @@ -15,25 +15,36 @@ use util::byte_utils::{be64_to_array, be32_to_array, be16_to_array, slice_to_be1 const MAX_BUF_SIZE: usize = 64 * 1024; -pub struct Writer { writer: W } -pub struct Reader { reader: R } - +/// A struct that holds an std::io::Write-impl'ing object and implements various +/// rust-lightning-specific write functions. +pub struct Writer { writer: W } +/// A struct that holds an std::io::Read-impl'ing object and implements various +/// rust-lightning-specific read functions. +pub struct Reader { reader: R } + +/// A trait that various rust-lightning types implement allowing them to be written out to a Writer pub trait Writeable { + /// Writes self out to the given Writer fn write(&self, writer: &mut Writer) -> Result<(), DecodeError>; } +/// A trait that various rust-lightning types implement allowing them to be read in from a Reader pub trait Readable where Self: Sized, R: Read { + /// Reads a Self in from the given Reader fn read(reader: &mut Reader) -> Result; } impl Writer { + /// Creates a new Writer from an std::io::Write-impl'ing object pub fn new(writer: W) -> Writer { return Writer { writer } } + /// Consumes this object and returns the original writer pub fn into_inner(self) -> W { self.writer } + /// Gets a reference to the original writer pub fn get_ref(&self) -> &W { &self.writer } fn write_u64(&mut self, v: u64) -> Result<(), DecodeError> { Ok(self.writer.write_all(&be64_to_array(v))?) @@ -50,16 +61,19 @@ impl Writer { fn write_bool(&mut self, v: bool) -> Result<(), DecodeError> { Ok(self.writer.write_all(&[if v {1} else {0}])?) } - pub fn write_all(&mut self, v: &[u8]) -> Result<(), DecodeError> { + pub(crate) fn write_all(&mut self, v: &[u8]) -> Result<(), DecodeError> { Ok(self.writer.write_all(v)?) } } impl Reader { + /// Creates a new Reader from an std::io::Read-impl'ing object pub fn new(reader: R) -> Reader { return Reader { reader } } + /// Consumes this object and returns the original reader pub fn into_inner(self) -> R { self.reader } + /// Gets a reference to the original reader pub fn get_ref(&self) -> &R { &self.reader } fn read_u64(&mut self) -> Result { @@ -93,10 +107,10 @@ impl Reader { } Ok(buf[0] == 1) } - pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), DecodeError> { + pub(crate) fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), DecodeError> { Ok(self.reader.read_exact(buf)?) } - pub fn read_to_end(&mut self, buf: &mut Vec) -> Result { + pub(crate) fn read_to_end(&mut self, buf: &mut Vec) -> Result { Ok(self.reader.read_to_end(buf)?) } } @@ -306,22 +320,3 @@ impl Readable for Signature { } } } - -macro_rules! impl_writeable { - ($st:ident, {$($field:ident),*}) => { - impl Writeable for $st { - fn write(&self, w: &mut Writer) -> Result<(), DecodeError> { - $( self.$field.write(w)?; )* - Ok(()) - } - } - - impl Readable for $st { - fn read(r: &mut Reader) -> Result { - Ok(Self { - $($field: Readable::read(r)?),* - }) - } - } - } -} diff --git a/src/util/ser_macros.rs b/src/util/ser_macros.rs new file mode 100644 index 000000000..619d8e2f7 --- /dev/null +++ b/src/util/ser_macros.rs @@ -0,0 +1,18 @@ +macro_rules! impl_writeable { + ($st:ident, {$($field:ident),*}) => { + impl Writeable for $st { + fn write(&self, w: &mut Writer) -> Result<(), DecodeError> { + $( self.$field.write(w)?; )* + Ok(()) + } + } + + impl Readable for $st { + fn read(r: &mut Reader) -> Result { + Ok(Self { + $($field: Readable::read(r)?),* + }) + } + } + } +} -- 2.39.5