From e8a66ef0ea857101ee86578d2037d56a343be57b Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 16 Sep 2018 14:02:58 -0400 Subject: [PATCH] Make Writeable::write typed instead of Writeable This lets us add some untyped default functions to the trait --- src/ln/msgs.rs | 40 ++++++++++++++--------------- src/util/ser.rs | 57 +++++++++++++++++++++--------------------- src/util/ser_macros.rs | 8 +++--- 3 files changed, 52 insertions(+), 53 deletions(-) diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index 5100ed79c..b2f48997c 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -742,8 +742,8 @@ impl_writeable!(AnnouncementSignatures, 32+8+64*2, { bitcoin_signature }); -impl Writeable for ChannelReestablish { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for ChannelReestablish { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(if self.data_loss_protect.is_some() { 32+2*8+33+32 } else { 32+2*8 }); self.channel_id.write(w)?; self.next_local_commitment_number.write(w)?; @@ -905,8 +905,8 @@ impl_writeable_len_match!(OnionErrorPacket, { data }); -impl Writeable for OnionPacket { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for OnionPacket { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(1 + 33 + 20*65 + 32); self.version.write(w)?; match self.public_key { @@ -943,8 +943,8 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, { onion_routing_packet }); -impl Writeable for OnionRealm0HopData { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for OnionRealm0HopData { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(32); self.short_channel_id.write(w)?; self.amt_to_forward.write(w)?; @@ -968,8 +968,8 @@ impl Readable for OnionRealm0HopData { } } -impl Writeable for OnionHopData { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for OnionHopData { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(65); self.realm.write(w)?; self.data.write(w)?; @@ -994,8 +994,8 @@ impl Readable for OnionHopData { } } -impl Writeable for Ping { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for Ping { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(self.byteslen as usize + 4); self.ponglen.write(w)?; vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write @@ -1016,8 +1016,8 @@ impl Readable for Ping { } } -impl Writeable for Pong { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for Pong { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(self.byteslen as usize + 2); vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write Ok(()) @@ -1036,8 +1036,8 @@ impl Readable for Pong { } } -impl Writeable for UnsignedChannelAnnouncement { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for UnsignedChannelAnnouncement { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len()); self.features.write(w)?; self.chain_hash.write(w)?; @@ -1087,8 +1087,8 @@ impl_writeable_len_match!(ChannelAnnouncement, { contents }); -impl Writeable for UnsignedChannelUpdate { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for UnsignedChannelUpdate { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(64 + self.excess_data.len()); self.chain_hash.write(w)?; self.short_channel_id.write(w)?; @@ -1131,8 +1131,8 @@ impl_writeable_len_match!(ChannelUpdate, { contents }); -impl Writeable for ErrorMessage { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for ErrorMessage { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(32 + 2 + self.data.len()); self.channel_id.write(w)?; (self.data.len() as u16).write(w)?; @@ -1159,8 +1159,8 @@ impl Readable for ErrorMessage { } } -impl Writeable for UnsignedNodeAnnouncement { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for UnsignedNodeAnnouncement { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(64 + 76 + self.features.flags.len() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len()); self.features.write(w)?; self.timestamp.write(w)?; diff --git a/src/util/ser.rs b/src/util/ser.rs index 18aa7eee9..2dea21ef1 100644 --- a/src/util/ser.rs +++ b/src/util/ser.rs @@ -36,9 +36,9 @@ impl Writer for W { } /// A trait that various rust-lightning types implement allowing them to be written out to a Writer -pub trait Writeable { +pub trait Writeable { /// Writes self out to the given Writer - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>; + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>; } /// A trait that various rust-lightning types implement allowing them to be read in from a Read @@ -52,9 +52,9 @@ pub trait Readable macro_rules! impl_writeable_primitive { ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => { - impl Writeable for $val_type { + impl Writeable for $val_type { #[inline] - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { writer.write_all(&$meth_write(*self)) } } @@ -73,9 +73,9 @@ impl_writeable_primitive!(u64, be64_to_array, 8, slice_to_be64); impl_writeable_primitive!(u32, be32_to_array, 4, slice_to_be32); impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16); -impl Writeable for u8 { +impl Writeable for u8 { #[inline] - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { writer.write_all(&[*self]) } } @@ -88,9 +88,9 @@ impl Readable for u8 { } } -impl Writeable for bool { +impl Writeable for bool { #[inline] - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { writer.write_all(&[if *self {1} else {0}]) } } @@ -109,10 +109,10 @@ impl Readable for bool { // u8 arrays macro_rules! impl_array { ( $size:expr ) => ( - impl Writeable for [u8; $size] + impl Writeable for [u8; $size] { #[inline] - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.write_all(self) } } @@ -136,13 +136,12 @@ impl_array!(64); // for Signature impl_array!(1300); // for OnionPacket.hop_data // HashMap -impl Writeable for HashMap - where W: Writer, - K: Writeable + Eq + Hash, - V: Writeable +impl Writeable for HashMap + where K: Writeable + Eq + Hash, + V: Writeable { #[inline] - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { (self.len() as u16).write(w)?; for (key, value) in self.iter() { key.write(w)?; @@ -169,9 +168,9 @@ impl Readable for HashMap } // Vectors -impl Writeable for Vec { +impl Writeable for Vec { #[inline] - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { (self.len() as u16).write(w)?; w.write_all(&self) } @@ -187,9 +186,9 @@ impl Readable for Vec { Ok(ret) } } -impl Writeable for Vec { +impl Writeable for Vec { #[inline] - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { (self.len() as u16).write(w)?; for e in self.iter() { e.write(w)?; @@ -214,8 +213,8 @@ impl Readable for Vec { } } -impl Writeable for Script { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { +impl Writeable for Script { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { (self.len() as u16).write(w)?; w.write_all(self.as_bytes()) } @@ -230,8 +229,8 @@ impl Readable for Script { } } -impl Writeable for Option