Skip to content

Commit 6f27210

Browse files
committed
Make Writeable::write typed instead of Writeable
This lets us add some untyped default functions to the trait
1 parent a399d4f commit 6f27210

File tree

3 files changed

+52
-53
lines changed

3 files changed

+52
-53
lines changed

src/ln/msgs.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,8 @@ impl_writeable!(AnnouncementSignatures, 32+8+64*2, {
742742
bitcoin_signature
743743
});
744744

745-
impl<W: Writer> Writeable<W> for ChannelReestablish {
746-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
745+
impl Writeable for ChannelReestablish {
746+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
747747
w.size_hint(if self.data_loss_protect.is_some() { 32+2*8+33+32 } else { 32+2*8 });
748748
self.channel_id.write(w)?;
749749
self.next_local_commitment_number.write(w)?;
@@ -905,8 +905,8 @@ impl_writeable_len_match!(OnionErrorPacket, {
905905
data
906906
});
907907

908-
impl<W: Writer> Writeable<W> for OnionPacket {
909-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
908+
impl Writeable for OnionPacket {
909+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
910910
w.size_hint(1 + 33 + 20*65 + 32);
911911
self.version.write(w)?;
912912
match self.public_key {
@@ -943,8 +943,8 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
943943
onion_routing_packet
944944
});
945945

946-
impl<W: Writer> Writeable<W> for OnionRealm0HopData {
947-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
946+
impl Writeable for OnionRealm0HopData {
947+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
948948
w.size_hint(32);
949949
self.short_channel_id.write(w)?;
950950
self.amt_to_forward.write(w)?;
@@ -968,8 +968,8 @@ impl<R: Read> Readable<R> for OnionRealm0HopData {
968968
}
969969
}
970970

971-
impl<W: Writer> Writeable<W> for OnionHopData {
972-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
971+
impl Writeable for OnionHopData {
972+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
973973
w.size_hint(65);
974974
self.realm.write(w)?;
975975
self.data.write(w)?;
@@ -994,8 +994,8 @@ impl<R: Read> Readable<R> for OnionHopData {
994994
}
995995
}
996996

997-
impl<W: Writer> Writeable<W> for Ping {
998-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
997+
impl Writeable for Ping {
998+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
999999
w.size_hint(self.byteslen as usize + 4);
10001000
self.ponglen.write(w)?;
10011001
vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
@@ -1016,8 +1016,8 @@ impl<R: Read> Readable<R> for Ping {
10161016
}
10171017
}
10181018

1019-
impl<W: Writer> Writeable<W> for Pong {
1020-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1019+
impl Writeable for Pong {
1020+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
10211021
w.size_hint(self.byteslen as usize + 2);
10221022
vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
10231023
Ok(())
@@ -1036,8 +1036,8 @@ impl<R: Read> Readable<R> for Pong {
10361036
}
10371037
}
10381038

1039-
impl<W: Writer> Writeable<W> for UnsignedChannelAnnouncement {
1040-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1039+
impl Writeable for UnsignedChannelAnnouncement {
1040+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
10411041
w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len());
10421042
self.features.write(w)?;
10431043
self.chain_hash.write(w)?;
@@ -1087,8 +1087,8 @@ impl_writeable_len_match!(ChannelAnnouncement, {
10871087
contents
10881088
});
10891089

1090-
impl<W: Writer> Writeable<W> for UnsignedChannelUpdate {
1091-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1090+
impl Writeable for UnsignedChannelUpdate {
1091+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
10921092
w.size_hint(64 + self.excess_data.len());
10931093
self.chain_hash.write(w)?;
10941094
self.short_channel_id.write(w)?;
@@ -1131,8 +1131,8 @@ impl_writeable_len_match!(ChannelUpdate, {
11311131
contents
11321132
});
11331133

1134-
impl<W: Writer> Writeable<W> for ErrorMessage {
1135-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1134+
impl Writeable for ErrorMessage {
1135+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
11361136
w.size_hint(32 + 2 + self.data.len());
11371137
self.channel_id.write(w)?;
11381138
(self.data.len() as u16).write(w)?;
@@ -1159,8 +1159,8 @@ impl<R: Read> Readable<R> for ErrorMessage {
11591159
}
11601160
}
11611161

1162-
impl<W: Writer> Writeable<W> for UnsignedNodeAnnouncement {
1163-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1162+
impl Writeable for UnsignedNodeAnnouncement {
1163+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
11641164
w.size_hint(64 + 76 + self.features.flags.len() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
11651165
self.features.write(w)?;
11661166
self.timestamp.write(w)?;

src/util/ser.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ impl<W: ::std::io::Write> Writer for W {
3636
}
3737

3838
/// A trait that various rust-lightning types implement allowing them to be written out to a Writer
39-
pub trait Writeable<W: Writer> {
39+
pub trait Writeable {
4040
/// Writes self out to the given Writer
41-
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
41+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
4242
}
4343

4444
/// 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>
5252

5353
macro_rules! impl_writeable_primitive {
5454
($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
55-
impl<W: Writer> Writeable<W> for $val_type {
55+
impl Writeable for $val_type {
5656
#[inline]
57-
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
57+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
5858
writer.write_all(&$meth_write(*self))
5959
}
6060
}
@@ -73,9 +73,9 @@ impl_writeable_primitive!(u64, be64_to_array, 8, slice_to_be64);
7373
impl_writeable_primitive!(u32, be32_to_array, 4, slice_to_be32);
7474
impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16);
7575

76-
impl<W: Writer> Writeable<W> for u8 {
76+
impl Writeable for u8 {
7777
#[inline]
78-
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
78+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
7979
writer.write_all(&[*self])
8080
}
8181
}
@@ -88,9 +88,9 @@ impl<R: Read> Readable<R> for u8 {
8888
}
8989
}
9090

91-
impl<W: Writer> Writeable<W> for bool {
91+
impl Writeable for bool {
9292
#[inline]
93-
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
93+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
9494
writer.write_all(&[if *self {1} else {0}])
9595
}
9696
}
@@ -109,10 +109,10 @@ impl<R: Read> Readable<R> for bool {
109109
// u8 arrays
110110
macro_rules! impl_array {
111111
( $size:expr ) => (
112-
impl<W: Writer> Writeable<W> for [u8; $size]
112+
impl Writeable for [u8; $size]
113113
{
114114
#[inline]
115-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
115+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
116116
w.write_all(self)
117117
}
118118
}
@@ -136,13 +136,12 @@ impl_array!(64); // for Signature
136136
impl_array!(1300); // for OnionPacket.hop_data
137137

138138
// HashMap
139-
impl<W, K, V> Writeable<W> for HashMap<K, V>
140-
where W: Writer,
141-
K: Writeable<W> + Eq + Hash,
142-
V: Writeable<W>
139+
impl<K, V> Writeable for HashMap<K, V>
140+
where K: Writeable + Eq + Hash,
141+
V: Writeable
143142
{
144143
#[inline]
145-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
144+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
146145
(self.len() as u16).write(w)?;
147146
for (key, value) in self.iter() {
148147
key.write(w)?;
@@ -169,9 +168,9 @@ impl<R, K, V> Readable<R> for HashMap<K, V>
169168
}
170169

171170
// Vectors
172-
impl<W: Writer> Writeable<W> for Vec<u8> {
171+
impl Writeable for Vec<u8> {
173172
#[inline]
174-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
173+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
175174
(self.len() as u16).write(w)?;
176175
w.write_all(&self)
177176
}
@@ -187,9 +186,9 @@ impl<R: Read> Readable<R> for Vec<u8> {
187186
Ok(ret)
188187
}
189188
}
190-
impl<W: Writer> Writeable<W> for Vec<Signature> {
189+
impl Writeable for Vec<Signature> {
191190
#[inline]
192-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
191+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
193192
(self.len() as u16).write(w)?;
194193
for e in self.iter() {
195194
e.write(w)?;
@@ -214,8 +213,8 @@ impl<R: Read> Readable<R> for Vec<Signature> {
214213
}
215214
}
216215

217-
impl<W: Writer> Writeable<W> for Script {
218-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
216+
impl Writeable for Script {
217+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
219218
(self.len() as u16).write(w)?;
220219
w.write_all(self.as_bytes())
221220
}
@@ -230,8 +229,8 @@ impl<R: Read> Readable<R> for Script {
230229
}
231230
}
232231

233-
impl<W: Writer> Writeable<W> for Option<Script> {
234-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
232+
impl Writeable for Option<Script> {
233+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
235234
if let &Some(ref script) = self {
236235
script.write(w)?;
237236
}
@@ -253,8 +252,8 @@ impl<R: Read> Readable<R> for Option<Script> {
253252
}
254253
}
255254

256-
impl<W: Writer> Writeable<W> for PublicKey {
257-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
255+
impl Writeable for PublicKey {
256+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
258257
self.serialize().write(w)
259258
}
260259
}
@@ -269,8 +268,8 @@ impl<R: Read> Readable<R> for PublicKey {
269268
}
270269
}
271270

272-
impl<W: Writer> Writeable<W> for Sha256dHash {
273-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
271+
impl Writeable for Sha256dHash {
272+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
274273
self.as_bytes().write(w)
275274
}
276275
}
@@ -282,8 +281,8 @@ impl<R: Read> Readable<R> for Sha256dHash {
282281
}
283282
}
284283

285-
impl<W: Writer> Writeable<W> for Signature {
286-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
284+
impl Writeable for Signature {
285+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
287286
self.serialize_compact(&Secp256k1::without_caps()).write(w)
288287
}
289288
}

src/util/ser_macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
macro_rules! impl_writeable {
22
($st:ident, $len: expr, {$($field:ident),*}) => {
3-
impl<W: Writer> Writeable<W> for $st {
4-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
3+
impl Writeable for $st {
4+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
55
w.size_hint($len);
66
$( self.$field.write(w)?; )*
77
Ok(())
@@ -19,8 +19,8 @@ macro_rules! impl_writeable {
1919
}
2020
macro_rules! impl_writeable_len_match {
2121
($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
22-
impl<W: Writer> Writeable<W> for $st {
23-
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
22+
impl Writeable for $st {
23+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
2424
w.size_hint(match *self {
2525
$($m => $l,)*
2626
});

0 commit comments

Comments
 (0)