X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser_macros.rs;h=5d5171adbb4401c1dd1fb5faf3b1f8afab919db2;hb=57feb2630779410b5977ccb3c12dd482a20440fc;hp=b93115dcc95933bbc5a617bbf1dc5083f25cdd10;hpb=96a738aa5379d5993a7881203c5a643cff3e7933;p=rust-lightning diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index b93115dc..5d5171ad 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -93,7 +93,7 @@ macro_rules! check_tlv_order { #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type; if invalid_order { - Err(DecodeError::InvalidValue)? + return Err(DecodeError::InvalidValue); } }}; ($last_seen_type: expr, $typ: expr, $type: expr, option) => {{ @@ -109,7 +109,7 @@ macro_rules! check_missing_tlv { #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type; if missing_req_type { - Err(DecodeError::InvalidValue)? + return Err(DecodeError::InvalidValue); } }}; ($last_seen_type: expr, $type: expr, vec_type) => {{ @@ -149,12 +149,12 @@ macro_rules! decode_tlv_stream { match ser::Readable::read(&mut tracking_reader) { Err(DecodeError::ShortRead) => { if !tracking_reader.have_read { - break 'tlv_read + break 'tlv_read; } else { - Err(DecodeError::ShortRead)? + return Err(DecodeError::ShortRead); } }, - Err(e) => Err(e)?, + Err(e) => return Err(e), Ok(t) => t, } }; @@ -162,7 +162,7 @@ macro_rules! decode_tlv_stream { // Types must be unique and monotonically increasing: match last_seen_type { Some(t) if typ.0 <= t => { - Err(DecodeError::InvalidValue)? + return Err(DecodeError::InvalidValue); }, _ => {}, } @@ -173,18 +173,18 @@ macro_rules! decode_tlv_stream { last_seen_type = Some(typ.0); // Finally, read the length and value itself: - let length: ser::BigSize = Readable::read($stream)?; + let length: ser::BigSize = ser::Readable::read($stream)?; let mut s = ser::FixedLengthReader::new($stream, length.0); match typ.0 { $($type => { decode_tlv!(s, $field, $fieldty); if s.bytes_remain() { s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes - Err(DecodeError::InvalidValue)? + return Err(DecodeError::InvalidValue); } },)* x if x % 2 == 0 => { - Err(DecodeError::UnknownRequiredFeature)? + return Err(DecodeError::UnknownRequiredFeature); }, _ => {}, } @@ -200,7 +200,7 @@ macro_rules! decode_tlv_stream { macro_rules! impl_writeable { ($st:ident, $len: expr, {$($field:ident),*}) => { impl ::util::ser::Writeable for $st { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { if $len != 0 { w.size_hint($len); } @@ -235,7 +235,7 @@ macro_rules! impl_writeable { } impl ::util::ser::Readable for $st { - fn read(r: &mut R) -> Result { + fn read(r: &mut R) -> Result { Ok(Self { $($field: ::util::ser::Readable::read(r)?),* }) @@ -246,7 +246,7 @@ macro_rules! impl_writeable { macro_rules! impl_writeable_len_match { ($struct: ident, $cmp: tt, ($calc_len: expr), {$({$match: pat, $length: expr}),*}, {$($field:ident),*}) => { impl Writeable for $struct { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { let len = match *self { $($match => $length,)* }; @@ -282,7 +282,7 @@ macro_rules! impl_writeable_len_match { } impl ::util::ser::Readable for $struct { - fn read(r: &mut R) -> Result { + fn read(r: &mut R) -> Result { Ok(Self { $($field: Readable::read(r)?),* }) @@ -387,7 +387,7 @@ macro_rules! init_tlv_field_var { macro_rules! impl_writeable_tlv_based { ($st: ident, {$(($type: expr, $field: ident, $fieldty: ident)),* $(,)*}) => { impl ::util::ser::Writeable for $st { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { write_tlv_fields!(writer, { $(($type, self.$field, $fieldty)),* }); @@ -412,7 +412,7 @@ macro_rules! impl_writeable_tlv_based { } impl ::util::ser::Readable for $st { - fn read(reader: &mut R) -> Result { + fn read(reader: &mut R) -> Result { $( init_tlv_field_var!($field, $fieldty); )* @@ -445,7 +445,7 @@ macro_rules! impl_writeable_tlv_based_enum { ),* $(,)*; $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*) => { impl ::util::ser::Writeable for $st { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { match self { $($st::$variant_name { $(ref $field),* } => { let id: u8 = $variant_id; @@ -465,7 +465,7 @@ macro_rules! impl_writeable_tlv_based_enum { } impl ::util::ser::Readable for $st { - fn read(reader: &mut R) -> Result { + fn read(reader: &mut R) -> Result { let id: u8 = ::util::ser::Readable::read(reader)?; match id { $($variant_id => { @@ -490,7 +490,7 @@ macro_rules! impl_writeable_tlv_based_enum { Ok($st::$tuple_variant_name(Readable::read(reader)?)) }),* _ => { - Err(DecodeError::UnknownRequiredFeature)? + Err(DecodeError::UnknownRequiredFeature) }, } } @@ -500,10 +500,10 @@ macro_rules! impl_writeable_tlv_based_enum { #[cfg(test)] mod tests { + use io::{self, Cursor}; use prelude::*; - use std::io::Cursor; use ln::msgs::DecodeError; - use util::ser::{Readable, Writeable, HighZeroBytesDroppedVarInt, VecWriter}; + use util::ser::{Writeable, HighZeroBytesDroppedVarInt, VecWriter}; use bitcoin::secp256k1::PublicKey; // The BOLT TLV test cases don't include any tests which use our "required-value" logic since @@ -685,7 +685,7 @@ mod tests { do_test!(concat!("fd00fe", "02", "0226"), None, None, None, Some(550)); } - fn do_simple_test_tlv_write() -> Result<(), ::std::io::Error> { + fn do_simple_test_tlv_write() -> Result<(), io::Error> { let mut stream = VecWriter(Vec::new()); stream.0.clear();