Make Writeable::write typed instead of Writeable

This lets us add some untyped default functions to the trait
This commit is contained in:
Matt Corallo 2018-09-16 14:02:58 -04:00
parent 8322c756cb
commit e8a66ef0ea
3 changed files with 52 additions and 53 deletions

View file

@ -742,8 +742,8 @@ impl_writeable!(AnnouncementSignatures, 32+8+64*2, {
bitcoin_signature bitcoin_signature
}); });
impl<W: Writer> Writeable<W> for ChannelReestablish { impl Writeable for ChannelReestablish {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&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 }); 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.channel_id.write(w)?;
self.next_local_commitment_number.write(w)?; self.next_local_commitment_number.write(w)?;
@ -905,8 +905,8 @@ impl_writeable_len_match!(OnionErrorPacket, {
data data
}); });
impl<W: Writer> Writeable<W> for OnionPacket { impl Writeable for OnionPacket {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(1 + 33 + 20*65 + 32); w.size_hint(1 + 33 + 20*65 + 32);
self.version.write(w)?; self.version.write(w)?;
match self.public_key { match self.public_key {
@ -943,8 +943,8 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
onion_routing_packet onion_routing_packet
}); });
impl<W: Writer> Writeable<W> for OnionRealm0HopData { impl Writeable for OnionRealm0HopData {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(32); w.size_hint(32);
self.short_channel_id.write(w)?; self.short_channel_id.write(w)?;
self.amt_to_forward.write(w)?; self.amt_to_forward.write(w)?;
@ -968,8 +968,8 @@ impl<R: Read> Readable<R> for OnionRealm0HopData {
} }
} }
impl<W: Writer> Writeable<W> for OnionHopData { impl Writeable for OnionHopData {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(65); w.size_hint(65);
self.realm.write(w)?; self.realm.write(w)?;
self.data.write(w)?; self.data.write(w)?;
@ -994,8 +994,8 @@ impl<R: Read> Readable<R> for OnionHopData {
} }
} }
impl<W: Writer> Writeable<W> for Ping { impl Writeable for Ping {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(self.byteslen as usize + 4); w.size_hint(self.byteslen as usize + 4);
self.ponglen.write(w)?; self.ponglen.write(w)?;
vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
@ -1016,8 +1016,8 @@ impl<R: Read> Readable<R> for Ping {
} }
} }
impl<W: Writer> Writeable<W> for Pong { impl Writeable for Pong {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(self.byteslen as usize + 2); w.size_hint(self.byteslen as usize + 2);
vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
Ok(()) Ok(())
@ -1036,8 +1036,8 @@ impl<R: Read> Readable<R> for Pong {
} }
} }
impl<W: Writer> Writeable<W> for UnsignedChannelAnnouncement { impl Writeable for UnsignedChannelAnnouncement {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len()); w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len());
self.features.write(w)?; self.features.write(w)?;
self.chain_hash.write(w)?; self.chain_hash.write(w)?;
@ -1087,8 +1087,8 @@ impl_writeable_len_match!(ChannelAnnouncement, {
contents contents
}); });
impl<W: Writer> Writeable<W> for UnsignedChannelUpdate { impl Writeable for UnsignedChannelUpdate {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(64 + self.excess_data.len()); w.size_hint(64 + self.excess_data.len());
self.chain_hash.write(w)?; self.chain_hash.write(w)?;
self.short_channel_id.write(w)?; self.short_channel_id.write(w)?;
@ -1131,8 +1131,8 @@ impl_writeable_len_match!(ChannelUpdate, {
contents contents
}); });
impl<W: Writer> Writeable<W> for ErrorMessage { impl Writeable for ErrorMessage {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(32 + 2 + self.data.len()); w.size_hint(32 + 2 + self.data.len());
self.channel_id.write(w)?; self.channel_id.write(w)?;
(self.data.len() as u16).write(w)?; (self.data.len() as u16).write(w)?;
@ -1159,8 +1159,8 @@ impl<R: Read> Readable<R> for ErrorMessage {
} }
} }
impl<W: Writer> Writeable<W> for UnsignedNodeAnnouncement { impl Writeable for UnsignedNodeAnnouncement {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&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()); 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.features.write(w)?;
self.timestamp.write(w)?; self.timestamp.write(w)?;

View file

@ -36,9 +36,9 @@ impl<W: ::std::io::Write> Writer for W {
} }
/// A trait that various rust-lightning types implement allowing them to be written out to a Writer /// A trait that various rust-lightning types implement allowing them to be written out to a Writer
pub trait Writeable<W: Writer> { pub trait Writeable {
/// Writes self out to the given Writer /// Writes self out to the given Writer
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>; fn write<W: Writer>(&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 /// A trait that various rust-lightning types implement allowing them to be read in from a Read
@ -52,9 +52,9 @@ pub trait Readable<R>
macro_rules! impl_writeable_primitive { macro_rules! impl_writeable_primitive {
($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => { ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
impl<W: Writer> Writeable<W> for $val_type { impl Writeable for $val_type {
#[inline] #[inline]
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
writer.write_all(&$meth_write(*self)) 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!(u32, be32_to_array, 4, slice_to_be32);
impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16); impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16);
impl<W: Writer> Writeable<W> for u8 { impl Writeable for u8 {
#[inline] #[inline]
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
writer.write_all(&[*self]) writer.write_all(&[*self])
} }
} }
@ -88,9 +88,9 @@ impl<R: Read> Readable<R> for u8 {
} }
} }
impl<W: Writer> Writeable<W> for bool { impl Writeable for bool {
#[inline] #[inline]
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
writer.write_all(&[if *self {1} else {0}]) writer.write_all(&[if *self {1} else {0}])
} }
} }
@ -109,10 +109,10 @@ impl<R: Read> Readable<R> for bool {
// u8 arrays // u8 arrays
macro_rules! impl_array { macro_rules! impl_array {
( $size:expr ) => ( ( $size:expr ) => (
impl<W: Writer> Writeable<W> for [u8; $size] impl Writeable for [u8; $size]
{ {
#[inline] #[inline]
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.write_all(self) w.write_all(self)
} }
} }
@ -136,13 +136,12 @@ impl_array!(64); // for Signature
impl_array!(1300); // for OnionPacket.hop_data impl_array!(1300); // for OnionPacket.hop_data
// HashMap // HashMap
impl<W, K, V> Writeable<W> for HashMap<K, V> impl<K, V> Writeable for HashMap<K, V>
where W: Writer, where K: Writeable + Eq + Hash,
K: Writeable<W> + Eq + Hash, V: Writeable
V: Writeable<W>
{ {
#[inline] #[inline]
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
(self.len() as u16).write(w)?; (self.len() as u16).write(w)?;
for (key, value) in self.iter() { for (key, value) in self.iter() {
key.write(w)?; key.write(w)?;
@ -169,9 +168,9 @@ impl<R, K, V> Readable<R> for HashMap<K, V>
} }
// Vectors // Vectors
impl<W: Writer> Writeable<W> for Vec<u8> { impl Writeable for Vec<u8> {
#[inline] #[inline]
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
(self.len() as u16).write(w)?; (self.len() as u16).write(w)?;
w.write_all(&self) w.write_all(&self)
} }
@ -187,9 +186,9 @@ impl<R: Read> Readable<R> for Vec<u8> {
Ok(ret) Ok(ret)
} }
} }
impl<W: Writer> Writeable<W> for Vec<Signature> { impl Writeable for Vec<Signature> {
#[inline] #[inline]
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
(self.len() as u16).write(w)?; (self.len() as u16).write(w)?;
for e in self.iter() { for e in self.iter() {
e.write(w)?; e.write(w)?;
@ -214,8 +213,8 @@ impl<R: Read> Readable<R> for Vec<Signature> {
} }
} }
impl<W: Writer> Writeable<W> for Script { impl Writeable for Script {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
(self.len() as u16).write(w)?; (self.len() as u16).write(w)?;
w.write_all(self.as_bytes()) w.write_all(self.as_bytes())
} }
@ -230,8 +229,8 @@ impl<R: Read> Readable<R> for Script {
} }
} }
impl<W: Writer> Writeable<W> for Option<Script> { impl Writeable for Option<Script> {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
if let &Some(ref script) = self { if let &Some(ref script) = self {
script.write(w)?; script.write(w)?;
} }
@ -253,8 +252,8 @@ impl<R: Read> Readable<R> for Option<Script> {
} }
} }
impl<W: Writer> Writeable<W> for PublicKey { impl Writeable for PublicKey {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
self.serialize().write(w) self.serialize().write(w)
} }
} }
@ -269,8 +268,8 @@ impl<R: Read> Readable<R> for PublicKey {
} }
} }
impl<W: Writer> Writeable<W> for Sha256dHash { impl Writeable for Sha256dHash {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
self.as_bytes().write(w) self.as_bytes().write(w)
} }
} }
@ -282,8 +281,8 @@ impl<R: Read> Readable<R> for Sha256dHash {
} }
} }
impl<W: Writer> Writeable<W> for Signature { impl Writeable for Signature {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
self.serialize_compact(&Secp256k1::without_caps()).write(w) self.serialize_compact(&Secp256k1::without_caps()).write(w)
} }
} }

View file

@ -1,7 +1,7 @@
macro_rules! impl_writeable { macro_rules! impl_writeable {
($st:ident, $len: expr, {$($field:ident),*}) => { ($st:ident, $len: expr, {$($field:ident),*}) => {
impl<W: Writer> Writeable<W> for $st { impl Writeable for $st {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint($len); w.size_hint($len);
$( self.$field.write(w)?; )* $( self.$field.write(w)?; )*
Ok(()) Ok(())
@ -19,8 +19,8 @@ macro_rules! impl_writeable {
} }
macro_rules! impl_writeable_len_match { macro_rules! impl_writeable_len_match {
($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => { ($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
impl<W: Writer> Writeable<W> for $st { impl Writeable for $st {
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
w.size_hint(match *self { w.size_hint(match *self {
$($m => $l,)* $($m => $l,)*
}); });