Update rust-bitcoin
[rust-lightning] / lightning / src / util / ser.rs
index a087467f7a438304ee3db7f52c956ce1a9ef90c3..b718e228c93e8233ce799e7fa10c90b26cd89b9d 100644 (file)
@@ -1,7 +1,15 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
 //! A very simple serialization framework which is used to serialize/deserialize messages as well
 //! as ChannelsManagers and ChannelMonitors.
 
-use std::result::Result;
 use std::io::{Read, Write};
 use std::collections::HashMap;
 use std::hash::Hash;
@@ -23,12 +31,15 @@ use util::byte_utils;
 
 use util::byte_utils::{be64_to_array, be48_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be48, slice_to_be64};
 
-const MAX_BUF_SIZE: usize = 64 * 1024;
+/// serialization buffer size
+pub const MAX_BUF_SIZE: usize = 64 * 1024;
 
 /// A trait that is similar to std::io::Write but has one extra function which can be used to size
 /// buffers being written into.
 /// An impl is provided for any type that also impls std::io::Write which simply ignores size
 /// hints.
+///
+/// (C-not exported) as we only export serialization to/from byte arrays instead
 pub trait Writer {
        /// Writes the given buf out. See std::io::Write::write_all for more
        fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>;
@@ -151,6 +162,8 @@ impl<R: Read> Read for ReadTrackingReader<R> {
 }
 
 /// A trait that various rust-lightning types implement allowing them to be written out to a Writer
+///
+/// (C-not exported) as we only export serialization to/from byte arrays instead
 pub trait Writeable {
        /// Writes self out to the given Writer
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
@@ -178,6 +191,8 @@ impl<'a, T: Writeable> Writeable for &'a T {
 }
 
 /// A trait that various rust-lightning types implement allowing them to be read in from a Read
+///
+/// (C-not exported) as we only export serialization to/from byte arrays instead
 pub trait Readable
        where Self: Sized
 {
@@ -187,6 +202,8 @@ pub trait Readable
 
 /// A trait that various higher-level rust-lightning types implement allowing them to be read in
 /// from a Read given some additional set of arguments which is required to deserialize.
+///
+/// (C-not exported) as we only export serialization to/from byte arrays instead
 pub trait ReadableArgs<P>
        where Self: Sized
 {
@@ -195,6 +212,8 @@ pub trait ReadableArgs<P>
 }
 
 /// A trait that various rust-lightning types implement allowing them to (maybe) be read in from a Read
+///
+/// (C-not exported) as we only export serialization to/from byte arrays instead
 pub trait MaybeReadable
        where Self: Sized
 {
@@ -688,8 +707,7 @@ macro_rules! impl_consensus_ser {
                        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
                                match self.consensus_encode(WriterWriteAdaptor(writer)) {
                                        Ok(_) => Ok(()),
-                                       Err(consensus::encode::Error::Io(e)) => Err(e),
-                                       Err(_) => panic!("We shouldn't get a consensus::encode::Error unless our Write generated an std::io::Error"),
+                                       Err(e) => Err(e),
                                }
                        }
                }
@@ -699,7 +717,7 @@ macro_rules! impl_consensus_ser {
                                match consensus::encode::Decodable::consensus_decode(r) {
                                        Ok(t) => Ok(t),
                                        Err(consensus::encode::Error::Io(ref e)) if e.kind() == ::std::io::ErrorKind::UnexpectedEof => Err(DecodeError::ShortRead),
-                                       Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e)),
+                                       Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e.kind())),
                                        Err(_) => Err(DecodeError::InvalidValue),
                                }
                        }